Skip to content

Automate browser based workflows with AI

fix(SKY-11718): require affirmative NANP evidence for bare-digit tel fill + success-path logging (#7292)

Marc Kelechava05d0053f939f
diff --git a/skyvern/webeye/actions/handler.py b/skyvern/webeye/actions/handler.py
index b9a2dfd7b..1db35ef02 100644
--- a/skyvern/webeye/actions/handler.py
+++ b/skyvern/webeye/actions/handler.py
@@ -1055,13 +1055,28 @@ def _phone_readback_digits_match(expected_digits: str, actual_digits: str) -> bo
return expected_nanp_digits is not None and expected_nanp_digits == actual_nanp_digits
-def _is_plain_nanp_number(value: str | None) -> bool:
- # A 10-digit North American number with no '+'/country-code/extension markers. There is no
- # international handling, so a value carrying a '+' or letters (an extension) is excluded.
- text = value or ""
- if "+" in text or re.search(r"[A-Za-z]", text):
- return False
- return len(_phone_digits(text)) == 10
+def _nanp_national_digits(value: str | None) -> str | None:
+ text = (value or "").strip()
+ if re.search(r"[A-Za-z]", text):
+ return None
+
+ digits = _phone_digits(text)
+ # A leading 1 counts as a country code only when written as one (+1, or 1 set off by a
+ # separator); bare 11-digit strings can be non-NANP numbers whose first digit is 1.
+ if re.fullmatch(r"1[0-9]{10}", digits) and re.match(r"\+1|1[ \-.(]", text):
+ national_digits = digits[-10:]
+ elif re.fullmatch(
+ r"\([0-9]{3}\) [0-9]{3}-[0-9]{4}|[0-9]{3}-[0-9]{3}-[0-9]{4}|"
+ r"[0-9]{3}\.[0-9]{3}\.[0-9]{4}|[0-9]{3} [0-9]{3} [0-9]{4}",
+ text,
+ ):
+ national_digits = digits
+ else:
+ return None
+
+ if national_digits[0] not in "23456789" or national_digits[3] not in "23456789":
+ return None
+ return national_digits
def _tel_pattern_allows_bare_digits(pattern: str | None, bare_digits: str) -> bool:
@@ -1079,12 +1094,11 @@ def _tel_pattern_allows_bare_digits(pattern: str | None, bare_digits: str) -> bo
def _plan_tel_text(*, is_tel: bool, is_secret: bool, value: str, pattern: str | None) -> tuple[str, bool, bool]:
# Decide how to fill a tel field. Returns (text_to_type, used_bare_nanp, run_format_check).
- # A separator-formatted value is long enough to fill()-split into a half-open "(ddd" that a
- # self-formatting field collapses, dropping a digit; bare national digits avoid that. Stripping is a
- # local transform, so it is applied to secret values too. The format-check LLM is reserved for
- # non-secret values that the bare-digit path does not handle.
- if is_tel and _is_plain_nanp_number(value) and _tel_pattern_allows_bare_digits(pattern, _phone_digits(value)):
- return _phone_digits(value), True, False
+ # Bare national digits avoid the fill()-split behavior that can drop a digit in self-formatting fields.
+ # This local transform is safe for secrets; the format-check LLM remains limited to non-secret fallbacks.
+ national_digits = _nanp_national_digits(value) if is_tel else None
+ if national_digits and _tel_pattern_allows_bare_digits(pattern, national_digits):
+ return national_digits, True, False
return value, False, is_tel and not is_secret
@@ -1195,6 +1209,11 @@ async def verify_phone_input_digits(*, tag_name: str, locator: Locator, expected
expected_digit_count=len(expected_digits),
actual_digit_count=len(actual_digits),
)
+ LOG.info(
+ "Phone input read-back verified",
+ expected_digit_count=len(expected_digits),
+ actual_digit_count=len(actual_digits),
+ )
async def _verify_tel_input_after_fill(*, skyvern_element: SkyvernElement, tag_name: str, expected_value: str) -> None:
@@ -1205,6 +1224,33 @@ async def _verify_tel_input_after_fill(*, skyvern_element: SkyvernElement, tag_n
)
+async def _log_tel_fallback_fill_digit_counts(
+ *, skyvern_element: SkyvernElement, tag_name: str, expected_value: str, task_id: str | None, step_id: str | None
+) -> None:
+ # Observability only: the LLM-fallback tel fill has no raising read-back, so a digit drop there is
+ # otherwise invisible. Count-only (values may be secrets) and never fails the action.
+ try:
+ actual_value = await get_input_value(tag_name=tag_name, locator=skyvern_element.get_locator())
+ expected_digit_count = len(_phone_digits(expected_value))
+ actual_digit_count = len(_phone_digits(actual_value))
+ LOG.info(
+ "Tel fallback fill digit counts",
+ expected_digit_count=expected_digit_count,
+ actual_digit_count=actual_digit_count,
+ digit_count_match=expected_digit_count == actual_digit_count,
+ element_id=skyvern_element.get_id(),
+ task_id=task_id,
+ step_id=step_id,
+ )
+ except Exception:
+ LOG.warning(
+ "Failed to read back tel fallback fill",
+ task_id=task_id,
+ step_id=step_id,
+ exc_info=True,
+ )
+
+
_CARD_NUMBER_MIN_DIGITS = 13
_CARD_NUMBER_MAX_DIGITS = 19
@@ -3116,16 +3162,27 @@ async def handle_input_text_action(
is_card_number_input = _is_probable_card_number(candidate_card_digits) and await _is_card_number_field(
skyvern_element
)
- is_plain_nanp_tel = False
+ used_bare_nanp = False
run_phone_format_check = False
+ log_tel_fallback_readback = False
if is_tel and not is_card_number_input and await _is_tel_digit_fix_enabled(task):
# SKY-11315 fix, behind FIX_TEL_INPUT_DIGIT_DROP. Flag-off keeps the original behavior below
- # byte-for-byte. Plain-NANP tel is typed as bare digits (skipping the format-check LLM) unless
- # the field's pattern requires a mask; secrets are eligible (local strip, no LLM).
+ # byte-for-byte. Affirmative-NANP tel is typed as bare national digits (skipping the format-check
+ # LLM) unless the field's pattern requires a mask; secrets are eligible (local strip, no LLM).
tel_pattern = await skyvern_element.get_attr("pattern")
- text, is_plain_nanp_tel, run_phone_format_check = _plan_tel_text(
+ text, used_bare_nanp, run_phone_format_check = _plan_tel_text(
is_tel=True, is_secret=is_secret_value, value=text, pattern=tel_pattern
)
+ log_tel_fallback_readback = run_phone_format_check
+ if used_bare_nanp:
+ LOG.info(
+ "Tel bare-digit fill using national digits",
+ used_bare_nanp=True,
+ expected_digit_count=len(text),
+ element_id=skyvern_element.get_id(),
+ task_id=task.task_id,
+ step_id=step.step_id,
+ )
elif is_tel and not is_card_number_input and not is_secret_value:
run_phone_format_check = True
if run_phone_format_check:
@@ -3288,7 +3345,7 @@ async def handle_input_text_action(
return [result]
# Only the bare-digit NANP fill is read back to verify; other tel shapes are left unverified.
- verify_tel_input_after_fill = is_plain_nanp_tel
+ verify_tel_input_after_fill = used_bare_nanp
# SKY-11720: an auto-formatting card-number field (a space every 4 digits) restores its caret
# naively, racing character-by-character entry so the rendered value can silently differ from
@@ -3340,6 +3397,14 @@ async def handle_input_text_action(
actual_digit_count=mismatch.actual_digit_count,
)
return [ActionFailure(mismatch)]
+ elif log_tel_fallback_readback:
+ await _log_tel_fallback_fill_digit_counts(
+ skyvern_element=skyvern_element,
+ tag_name=tag_name,
+ expected_value=text,
+ task_id=task.task_id,
+ step_id=step.step_id,
+ )
incremental_element = await incremental_scraped.get_incremental_element_tree(
clean_and_remove_element_tree_factory(
diff --git a/tests/unit/test_input_text_phone_readback.py b/tests/unit/test_input_text_phone_readback.py
index 43e12541d..f65868854 100644
--- a/tests/unit/test_input_text_phone_readback.py
+++ b/tests/unit/test_input_text_phone_readback.py
@@ -5,7 +5,14 @@ from unittest.mock import AsyncMock, MagicMock
import pytest
from skyvern.exceptions import PhoneNumberInputMismatch
-from skyvern.webeye.actions.handler import verify_phone_input_digits
+from skyvern.webeye.actions.handler import _log_tel_fallback_fill_digit_counts, verify_phone_input_digits
+
+
+def _make_element(locator: MagicMock) -> MagicMock:
+ element = MagicMock()
+ element.get_locator.return_value = locator
+ element.get_id.return_value = "element-id"
+ return element
@pytest.mark.asyncio
@@ -16,10 +23,11 @@ async def test_phone_readback_accepts_matching_ten_digits() -> None:
await verify_phone_input_digits(tag_name="input", locator=locator, expected_value="224-555-0199")
[email protected]("actual_value", ["+1 (224) 555-0199", "+11 (224) 555-0199"])
@pytest.mark.asyncio
-async def test_phone_readback_accepts_country_code_readback() -> None:
+async def test_phone_readback_accepts_country_code_readback(actual_value: str) -> None:
locator = MagicMock()
- locator.input_value = AsyncMock(return_value="+1 (224) 555-0199")
+ locator.input_value = AsyncMock(return_value=actual_value)
await verify_phone_input_digits(tag_name="input", locator=locator, expected_value="2245550199")
@@ -34,3 +42,32 @@ async def test_phone_readback_digit_drop_raises() -> None:
assert exc.value.expected_digit_count == 10
assert exc.value.actual_digit_count == 9
+
+
+async def test_tel_fallback_digit_count_log_never_raises_on_mismatch() -> None:
+ # A 10 -> 9 digit drop on the LLM-fallback fill is observed and logged, never raised.
+ locator = MagicMock()
+ locator.input_value = AsyncMock(return_value="(224) 555-019")
+
+ await _log_tel_fallback_fill_digit_counts(
+ skyvern_element=_make_element(locator),
+ tag_name="input",
+ expected_value="(224) 555-0199",
+ task_id="tsk_1",
+ step_id="stp_1",
+ )
+
+
+async def test_tel_fallback_digit_count_log_swallows_read_errors() -> None:
+ locator = MagicMock()
+ locator.input_value = AsyncMock(side_effect=RuntimeError("element detached"))
+
+ await _log_tel_fallback_fill_digit_counts(
+ skyvern_element=_make_element(locator),
+ tag_name="input",
+ expected_value="2245550199",
+ task_id="tsk_1",
+ step_id="stp_1",
+ )
diff --git a/tests/unit/test_tel_input_bare_digits.py b/tests/unit/test_tel_input_bare_digits.py
index f0aa674d8..b70f8b80e 100644
--- a/tests/unit/test_tel_input_bare_digits.py
+++ b/tests/unit/test_tel_input_bare_digits.py
@@ -7,40 +7,67 @@ from unittest.mock import AsyncMock, MagicMock
import pytest
from skyvern.webeye.actions import handler_utils
-from skyvern.webeye.actions.handler import _is_plain_nanp_number, _is_tel_digit_fix_enabled, _plan_tel_text
+from skyvern.webeye.actions.handler import _is_tel_digit_fix_enabled, _nanp_national_digits, _plan_tel_text
from tests.unit.helpers import make_organization, make_task
@pytest.mark.parametrize(
"value,expected",
[
- ("224-555-0199", True),
- ("(224) 555-0199", True),
- ("2245550199", True),
- (" 224 555 0199 ", True),
- ("+44 20 7946 0958", False), # international: leave to the format-check LLM
- ("+1 224-555-0199", False), # explicit country code
- ("224-555-0199 x123", False), # extension marker
- ("1-224-555-0199", False), # 11 digits (leading country code)
- ("224-555-019", False), # 9 digits
- ("", False),
+ ("+1 (224) 555-0199", "2245550199"),
+ ("+12245550199", "2245550199"),
+ ("1-224-555-0199", "2245550199"),
+ ("1 (224) 555-0199", "2245550199"),
+ ("(224) 555-0199", "2245550199"),
+ ("224-555-0199", "2245550199"),
+ ("224.555.0199", "2245550199"),
+ ("224 555 0199", "2245550199"),
+ ("12245550199", None), # bare leading 1 is not written as a country code
+ ("13912345678", None), # 11-digit non-NANP mobile whose first digit is 1
+ ("15012345678", None),
+ ("13987654321", None),
+ ("(13) 98765-4321", None), # 2-digit area code + 9-digit subscriber, strips to 1 + 10
+ ("0412345678", None),
+ ("0412 345 678", None),
+ ("1234567890", None),
+ ("2245550199", None),
+ ("4155550123", None),
+ ("(024) 555-0199", None),
+ ("(224) 155-0199", None),
+ ("+44 20 7946 0958", None),
+ ("224-555-0199 x123", None),
+ ("224-555-019", None),
+ ("", None),
],
)
-def test_is_plain_nanp_number(value: str, expected: bool) -> None:
- assert _is_plain_nanp_number(value) is expected
+def test_nanp_national_digits(value: str, expected: str | None) -> None:
+ assert _nanp_national_digits(value) == expected
def test_plan_tel_text_strips_secret_resolved_formatted_nanp() -> None:
- # A secret that resolves to a formatted 10-digit NANP number is typed as bare digits and is never
- # sent to the format-check LLM.
+ # A secret with an affirmative NANP country code is typed as national digits and is never sent to
+ # the format-check LLM.
text, used_bare, run_format_check = _plan_tel_text(
- is_tel=True, is_secret=True, value="(224) 555-0199", pattern=None
+ is_tel=True, is_secret=True, value="+1 (224) 555-0199", pattern=None
)
assert text == "2245550199"
assert used_bare is True
assert run_format_check is False
+ "is_secret,expected_format_check",
+ [(False, True), (True, False)],
+)
+def test_plan_tel_text_ineligible_value_falls_back(is_secret: bool, expected_format_check: bool) -> None:
+ value = "0412 345 678"
+ assert _plan_tel_text(is_tel=True, is_secret=is_secret, value=value, pattern=None) == (
+ value,
+ False,
+ expected_format_check,
+ )
+
+
def test_plan_tel_text_self_formatting_field_uses_bare_digits() -> None:
# A permissive pattern that accepts bare digits keeps the bare-digit fast path.
text, used_bare, run_format_check = _plan_tel_text(