Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
Recover a verification code glued to the next word when parsing TOTP emails (SKY-17543) (#8813)
Shuchang Zheng55bea7f262b5
diff --git a/skyvern/services/otp_service.py b/skyvern/services/otp_service.pyindex 8edabf11f..8b1d10d41 100644--- a/skyvern/services/otp_service.py+++ b/skyvern/services/otp_service.py@@ -135,6 +135,10 @@ _CODE_SEPARATOR_PATTERN = re.compile(r"[\s\-]")_CODE_CANDIDATE_PATTERN = re.compile(r"(?<![A-Za-z0-9])\d{3,4}(?:[ \t]\d{3,4})+(?![A-Za-z0-9])|[A-Za-z0-9]+(?:-[A-Za-z0-9]+)*")+# HTML-to-text output can glue a code to the next word ("482913This"), which the tokenizer reads as one token.+# Six digits minimum keeps a footer year glued to the company name ("2026Example") out of the candidates.+_GLUED_DIGIT_RUN_PATTERN = re.compile(r"(?<![A-Za-z0-9])([0-9]{6,8})(?=[A-Z][a-z])")+_ALL_DIGITS_PATTERN = re.compile(r"[0-9]+")TOTP_WEBHOOK_EXPECTED_RESPONSE_SHAPE = '{"verification_code":"123456"}'# Recovers the verification_code value when the surrounding JSON is malformed# (e.g. unescaped quotes inside a relayed email). Assumes verification_code is@@ -236,6 +240,18 @@ def _verbatim_otp_value(content: str, otp_type: OTPType | None, llm_value: str |prefixed = [candidate for candidate in numeric_candidates if candidate.startswith(stripped_value)]if prefixed:return prefixed[0] if len(prefixed) == 1 else None+ # Glued runs rank below whole tokens and stand in only for an all-digit read no longer than themselves, so+ # a read that kept an alphanumeric code's letters or later digits ("482913Ab56") never shrinks to "482913".+ if _ALL_DIGITS_PATTERN.fullmatch(stripped_value):+ glued_runs = [+ run for run in dict.fromkeys(_GLUED_DIGIT_RUN_PATTERN.findall(content)) if len(run) >= len(stripped_value)+ ]+ if stripped_value in glued_runs:+ return stripped_value+ prefixed = [run for run in glued_runs if run.startswith(stripped_value)]+ if prefixed:+ return prefixed[0] if len(prefixed) == 1 else None+ numeric_candidates = list(dict.fromkeys([*numeric_candidates, *glued_runs]))# Approximate recovery never guesses: an equally-similar runner-up ("123456" and "123457" against a# misread "123458") means the source cannot say which code was located, so let polling retry instead.scored = sorted(diff --git a/tests/unit/test_otp_service.py b/tests/unit/test_otp_service.pyindex fd2c92693..0d0f621aa 100644--- a/tests/unit/test_otp_service.py+++ b/tests/unit/test_otp_service.py@@ -1204,6 +1204,63 @@ class TestParseOtpLogin:assert result is None # no digit-run candidate to recover from; better than storing a short code+ @pytest.mark.asyncio+ @pytest.mark.parametrize(+ ("code", "next_word"),+ [("482913", "This"), ("202613", "Note"), ("551207", "Enter")],+ )+ async def test_code_glued_to_the_next_word_is_recovered_not_the_footer_year(+ self, monkeypatch: pytest.MonkeyPatch, code: str, next_word: str+ ) -> None:+ _patch_otp_llm(monkeypatch, otp_type="totp", otp_value=code)+ content = (+ f"Your verification code is {code}{next_word} code expires in 10 minutes. "+ "(c)2026 Example Co, 1 Example Way, Exampletown 99999"+ )++ result = await parse_otp_login(content=content, organization_id="o_test")++ assert result is not None+ assert result.value == code++ @pytest.mark.asyncio+ @pytest.mark.parametrize(+ ("content", "llm_value", "expected"),+ [+ (+ (+ "Your verification code is 482913This code expires in 10 minutes. "+ "(c)2026 Example Co, 1 Example Way, Exampletown 99999"+ ),+ "48291",+ "482913",+ ),+ ("Your verification code is 202613This code expires in 10 minutes. (c)2026Example Co", "2026", "202613"),+ ("Your verification code is 482913. Order 48291377Shipped today. (c)2026Example Co", "48291", "482913"),+ ],+ ids=["glued_code", "glued_code_read_as_glued_year", "separate_code_beside_glued_number"],+ )+ async def test_truncated_read_recovers_the_whole_code_not_a_glued_number(+ self, monkeypatch: pytest.MonkeyPatch, content: str, llm_value: str, expected: str+ ) -> None:+ _patch_otp_llm(monkeypatch, otp_type="totp", otp_value=llm_value)++ result = await parse_otp_login(content=content, organization_id="o_test")++ assert result is not None+ assert result.value == expected++ @pytest.mark.asyncio+ @pytest.mark.parametrize("llm_value", ["482913ab56", "48291356"], ids=["case_misread", "letters_dropped"])+ async def test_case_misread_or_letter_dropped_alphanumeric_code_is_not_cut_to_its_digits(+ self, monkeypatch: pytest.MonkeyPatch, llm_value: str+ ) -> None:+ _patch_otp_llm(monkeypatch, otp_type="totp", otp_value=llm_value)++ result = await parse_otp_login(content="Your code: 482913Ab56 (c)2026 Example Co", organization_id="o_test")++ assert result is None or result.value == "482913Ab56" # never the fragment "482913" or the year+@pytest.mark.asyncioasync def test_does_not_charge_when_llm_call_raises(self, monkeypatch: pytest.MonkeyPatch) -> None:from skyvern.forge.sdk.api.llm.exceptions import LLMProviderError