Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
fix: add missing apply_stealth_config() to persistent stealth browser path [SKY-8789] (#5425)
Marc Kelechavab168f7694b59
diff --git a/skyvern/exceptions.py b/skyvern/exceptions.pyindex 6303eff98..1c539a798 100644--- a/skyvern/exceptions.py+++ b/skyvern/exceptions.py@@ -345,6 +345,32 @@ class UnknownErrorWhileCreatingBrowserContext(SkyvernException):@staticmethoddef _get_detail(exception: Exception) -> str:raw_message = str(exception).strip()+ raw_lower = raw_message.lower()++ # Browser launch environment errors: worker cannot initialize the+ # headed browser display/graphics stack (X display or EGL/SwiftShader).+ if any(+ indicator in raw_lower+ for indicator in (+ "missing x server",+ "xserver running",+ "no display",+ "$display",+ "the platform failed to initialize",+ "no suitable egl configs found",+ "failed to get config for surface",+ "collectgraphicsinfo failed",+ "glcontext::createoffscreenglsurface failed",+ "exiting gpu process due to errors during initialization",+ )+ ):+ return (+ "Browser launch failed: worker node could not initialize the browser display/graphics stack "+ "(X display/EGL). This is an infrastructure or browser-environment issue on the worker node, "+ "not a browser profile problem. "+ f"{UnknownErrorWhileCreatingBrowserContext.SUPPORT_GUIDANCE}"+ )+# Patchright timeout errors include a verbose "Call log" section with launch args.trimmed_message = raw_message.split("Call log:")[0].strip()normalized_message = " ".join(trimmed_message.split())diff --git a/skyvern/webeye/browser_factory.py b/skyvern/webeye/browser_factory.pyindex bac7a5521..913f75c73 100644--- a/skyvern/webeye/browser_factory.py+++ b/skyvern/webeye/browser_factory.py@@ -426,6 +426,30 @@ def _get_proxy_server_creds(proxy: str) -> dict:return {}+def _is_display_server_error(error: Exception) -> bool:+ """Return True if the worker cannot initialize the browser display/graphics stack.++ These errors appear when a headed browser is launched on a worker node+ without a usable display/graphics environment. Retrying with a fresh profile+ will not help — the fix is environment-side (display/EGL/SwiftShader support)+ rather than profile-side.+ """+ error_str = str(error).lower()+ display_indicators = [+ "missing x server",+ "xserver running",+ "no display",+ "$display",+ "the platform failed to initialize",+ "no suitable egl configs found",+ "failed to get config for surface",+ "collectgraphicsinfo failed",+ "glcontext::createoffscreenglsurface failed",+ "exiting gpu process due to errors during initialization",+ ]+ return any(indicator in error_str for indicator in display_indicators)++def _is_browser_profile_corruption_error(error: Exception) -> bool:"""Return True if the error is consistent with a corrupted or bloated browser profile.@@ -433,6 +457,9 @@ def _is_browser_profile_corruption_error(error: Exception) -> bool:the user_data_dir is in a bad state (corrupted files, oversized cache, lock filesfrom a prior crash, etc.). The error text comes from Playwright's CDP driver."""+ if _is_display_server_error(error):+ return False+error_str = str(error).lower()corruption_indicators = ["connection closed while reading from the driver",diff --git a/tests/unit/test_browser_profile_corruption_detection.py b/tests/unit/test_browser_profile_corruption_detection.pynew file mode 100644index 000000000..678adab45--- /dev/null+++ b/tests/unit/test_browser_profile_corruption_detection.py@@ -0,0 +1,107 @@+"""Tests for browser launch error classification in browser_factory.py."""++from skyvern.webeye.browser_factory import (+ _is_browser_profile_corruption_error,+ _is_display_server_error,+)++# -- _is_display_server_error ------------------------------------------------+++class TestIsDisplayServerError:+ """Detect browser launch environment errors so we skip the profile-corruption retry."""++ def test_missing_x_server_message(self) -> None:+ error = Exception(+ "BrowserType.launch_persistent_context: Target page, context or browser has been closed\n\n"+ "Browser logs:\n"+ "Looks like you launched a headed browser without having a XServer running.\n"+ "Set either 'headless: true' or use 'xvfb-run <your-playwright-app>' before running Playwright."+ )+ assert _is_display_server_error(error) is True++ def test_missing_x_server_with_display_var(self) -> None:+ error = Exception("[err] Missing X server or $DISPLAY")+ assert _is_display_server_error(error) is True++ def test_platform_failed_to_initialize(self) -> None:+ error = Exception("[err] ui/aura/env.cc: The platform failed to initialize. Exiting.")+ assert _is_display_server_error(error) is True++ def test_egl_config_failure(self) -> None:+ error = Exception(+ "[err] [297028:297028:0407/015340.854525:ERROR:ui/gl/gl_surface_egl.cc:1013] No suitable EGL configs found for initialization."+ )+ assert _is_display_server_error(error) is True++ def test_gpu_process_initialization_failure(self) -> None:+ error = Exception(+ "[err] [297028:297028:0407/015340.854713:ERROR:gpu/ipc/service/gpu_init.cc:118] CollectGraphicsInfo failed."+ )+ assert _is_display_server_error(error) is True++ def test_no_display_env(self) -> None:+ error = Exception("No display environment variable set")+ assert _is_display_server_error(error) is True++ def test_actual_corruption_is_not_display_error(self) -> None:+ error = Exception("unable to open database file")+ assert _is_display_server_error(error) is False++ def test_target_closed_without_display_context(self) -> None:+ error = Exception("Target page, context or browser has been closed")+ assert _is_display_server_error(error) is False++ def test_generic_error_is_not_display_error(self) -> None:+ error = Exception("something completely unrelated went wrong")+ assert _is_display_server_error(error) is False+++# -- _is_browser_profile_corruption_error ------------------------------------+++class TestIsBrowserProfileCorruptionError:+ """Profile corruption detection must NOT match display-server errors."""++ def test_xserver_error_not_classified_as_corruption(self) -> None:+ error = Exception(+ "BrowserType.launch_persistent_context: Target page, context or browser has been closed\n\n"+ "Browser logs:\n"+ "Looks like you launched a headed browser without having a XServer running.\n"+ "[err] Missing X server or $DISPLAY\n"+ "[err] ui/aura/env.cc: The platform failed to initialize. Exiting."+ )+ assert _is_browser_profile_corruption_error(error) is False++ def test_egl_error_not_classified_as_corruption(self) -> None:+ error = Exception(+ "BrowserType.launch_persistent_context: Target page, context or browser has been closed\n\n"+ "[err] [297028:297028:0407/015340.854525:ERROR:ui/gl/gl_surface_egl.cc:1013] No suitable EGL configs found for initialization.\n"+ "[err] [297028:297028:0407/015340.854713:ERROR:gpu/ipc/service/gpu_init.cc:118] CollectGraphicsInfo failed."+ )+ assert _is_browser_profile_corruption_error(error) is False++ def test_target_closed_without_xserver_is_corruption(self) -> None:+ """A plain 'target closed' without XServer context is still corruption."""+ error = Exception("target closed")+ assert _is_browser_profile_corruption_error(error) is True++ def test_connection_closed_while_reading(self) -> None:+ error = Exception("connection closed while reading from the driver")+ assert _is_browser_profile_corruption_error(error) is True++ def test_browser_has_been_closed(self) -> None:+ error = Exception("browser has been closed")+ assert _is_browser_profile_corruption_error(error) is True++ def test_failed_to_launch(self) -> None:+ error = Exception("failed to launch chromium")+ assert _is_browser_profile_corruption_error(error) is True++ def test_unable_to_open_database(self) -> None:+ error = Exception("unable to open database file")+ assert _is_browser_profile_corruption_error(error) is True++ def test_unrelated_error_is_not_corruption(self) -> None:+ error = Exception("network timeout after 30 seconds")+ assert _is_browser_profile_corruption_error(error) is Falsediff --git a/tests/unit/test_exception_messages.py b/tests/unit/test_exception_messages.pyindex eca3239de..a2200f8a2 100644--- a/tests/unit/test_exception_messages.py+++ b/tests/unit/test_exception_messages.py@@ -67,3 +67,45 @@ def test_non_browser_error_not_intercepted() -> None:"""Regular errors should still pass through as-is."""message = get_user_facing_exception_message(ValueError("some other error"))assert message == "Unexpected error: some other error"+++def test_unknown_error_display_server_missing_xserver() -> None:+ inner_exception = Exception(+ "BrowserType.launch_persistent_context: Target page, context or browser has been closed\n\n"+ "Browser logs:\n"+ "Looks like you launched a headed browser without having a XServer running.\n"+ "[err] Missing X server or $DISPLAY\n"+ "[err] ui/aura/env.cc: The platform failed to initialize. Exiting."+ )+ error = UnknownErrorWhileCreatingBrowserContext("dynamic-browser", inner_exception)+ message = str(error)+ assert "browser display/graphics stack" in message+ assert "browser-environment issue" in message+ assert "[email protected]" in message+++def test_unknown_error_display_server_platform_failed() -> None:+ inner_exception = Exception("[err] ui/aura/env.cc: The platform failed to initialize. Exiting.")+ error = UnknownErrorWhileCreatingBrowserContext("dynamic-browser", inner_exception)+ message = str(error)+ assert "browser display/graphics stack" in message+++def test_unknown_error_display_server_egl_failure() -> None:+ inner_exception = Exception(+ "[err] [297028:297028:0407/015340.854525:ERROR:ui/gl/gl_surface_egl.cc:1013] "+ "No suitable EGL configs found for initialization.\n"+ "[err] [297028:297028:0407/015340.854713:ERROR:gpu/ipc/service/gpu_init.cc:118] "+ "CollectGraphicsInfo failed."+ )+ error = UnknownErrorWhileCreatingBrowserContext("dynamic-browser", inner_exception)+ message = str(error)+ assert "browser display/graphics stack" in message+ assert "browser profile problem" in message+++def test_unknown_error_display_server_no_display() -> None:+ inner_exception = Exception("No display environment variable set")+ error = UnknownErrorWhileCreatingBrowserContext("dynamic-browser", inner_exception)+ message = str(error)+ assert "browser display/graphics stack" in message