Skip to content

Automate browser based workflows with AI

Ask for the country code when a phone field reads a national number as international (SKY-18276) (#8896)

pedrohsdbcdae5a16edd3
diff --git a/skyvern/forge/taskv3/field_commit.py b/skyvern/forge/taskv3/field_commit.py
index 77860eac8..6814d501a 100644
--- a/skyvern/forge/taskv3/field_commit.py
+++ b/skyvern/forge/taskv3/field_commit.py
@@ -100,6 +100,9 @@ class EntryMatch(str, Enum):
REFORMATTED = "reformatted"
# Nothing landed: the field is empty, or holds exactly what it held before.
NOT_HELD = "not_held"
+ # A national phone number the field read as international: the same digits behind a "+" the page added, so its
+ # first digits now stand for a country code.
+ PLUS_ADDED = "plus_added"
DIFFERENT = "different"
@@ -137,7 +140,36 @@ def _groups(text: str) -> list[str]:
return [str(int(g)) if len(g) <= 2 else g for g in groups]
+# An input mask's empty slots, such as "(___) ___-____": nothing landed.
+# A field that hides its value ("***") is not empty.
+_MASK_PLACEHOLDER_RE = re.compile(r"[\s()\-/.]*_[\s()\-/._]*")
+# A phone number as typed or shown: digits and mask separators after an optional "+"; no point, so never a decimal.
+_PHONE_RE = re.compile(r"\+?[\d\s()\-/]+", re.ASCII)
+# Shorter digit runs are quantities or codes as often as phone numbers.
+_PHONE_MIN_DIGITS = 8
+
+
+def _phone_digits(text: str) -> str | None:
+ return re.sub(r"\D", "", text) if _PHONE_RE.fullmatch(text.strip()) else None
+
+
+def _plus_added(typed: str, held: str) -> bool:
+ digits = _phone_digits(typed)
+ return (
+ digits is not None
+ and len(digits) >= _PHONE_MIN_DIGITS
+ and not typed.strip().startswith("+")
+ and held.strip().startswith("+")
+ and _phone_digits(held) == digits
+ )
+
+
def _masked_same(typed: str, held: str) -> bool:
+ if typed.strip().startswith("+") and held.strip().startswith("+"):
+ # An international number is its digits after the "+"; a field regroups them however its country formats them.
+ digits = _phone_digits(typed)
+ if digits and digits == _phone_digits(held):
+ return True
if _number_changed(typed, held):
return False
if _MASK_SEPARATOR_RE.search(typed.strip()) and _MASK_SEPARATOR_RE.search(held.strip()):
@@ -160,6 +192,10 @@ def compare_text(typed: str, held: str | None, before: str | None = None, *, exa
return EntryMatch.NOT_HELD
if exact:
return EntryMatch.NOT_HELD if before is not None and held == before else EntryMatch.DIFFERENT
+ if _MASK_PLACEHOLDER_RE.fullmatch(held) and re.search(r"[^\W_]", typed):
+ return EntryMatch.NOT_HELD
+ if _plus_added(typed, held):
+ return EntryMatch.PLUS_ADDED
if _masked_same(typed, held) or _same_cell_value(held, typed) or ("," in typed and _same_cell_value(typed, held)):
return EntryMatch.REFORMATTED
if before is not None and held == before:
@@ -195,6 +231,18 @@ def entry_outcome(selector: str, match: EntryMatch | None, *, shown: str | None,
error_class="text_not_held",
)
holds = f"'{shown}'" if shown is not None else "a different value"
+ if match is EntryMatch.PLUS_ADDED:
+ return ToolResult(
+ "ok",
+ f"{did} {selector}, but the field now holds {holds}: it put '+' in front of the digits, so it reads the "
+ "first of them as a country calling code. If the digits you typed began with the country calling code "
+ "(which never starts with 0), this is the right number, but it stays unconfirmed until you retype it "
+ "starting with '+'. If not, the number is wrong and typing the same digits again gives the same result: "
+ "retype the full number starting with '+' and the country calling code that matches the phone number or "
+ "country in the task data; if the data gives no country, say so instead of guessing a code.",
+ ok_class="held_international",
+ entry_unconfirmed=True,
+ )
return ToolResult(
"ok",
f"{did} {selector}, but the field now holds {holds}, not what was entered: the page may have reformatted, "
diff --git a/skyvern/forge/taskv3/loop.py b/skyvern/forge/taskv3/loop.py
index ac45c8dbd..0be95ae94 100644
--- a/skyvern/forge/taskv3/loop.py
+++ b/skyvern/forge/taskv3/loop.py
@@ -192,9 +192,11 @@ ToolOkClass = Literal[
"document_ready",
"committed_not_loaded",
# A field-entry read-back (field_commit): the field shows the value in its own format, holds something other
- # than what was entered, or could not be read at all.
+ # than what was entered (`held_international`: a national phone number it put a "+" in front of), or could not be
+ # read at all.
"held_as",
"held_differs",
+ "held_international",
"unverified",
]
diff --git a/skyvern/forge/taskv3/tools.py b/skyvern/forge/taskv3/tools.py
index 4eff2f8a0..4a08bef02 100644
--- a/skyvern/forge/taskv3/tools.py
+++ b/skyvern/forge/taskv3/tools.py
@@ -18450,7 +18450,7 @@ def build_browser_tools(
return entry
if password_anchor is not None:
input_dispatch.note_secret_fill(page, password_anchor)
- if press_enter and entry.ok_class == "held_differs":
+ if press_enter and entry.ok_class in ("held_differs", "held_international"):
# Enter would submit a value the field shows is not the one typed, before the batch rule could hold it back;
# an unreadable field is no such evidence, so Enter still goes there, as before.
entry.content += " Enter was not pressed: check the field, then press Enter once it holds the value."
diff --git a/tests/unit/test_taskv3_field_commit.py b/tests/unit/test_taskv3_field_commit.py
index e94487f46..14f01caee 100644
--- a/tests/unit/test_taskv3_field_commit.py
+++ b/tests/unit/test_taskv3_field_commit.py
@@ -114,6 +114,44 @@ def test_compare_text_never_accepts_extra_or_missing_alphanumerics() -> None:
assert compare_text(changed, number) is EntryMatch.DIFFERENT, (changed, number)
+ ("typed", "held", "match"),
+ [
+ # A national number the field read as international: same digits, a "+" the page added.
+ ("5550001234", "+55 50001234", EntryMatch.PLUS_ADDED),
+ ("(555) 000-1234", "+5 550001234", EntryMatch.PLUS_ADDED),
+ ("(555) 000-1234", "+(555) 000-1234", EntryMatch.PLUS_ADDED),
+ ("004455500012", "+004455500012", EntryMatch.PLUS_ADDED),
+ # The fix: the number retyped with its country code, regrouped by the field.
+ ("+1 5550001234", "+1 555 000 1234", EntryMatch.REFORMATTED),
+ ("+44 20 0000 1234", "+442000001234", EntryMatch.REFORMATTED),
+ # A decimal point still decides first, with "+" on both sides.
+ ("+15.00", "+1500", EntryMatch.DIFFERENT),
+ ("+1.5", "+15", EntryMatch.DIFFERENT),
+ # Too short to be a phone number: a sign the page added is a changed number, not a hint.
+ ("1500", "+1500", EntryMatch.DIFFERENT),
+ # A country code the page added is not the same digits.
+ ("5550001234", "+1 555 000 1234", EntryMatch.DIFFERENT),
+ # A mask placeholder is not a held value, with or without the picker's code left in it.
+ ("5550001234", "(___) ___-____", EntryMatch.NOT_HELD),
+ ("09/05/2026", "__/__/____", EntryMatch.NOT_HELD),
+ ("5550001234", "+1 (___) ___-____", EntryMatch.DIFFERENT),
+ # A field that masks what it holds is not empty.
+ ("555000123", "***-**-****", EntryMatch.DIFFERENT),
+ ("hello", "•••••", EntryMatch.DIFFERENT),
+ ],
+)
+def test_compare_text_phone_numbers(typed: str, held: str, match: EntryMatch) -> None:
+ assert compare_text(typed, held) is match
+
+
+def test_a_plus_added_entry_asks_for_the_country_code_without_naming_one() -> None:
+ r = entry_outcome("#tel", EntryMatch.PLUS_ADDED, shown="+55 50001234")
+ assert r.status == "ok" and r.ok_class == "held_international" and r.entry_unconfirmed
+ assert "'+55 50001234'" in r.content
+ assert "+1" not in r.content
+
+
def test_an_exact_compare_never_folds_a_secret() -> None:
assert compare_text("Ab C", "ab c") is EntryMatch.REFORMATTED
assert compare_text("Ab C", "ab c", exact=True) is EntryMatch.DIFFERENT
diff --git a/tests/unit/test_taskv3_tools.py b/tests/unit/test_taskv3_tools.py
index 406638a76..c09fefc15 100644
--- a/tests/unit/test_taskv3_tools.py
+++ b/tests/unit/test_taskv3_tools.py
@@ -43578,6 +43578,25 @@ async def test_type_does_not_press_enter_on_a_value_the_field_did_not_keep() ->
assert not submitted
+@_skip_no_browser
+async def test_a_phone_field_that_reads_a_national_number_as_international_asks_for_the_country_code() -> None:
+ html = '<form onsubmit="window.__submitted = true; return false"><input id="tel" type="tel"></form>'
+ html += "<script>document.getElementById('tel').addEventListener('input', (e) => {"
+ html += (
+ " const d = e.target.value.replace(/\\D/g, ''); e.target.value = '+' + d.slice(0, 2) + ' ' + d.slice(2); });"
+ )
+ html += "</script>"
+ async with _content_page(html) as page:
+ tools = build_browser_tools(_fixed_page_provider(page))
+ r = await _tool(tools, "type").handler({"selector": "#tel", "text": "5550001234", "press_enter": True})
+ submitted = await page.evaluate("() => window.__submitted === true")
+ fixed = await _tool(tools, "type").handler({"selector": "#tel", "text": "+1 5550001234"})
+ assert r.ok_class == "held_international" and r.entry_unconfirmed, r.content
+ assert "Enter was not pressed" in r.content and not submitted
+ assert fixed.ok_class == "held_as" and not fixed.entry_unconfirmed, fixed.content
+
+
@_skip_no_browser
@pytest.mark.asyncio
async def test_a_password_the_page_changed_is_never_a_reformat_and_is_not_submitted() -> None: