Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
fix(SKY-9292): retry OTP polling on transient fetch failures (#5698)
LawyZheng34061bab793b
diff --git a/skyvern/config.py b/skyvern/config.pyindex d7b94be68..82adf8642 100644--- a/skyvern/config.py+++ b/skyvern/config.py@@ -420,6 +420,7 @@ class Settings(BaseSettings):TOTP_LIFESPAN_MINUTES: int = 10VERIFICATION_CODE_INITIAL_WAIT_TIME_SECS: int = 40VERIFICATION_CODE_POLLING_TIMEOUT_MINS: int = 15+ VERIFICATION_CODE_POLLING_MAX_CONSECUTIVE_FAILURES: int = 3# Bitwarden SettingsBITWARDEN_CLIENT_ID: str | None = Nonediff --git a/skyvern/services/otp_service.py b/skyvern/services/otp_service.pyindex d06feaa51..e63a48f60 100644--- a/skyvern/services/otp_service.py+++ b/skyvern/services/otp_service.py@@ -187,6 +187,8 @@ async def poll_otp_value(totp_verification_url=totp_verification_url,totp_identifier=totp_identifier,)+ max_consecutive_failures = settings.VERIFICATION_CODE_POLLING_MAX_CONSECUTIVE_FAILURES+ consecutive_failures = 0while True:await asyncio.sleep(10)# check timeout@@ -200,22 +202,38 @@ async def poll_otp_value(totp_identifier=totp_identifier,)otp_value: OTPValue | None = None- if totp_verification_url:- otp_value = await _get_otp_value_from_url(- organization_id,- totp_verification_url,- org_token.token,- task_id=task_id,- workflow_run_id=workflow_run_id,- )- elif totp_identifier:- otp_value = await _get_otp_value_from_db(- organization_id,- totp_identifier,+ try:+ if totp_verification_url:+ otp_value = await _get_otp_value_from_url(+ organization_id,+ totp_verification_url,+ org_token.token,+ task_id=task_id,+ workflow_run_id=workflow_run_id,+ )+ elif totp_identifier:+ otp_value = await _get_otp_value_from_db(+ organization_id,+ totp_identifier,+ task_id=task_id,+ workflow_id=workflow_permanent_id,+ workflow_run_id=workflow_run_id,+ )+ except FailedToGetTOTPVerificationCode:+ consecutive_failures += 1+ LOG.warning(+ "OTP fetch failed, will retry",+ consecutive_failures=consecutive_failures,+ max_consecutive_failures=max_consecutive_failures,task_id=task_id,- workflow_id=workflow_permanent_id,workflow_run_id=workflow_run_id,+ totp_verification_url=totp_verification_url,+ totp_identifier=totp_identifier,)+ if consecutive_failures >= max_consecutive_failures:+ raise+ continue+ consecutive_failures = 0 # reset on any non-exception poll, even if otp_value is Noneif otp_value:LOG.info("Got otp value", otp_value=otp_value)return otp_valuediff --git a/tests/unit/test_otp_service.py b/tests/unit/test_otp_service.pyindex e4d2c4245..1ba4b8cfa 100644--- a/tests/unit/test_otp_service.py+++ b/tests/unit/test_otp_service.py@@ -2,9 +2,17 @@from __future__ import annotations+from unittest.mock import AsyncMock, MagicMock, patch+import pytest-from skyvern.services.otp_service import _is_mfa_like_parameter_key, extract_totp_from_navigation_inputs+from skyvern.exceptions import FailedToGetTOTPVerificationCode+from skyvern.services.otp_service import (+ OTPValue,+ _is_mfa_like_parameter_key,+ extract_totp_from_navigation_inputs,+ poll_otp_value,+)class TestIsMfaLikeParameterKey:@@ -95,3 +103,95 @@ class TestExtractTotpFromNavigationInputs:}result = extract_totp_from_navigation_inputs(payload)assert result is None+++def _mock_org_token() -> MagicMock:+ token = MagicMock()+ token.token = "fake-token"+ return token+++class TestPollOtpValueRetry:+ """poll_otp_value should tolerate transient FailedToGetTOTPVerificationCode up to max consecutive failures."""++ @pytest.mark.asyncio+ @patch("skyvern.services.otp_service.asyncio.sleep", new_callable=AsyncMock)+ @patch("skyvern.services.otp_service._get_otp_value_from_url", new_callable=AsyncMock)+ @patch("skyvern.services.otp_service.app")+ @patch("skyvern.services.otp_service.settings")+ async def test_retries_on_transient_failure_then_succeeds(+ self, mock_settings: MagicMock, mock_app: MagicMock, mock_fetch: AsyncMock, mock_sleep: AsyncMock+ ) -> None:+ """Fail twice, then return OTP on third attempt. Counter resets, OTP is returned."""+ mock_settings.VERIFICATION_CODE_POLLING_TIMEOUT_MINS = 15+ mock_settings.VERIFICATION_CODE_POLLING_MAX_CONSECUTIVE_FAILURES = 3+ mock_app.DATABASE.organizations.get_valid_org_auth_token = AsyncMock(return_value=_mock_org_token())++ otp = OTPValue(value="123456")+ mock_fetch.side_effect = [+ FailedToGetTOTPVerificationCode(reason="connection error"),+ FailedToGetTOTPVerificationCode(reason="connection error"),+ otp,+ ]++ result = await poll_otp_value(+ organization_id="o_test",+ task_id="tsk_test",+ totp_verification_url="https://example.com/mfa",+ )+ assert result == otp+ assert mock_fetch.call_count == 3++ @pytest.mark.asyncio+ @patch("skyvern.services.otp_service.asyncio.sleep", new_callable=AsyncMock)+ @patch("skyvern.services.otp_service._get_otp_value_from_url", new_callable=AsyncMock)+ @patch("skyvern.services.otp_service.app")+ @patch("skyvern.services.otp_service.settings")+ async def test_raises_after_max_consecutive_failures(+ self, mock_settings: MagicMock, mock_app: MagicMock, mock_fetch: AsyncMock, mock_sleep: AsyncMock+ ) -> None:+ """After N consecutive failures, re-raise FailedToGetTOTPVerificationCode."""+ mock_settings.VERIFICATION_CODE_POLLING_TIMEOUT_MINS = 15+ mock_settings.VERIFICATION_CODE_POLLING_MAX_CONSECUTIVE_FAILURES = 3+ mock_app.DATABASE.organizations.get_valid_org_auth_token = AsyncMock(return_value=_mock_org_token())++ mock_fetch.side_effect = FailedToGetTOTPVerificationCode(reason="connection error")++ with pytest.raises(FailedToGetTOTPVerificationCode):+ await poll_otp_value(+ organization_id="o_test",+ task_id="tsk_test",+ totp_verification_url="https://example.com/mfa",+ )+ assert mock_fetch.call_count == 3++ @pytest.mark.asyncio+ @patch("skyvern.services.otp_service.asyncio.sleep", new_callable=AsyncMock)+ @patch("skyvern.services.otp_service._get_otp_value_from_url", new_callable=AsyncMock)+ @patch("skyvern.services.otp_service.app")+ @patch("skyvern.services.otp_service.settings")+ async def test_counter_resets_after_success(+ self, mock_settings: MagicMock, mock_app: MagicMock, mock_fetch: AsyncMock, mock_sleep: AsyncMock+ ) -> None:+ """Fail twice, succeed (None), fail twice again, then return OTP. Counter resets between failure bursts."""+ mock_settings.VERIFICATION_CODE_POLLING_TIMEOUT_MINS = 15+ mock_settings.VERIFICATION_CODE_POLLING_MAX_CONSECUTIVE_FAILURES = 3+ mock_app.DATABASE.organizations.get_valid_org_auth_token = AsyncMock(return_value=_mock_org_token())++ otp = OTPValue(value="654321")+ mock_fetch.side_effect = [+ FailedToGetTOTPVerificationCode(reason="err"),+ FailedToGetTOTPVerificationCode(reason="err"),+ None, # success (no OTP yet, but resets counter)+ FailedToGetTOTPVerificationCode(reason="err"),+ FailedToGetTOTPVerificationCode(reason="err"),+ otp,+ ]++ result = await poll_otp_value(+ organization_id="o_test",+ task_id="tsk_test",+ totp_verification_url="https://example.com/mfa",+ )+ assert result == otp+ assert mock_fetch.call_count == 6