Skip to content

Automate browser based workflows with AI

Hand a stored run-scoped sign-in link to the Task V3 code tool (SKY-17544) (#8799)

Shuchang Zheng4e60ca4bcec4
diff --git a/skyvern/forge/taskv3/auth_tools.py b/skyvern/forge/taskv3/auth_tools.py
index e39aeeceb..953819b17 100644
--- a/skyvern/forge/taskv3/auth_tools.py
+++ b/skyvern/forge/taskv3/auth_tools.py
@@ -45,6 +45,7 @@ from skyvern.forge.taskv3.tools import (
TotpCodeAuthorizer,
)
from skyvern.services.otp_service import (
+ MagicLinkSurfacing,
OTPValue,
extract_totp_from_navigation_inputs,
has_otp_source,
@@ -618,6 +619,8 @@ def build_auth_tools(
state.budget_seconds = settings.VERIFICATION_CODE_POLLING_TIMEOUT_MINS * 60.0
# A value one tool resolved that the other tool owns (the webhook source does not filter by type).
cached_otp_value: OTPValue | None = None
+ # Links opened or passed over, so a code poll never hands one of them, or an older link, back.
+ spent_links: set[str] = set()
first_poll_started_at: datetime | None = None
# Consecutive answers that delivered nothing: an exception, a bare None, or a slice whose webhook
# polls were failing at its timeout. One says little about the source; a second in a row is a
@@ -700,6 +703,13 @@ def build_auth_tools(
return _budget_exhausted(expected_otp_type)
if first_poll_started_at is None:
first_poll_started_at = datetime.utcnow()
+ # A code poll may hand back a sign-in link stored for this run since this task began, but only when
+ # the link tool can open it; without a page the link would end the run as unsupported.
+ surface_magic_link = (
+ MagicLinkSurfacing(created_after=task.created_at, spent_values=frozenset(spent_links))
+ if expected_otp_type == OTPType.TOTP and offer_link_tool
+ else None
+ )
started = time.monotonic()
try:
otp_value = await resolve_otp_value(
@@ -709,6 +719,7 @@ def build_auth_tools(
poll_started_at=first_poll_started_at,
allowed_credential_parameter_keys=allowed_credential_parameter_keys,
min_remaining_seconds=state.totp_min_remaining_seconds,
+ surface_magic_link=surface_magic_link,
)
if otp_value is None:
if expected_otp_type == OTPType.MAGIC_LINK:
@@ -779,15 +790,19 @@ def build_auth_tools(
cached = cached_otp_value
if cached is not None:
if cached.get_otp_type() == OTPType.MAGIC_LINK:
- if offer_link_tool:
- return ToolResult.error(_MAGIC_LINK_REDIRECT)
- return _failed(
- _MAGIC_LINK_UNSUPPORTED,
- reason=VerificationFailure.MAGIC_LINK_UNSUPPORTED_CACHED,
- tool="get_verification_code",
- )
- cached_otp_value = None
- return _deliver_code(cached.value)
+ if not offer_link_tool:
+ return _failed(
+ _MAGIC_LINK_UNSUPPORTED,
+ reason=VerificationFailure.MAGIC_LINK_UNSUPPORTED_CACHED,
+ tool="get_verification_code",
+ )
+ # Asked for a code again instead of opening the link: the page may want a code after all, so
+ # poll for one rather than repeat the redirect, which would starve a code that arrives later.
+ spent_links.add(cached.value)
+ cached_otp_value = None
+ else:
+ cached_otp_value = None
+ return _deliver_code(cached.value)
polled = await _poll(OTPType.TOTP)
if isinstance(polled, ToolResult):
@@ -846,6 +861,7 @@ def build_auth_tools(
)
url = otp_value.value
+ spent_links.add(url)
# Held only while nothing has been attempted: once a link is handed to the browser or refused,
# it is treated as spent whatever the outcome, so a retry polls for a fresh one.
cached_otp_value = otp_value
diff --git a/skyvern/services/otp_service.py b/skyvern/services/otp_service.py
index 446c237c0..8edabf11f 100644
--- a/skyvern/services/otp_service.py
+++ b/skyvern/services/otp_service.py
@@ -31,6 +31,7 @@ from skyvern.forge.sdk.api.llm.api_handler_factory import get_org_aware_secondar
from skyvern.forge.sdk.core import skyvern_context
from skyvern.forge.sdk.core.aiohttp_helper import DEFAULT_REQUEST_TIMEOUT
from skyvern.forge.sdk.core.security import generate_skyvern_webhook_signature
+from skyvern.forge.sdk.db.datetime_utils import to_naive_utc
from skyvern.forge.sdk.db.enums import OrganizationAuthTokenType
from skyvern.forge.sdk.schemas.organizations import OrganizationAuthToken
from skyvern.forge.sdk.schemas.totp_codes import OTPType, RawTOTPCode, TOTPCode
@@ -73,6 +74,16 @@ class RawOTPVerificationContext:
store_queried: bool = False
+@dataclass(frozen=True)
+class MagicLinkSurfacing:
+ """Lets a code poll hand back a stored sign-in link bound to this run or task, for a caller that can open
+ one. Links created before ``created_after`` are never handed back, and a link in ``spent_values`` retires
+ itself and every older link."""
+
+ created_after: datetime | None
+ spent_values: frozenset[str]
+
+
@dataclass
class WebhookOTPVerificationContext:
"""What ``totp_verification_url`` actually answered across a whole poll.
@@ -244,7 +255,7 @@ def _verbatim_otp_value(content: str, otp_type: OTPType | None, llm_value: str |
def looks_like_magic_link(content: str) -> bool:
"""Whether a message body is a bare sign-in link, decided without an LLM call.
- Used only to explain a wrong-verb timeout: an enforced parse returns None rather than the
+ Used to explain a wrong-verb timeout: an enforced parse returns None rather than the
other OTP type, so the type that did arrive is otherwise unrecoverable.
"""
return bool(_BARE_URL_PATTERN.match(content.strip()))
@@ -882,6 +893,7 @@ async def resolve_otp_value(
multi_field_expected_digits: int | None = None,
*,
min_remaining_seconds: int = 0,
+ surface_magic_link: MagicLinkSurfacing | None = None,
) -> OTPValue | None:
"""Resolve the OTP value to use for a verification step.
@@ -940,6 +952,7 @@ async def resolve_otp_value(
max_wait_seconds=max_wait_seconds,
poll_started_at=poll_started_at,
multi_field_expected_digits=multi_field_expected_digits,
+ surface_magic_link=surface_magic_link,
)
_warn_if_scope_suppresses_legacy_credential(task, expected_otp_type, allowed_credential_parameter_keys)
@@ -963,6 +976,8 @@ async def poll_otp_value(
poll_started_at: datetime | None = None,
rejected_code_hash: str | None = None,
multi_field_expected_digits: int | None = None,
+ *,
+ surface_magic_link: MagicLinkSurfacing | None = None,
) -> OTPValue | None:
"""Poll until an OTP of ``expected_otp_type`` arrives or the wall-clock budget expires.
@@ -1085,6 +1100,7 @@ async def poll_otp_value(
raw_context=raw_otp_context,
rejected_code_hash=rejected_code_hash,
multi_field_expected_digits=multi_field_expected_digits,
+ surface_magic_link=surface_magic_link,
)
except FailedToGetTOTPVerificationCode as e:
consecutive_failures += 1
@@ -1250,6 +1266,27 @@ async def _get_otp_value_from_email(
)
+def _surfaceable_link(
+ row: TOTPCode,
+ surfacing: MagicLinkSurfacing | None,
+ *,
+ task_id: str | None,
+ workflow_run_id: str | None,
+ expected_otp_type: OTPType | None,
+) -> bool:
+ if surfacing is None or expected_otp_type != OTPType.TOTP or row.otp_type != OTPType.MAGIC_LINK:
+ return False
+ # Unscoped rows are never handed back: an identifier shared across runs carries other sites' links.
+ bound_to_execution = (row.workflow_run_id is not None and row.workflow_run_id == workflow_run_id) or (
+ row.task_id is not None and row.task_id == task_id
+ )
+ if not bound_to_execution:
+ return False
+ anchor = to_naive_utc(surfacing.created_after)
+ created_at = to_naive_utc(row.created_at)
+ return anchor is None or (created_at is not None and created_at >= anchor)
+
+
async def _get_otp_value_from_db(
organization_id: str,
totp_identifier: str,
@@ -1261,6 +1298,8 @@ async def _get_otp_value_from_db(
raw_context: RawOTPVerificationContext | None = None,
rejected_code_hash: str | None = None,
multi_field_expected_digits: int | None = None,
+ *,
+ surface_magic_link: MagicLinkSurfacing | None = None,
) -> OTPValue | None:
# Email/SMS deliveries can arrive through /v1/credentials/totp without run
# scope, so include both exact run matches and unscoped rows in SQL.
@@ -1296,6 +1335,7 @@ async def _get_otp_value_from_db(
]
candidates.sort(key=lambda candidate: candidate[0].created_at, reverse=True)
attempts = 0
+ surfacing = surface_magic_link
for row, is_raw in candidates:
if row.workflow_run_id and workflow_run_id and row.workflow_run_id != workflow_run_id:
continue
@@ -1305,6 +1345,7 @@ async def _get_otp_value_from_db(
continue
if row.expired_at and row.expired_at < datetime.utcnow():
continue
+ link_to_surface: OTPValue | None = None
if not is_raw:
parsed_row = row
stored_otp_value = _exclude_rejected_otp(
@@ -1318,6 +1359,20 @@ async def _get_otp_value_from_db(
if expected_otp_type is None or stored_otp_value.get_otp_type() == expected_otp_type:
return stored_otp_value
context.observed_otp_types.add(stored_otp_value.get_otp_type())
+ if surfacing is not None and parsed_row.code in surfacing.spent_values:
+ # Every link older than one the caller already opened or passed over is stale: a resend replaced it.
+ surfacing = None
+ if _surfaceable_link(
+ parsed_row,
+ surfacing,
+ task_id=task_id,
+ workflow_run_id=workflow_run_id,
+ expected_otp_type=expected_otp_type,
+ ):
+ link_to_surface = stored_otp_value
+ # A bare link has no code in it to find, so the reparse below could only miss.
+ if not parsed_row.content or looks_like_magic_link(parsed_row.content):
+ return link_to_surface
if not parsed_row.content:
continue
if expected_otp_type is None:
@@ -1333,7 +1388,8 @@ async def _get_otp_value_from_db(
try:
otp_value = await parse_otp_login(row.content, organization_id, enforced_otp_type=expected_otp_type)
except InsufficientCreditsForOTPParse:
- return None
+ # Unlike a transient failure, a later tick cannot parse this row either, so its link is the best answer.
+ return link_to_surface
except Exception as e:
LOG.warning(
"Raw OTP reparse failed" if is_raw else "Parsed OTP content reparse failed",
@@ -1349,10 +1405,14 @@ async def _get_otp_value_from_db(
context.misses.add(cache_key)
if otp_value is not None:
context.observed_otp_types.add(otp_value.get_otp_type())
+ if link_to_surface is not None:
+ return link_to_surface
continue
otp_value = _exclude_rejected_otp(otp_value, rejected_code_hash, multi_field_expected_digits, task_id=task_id)
if otp_value is None:
context.misses.add(cache_key)
+ if link_to_surface is not None:
+ return link_to_surface
continue
if is_raw:
await app.DATABASE.otp.promote_raw_otp_code(
diff --git a/skyvern/webeye/actions/handler.py b/skyvern/webeye/actions/handler.py
index b8006e51b..4455ac9d8 100644
--- a/skyvern/webeye/actions/handler.py
+++ b/skyvern/webeye/actions/handler.py
@@ -6251,9 +6251,10 @@ class ActionHandler:
)
actions_result.append(ActionFailure(e))
except MultipleElementsFound as e:
- LOG.exception(
+ LOG.warning(
"Cannot handle multiple elements with the same selector in one action.",
action=action,
+ exc_info=True,
)
actions_result.append(ActionFailure(e))
except LLMProviderError as e:
diff --git a/tests/unit/test_otp_service.py b/tests/unit/test_otp_service.py
index 3a6406d68..fd2c92693 100644
--- a/tests/unit/test_otp_service.py
+++ b/tests/unit/test_otp_service.py
@@ -23,6 +23,7 @@ from skyvern.forge.sdk.workflow.models.parameter import (
)
from skyvern.services import otp_service
from skyvern.services.otp_service import (
+ MagicLinkSurfacing,
OTPValue,
_clean_url,
_get_otp_value_from_db,
@@ -2487,6 +2488,7 @@ def _parsed_otp_row(
otp_type: OTPType,
created_at: datetime = datetime(2026, 7, 30, 12, 0, 0),
workflow_run_id: str | None = None,
+ task_id: str | None = None,
) -> SimpleNamespace:
return SimpleNamespace(
totp_code_id=totp_code_id,
@@ -2496,7 +2498,7 @@ def _parsed_otp_row(
created_at=created_at,
workflow_run_id=workflow_run_id,
workflow_id=None,
- task_id=None,
+ task_id=task_id,
expired_at=None,
)
@@ -2727,6 +2729,203 @@ async def test_get_otp_value_from_db_retries_unchecked_newer_content_after_attem
assert mock_app.DATABASE.otp.get_raw_otp_codes.await_count == 2
+_POLL_STARTED = datetime(2026, 7, 30, 12, 0, 0)
+_STORED_LINK = "https://example.test/careers/activate/synthetictoken4567/"
+_RESENT_LINK = "https://example.test/careers/activate/synthetictoken8901/"
+# A bare link the store cleaned: an HTML-escaped query separator decoded, a trailing period dropped.
+_ESCAPED_LINK_CONTENT = "https://example.test/careers/activate?u=alex&amp;t=synthetictoken4567."
+_CLEANED_LINK = "https://example.test/careers/activate?u=alex&t=synthetictoken4567"
+_SURFACE_LINKS = MagicLinkSurfacing(created_after=_POLL_STARTED, spent_values=frozenset())
+# 07:00 at UTC-5 is 12:00 UTC: an aware anchor must be compared in UTC, not by its wall-clock digits.
+_UTC_MINUS_5 = timezone(timedelta(hours=-5))
+
+
+def _stored_link(
+ *,
+ seconds: int = 5,
+ link: str = _STORED_LINK,
+ content: str | None = None,
+ workflow_run_id: str | None = "wr_test",
+ task_id: str | None = None,
+) -> SimpleNamespace:
+ return _parsed_otp_row(
+ totp_code_id=f"otp_link_{seconds}",
+ content=link if content is None else content,
+ code=link,
+ otp_type=OTPType.MAGIC_LINK,
+ created_at=_POLL_STARTED + timedelta(seconds=seconds),
+ workflow_run_id=workflow_run_id,
+ task_id=task_id,
+ )
+
+
+def _stored_code(code: str, *, seconds: int) -> SimpleNamespace:
+ return _parsed_otp_row(
+ totp_code_id=f"otp_{code}",
+ content=code,
+ code=code,
+ otp_type=OTPType.TOTP,
+ created_at=_POLL_STARTED + timedelta(seconds=seconds),
+ workflow_run_id="wr_test",
+ )
+
+
+_LINK = OTPValue(value=_STORED_LINK, type=OTPType.MAGIC_LINK)
+_CODE = OTPValue(value="482913", type=OTPType.TOTP)
+_NO_PARSE_CREDIT = otp_service.InsufficientCreditsForOTPParse()
+
+
+ ("rows", "reparsed", "surfacing", "rejected_code", "expected"),
+ [
+ pytest.param([_stored_link()], None, None, None, None, id="off_unless_the_caller_opts_in"),
+ pytest.param([_stored_link()], _NO_PARSE_CREDIT, _SURFACE_LINKS, None, _LINK, id="bare_link_needs_no_parse"),
+ pytest.param(
+ [_stored_link(workflow_run_id=None, task_id="tsk_test")],
+ _NO_PARSE_CREDIT,
+ _SURFACE_LINKS,
+ None,
+ _LINK,
+ id="bare_task_scoped_link",
+ ),
+ pytest.param(
+ [_stored_link(seconds=5), _stored_code("482913", seconds=10)],
+ None,
+ _SURFACE_LINKS,
+ None,
+ _CODE,
+ id="newer_code_beats_older_link",
+ ),
+ pytest.param(
+ [_stored_code("48291", seconds=5), _stored_link(seconds=6)],
+ None,
+ _SURFACE_LINKS,
+ None,
+ _LINK,
+ id="link_one_second_after_a_short_value_wins",
+ ),
+ pytest.param(
+ [_stored_code("482913", seconds=5), _stored_link(seconds=10)],
+ None,
+ MagicLinkSurfacing(created_after=_POLL_STARTED, spent_values=frozenset({_STORED_LINK})),
+ None,
+ _CODE,
+ id="spent_link_falls_back_to_older_code",
+ ),
+ pytest.param(
+ [_stored_link(seconds=5), _stored_link(seconds=60, link=_RESENT_LINK)],
+ None,
+ MagicLinkSurfacing(created_after=_POLL_STARTED, spent_values=frozenset({_RESENT_LINK})),
+ None,
+ None,
+ id="spent_link_retires_the_older_link_it_replaced",
+ ),
+ pytest.param(
+ [_stored_link(link=_CLEANED_LINK, content=_ESCAPED_LINK_CONTENT)],
+ _NO_PARSE_CREDIT,
+ _SURFACE_LINKS,
+ None,
+ OTPValue(value=_CLEANED_LINK, type=OTPType.MAGIC_LINK),
+ id="bare_link_the_store_cleaned_needs_no_parse",
+ ),
+ pytest.param(
+ [_stored_link(content=f"Hi Alex Demo, your code is 482913. Or confirm here: {_STORED_LINK}")],
+ _CODE,
+ _SURFACE_LINKS,
+ None,
+ _CODE,
+ id="email_with_a_code_and_a_link_yields_the_code",
+ ),
+ pytest.param(
+ [
+ _stored_link(
+ content=f"Hi Alex Demo, confirm your account: {_STORED_LINK} Help: https://example.test/help"
+ )
+ ],
+ None,
+ _SURFACE_LINKS,
+ None,
+ _LINK,
+ id="email_with_only_a_link_yields_the_link",
+ ),
+ pytest.param(
+ [_stored_link(content=f"Hi Alex Demo, your code is 482913. Or confirm here: {_STORED_LINK}")],
+ _CODE,
+ _SURFACE_LINKS,
+ "482913",
+ _LINK,
+ id="email_whose_code_was_rejected_yields_the_link",
+ ),
+ pytest.param(
+ [_stored_link(content=f"Hi Alex Demo, your code is 482913. Or confirm here: {_STORED_LINK}")],
+ _NO_PARSE_CREDIT,
+ _SURFACE_LINKS,
+ None,
+ _LINK,
+ id="email_with_a_code_and_a_link_yields_the_link_without_parse_credit",
+ ),
+ pytest.param(
+ [_stored_link(content=f"Hi Alex Demo, your code is 482913. Or confirm here: {_STORED_LINK}")],
+ RuntimeError("transient parser failure"),
+ _SURFACE_LINKS,
+ None,
+ None,
+ id="email_with_a_code_and_a_link_retries_a_transient_parse_failure",
+ ),
+ pytest.param(
+ [_stored_link(seconds=5)],
+ None,
+ MagicLinkSurfacing(
+ created_after=datetime(2026, 7, 30, 7, 0, 0, tzinfo=_UTC_MINUS_5), spent_values=frozenset()
+ ),
+ None,
+ _LINK,
+ id="aware_anchor_before_the_link",
+ ),
+ pytest.param(
+ [_stored_link(seconds=5)],
+ None,
+ MagicLinkSurfacing(
+ created_after=datetime(2026, 7, 30, 7, 0, 10, tzinfo=_UTC_MINUS_5), spent_values=frozenset()
+ ),
+ None,
+ None,
+ id="aware_anchor_after_the_link",
+ ),
+ ],
+)
+async def test_code_poll_hands_back_only_a_fresh_unspent_link_bound_to_this_run(
+ rows: list[SimpleNamespace],
+ reparsed: OTPValue | Exception | None,
+ surfacing: MagicLinkSurfacing | None,
+ rejected_code: str | None,
+ expected: OTPValue | None,
+) -> None:
+ # A caller that can open links opts in to receiving a stored link while it waits for a code; every
+ # other caller keeps the old answer. The newest usable value of either type wins, and an email holding
+ # both a code and a link still yields the code.
+ parse = AsyncMock(side_effect=reparsed) if isinstance(reparsed, Exception) else AsyncMock(return_value=reparsed)
+ with (
+ patch("skyvern.services.otp_service.app") as mock_app,
+ patch("skyvern.services.otp_service.parse_otp_login", new=parse),
+ ):
+ mock_app.DATABASE.otp.get_otp_codes = AsyncMock(return_value=rows)
+ mock_app.DATABASE.otp.get_raw_otp_codes = AsyncMock(return_value=[])
+ result = await _get_otp_value_from_db(
+ "o_test",
+ "application_synthetic0001",
+ task_id="tsk_test",
+ workflow_run_id="wr_test",
+ expected_otp_type=OTPType.TOTP,
+ raw_context=otp_service.RawOTPVerificationContext(),
+ rejected_code_hash=hashlib.sha256(rejected_code.encode()).hexdigest() if rejected_code else None,
+ surface_magic_link=surfacing,
+ )
+
+ assert result == expected
+
+
def _raw_otp_row(
totp_code_id: str = "otp_raw",
task_id: str | None = None,
diff --git a/tests/unit/test_taskv3_auth_tools.py b/tests/unit/test_taskv3_auth_tools.py
index 8e7c5ca62..e8e7ce71f 100644
--- a/tests/unit/test_taskv3_auth_tools.py
+++ b/tests/unit/test_taskv3_auth_tools.py
@@ -6,7 +6,7 @@ import asyncio
import logging
import re
import time
-from datetime import datetime, timezone
+from datetime import datetime, timedelta, timezone
from types import SimpleNamespace
from typing import Any
from unittest.mock import AsyncMock, MagicMock
@@ -26,7 +26,7 @@ from skyvern.forge.log_redaction import REDACTED
from skyvern.forge.sdk.core import skyvern_context
from skyvern.forge.sdk.core.skyvern_context import SkyvernContext
from skyvern.forge.sdk.forge_log import redact_sensitive_event_fields
-from skyvern.forge.sdk.schemas.totp_codes import OTPType
+from skyvern.forge.sdk.schemas.totp_codes import OTPType, TOTPCode
from skyvern.forge.sdk.services import credentials as credentials_module
from skyvern.forge.sdk.workflow import context_manager as cm
from skyvern.forge.sdk.workflow.context_manager import WorkflowContextManager, WorkflowRunContext
@@ -39,6 +39,8 @@ from skyvern.services.otp_service import OTPValue
from skyvern.utils.secret_redaction import redact_secrets_from_bytes
from tests.unit.scoped_asyncio import ScopedAsyncio
+_TASK_CREATED_AT = datetime(2026, 9, 30, 17, 50, 0)
+
def _task(**overrides: Any) -> SimpleNamespace:
base: dict[str, Any] = {
@@ -48,6 +50,7 @@ def _task(**overrides: Any) -> SimpleNamespace:
"totp_verification_url": None,
"totp_identifier": None,
"navigation_payload": None,
+ "created_at": _TASK_CREATED_AT,
}
base.update(overrides)
return SimpleNamespace(**base)
@@ -1700,6 +1703,177 @@ async def test_link_tool_hands_a_verification_code_back_to_the_code_tool(monkeyp
assert resolver.await_count == 1
+_RUN_ID = "wr_link"
+_STORED_IDENTIFIER = "application_synthetic0001"
+_ACTIVATION_LINK = "https://example.test/careers/activate/synthetictoken4567/"
+_RESENT_ACTIVATION_LINK = "https://example.test/careers/activate/synthetictoken8901/"
+# A slice that has to time out; one that delivers returns on the first tick of the default 5 s slice.
+_TIMEOUT_SLICE_SECONDS = 0.3
+
+
+def _stored_row(
+ totp_code_id: str,
+ code: str,
+ otp_type: OTPType,
+ created_at: datetime,
+ *,
+ workflow_run_id: str | None = _RUN_ID,
+) -> TOTPCode:
+ return TOTPCode(
+ totp_code_id=totp_code_id,
+ totp_identifier=_STORED_IDENTIFIER,
+ organization_id="o_1",
+ content=code,
+ code=code,
+ otp_type=otp_type,
+ workflow_run_id=workflow_run_id,
+ created_at=created_at,
+ modified_at=created_at,
+ )
+
+
+def _stored_code_tools(
+ monkeypatch: pytest.MonkeyPatch, rows: list[TOTPCode], page: _FakePage, *, page_offered: bool = True
+) -> dict[str, Any]:
+ """Both tools over a workflow-run task whose only source is the stored-code table, read through the real
+ resolve_otp_value -> poll_otp_value -> _get_otp_value_from_db chain."""
+
+ async def _get_otp_codes(**_kwargs: Any) -> list[TOTPCode]:
+ return list(rows)
+
+ async def _brief_sleep(_seconds: float) -> None:
+ await asyncio.sleep(0.01)
+
+ run = SimpleNamespace(
+ workflow_id="w_1", workflow_permanent_id="wpid_1", started_at=_TASK_CREATED_AT - timedelta(minutes=10)
+ )
+ monkeypatch.setattr(otp_service.app, "WORKFLOW_CONTEXT_MANAGER", WorkflowContextManager())
+ monkeypatch.setattr(
+ otp_service.app,
+ "DATABASE",
+ SimpleNamespace(
+ otp=SimpleNamespace(get_otp_codes=_get_otp_codes, get_raw_otp_codes=AsyncMock(return_value=[])),
+ workflow_runs=SimpleNamespace(get_workflow_run=AsyncMock(return_value=run)),
+ ),
+ )
+ monkeypatch.setattr(otp_service, "_get_otp_value_from_email", AsyncMock(return_value=None))
+ # The LLM seam: neither a bare link nor these rows' content holds a code.
+ monkeypatch.setattr(otp_service, "parse_otp_login", AsyncMock(return_value=None))
+ monkeypatch.setattr(otp_service, "asyncio", ScopedAsyncio(sleep=_brief_sleep))
+ monkeypatch.setattr(auth_tools, "_PER_CALL_WAIT_SECONDS", 5.0)
+ task = _task(workflow_run_id=_RUN_ID, totp_identifier=_STORED_IDENTIFIER)
+ tools, _ = auth_tools.build_auth_tools(task, _provider(page) if page_offered else None)
+ return {t.name: t.handler for t in tools}
+
+
+async def test_code_tool_hands_a_stored_run_scoped_sign_in_link_to_the_link_tool_once(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ # A site that verifies by link while the model asks for a code: the newest stored link for this run must
+ # send the model to the link tool instead of waiting out the code budget. Once opened, neither it nor the
+ # older link its resend replaced may be handed back, since the page may then ask for a real code.
+ page = _FakePage()
+ rows = [
+ _stored_row("otp_link", _ACTIVATION_LINK, OTPType.MAGIC_LINK, _TASK_CREATED_AT + timedelta(seconds=40)),
+ _stored_row(
+ "otp_resent", _RESENT_ACTIVATION_LINK, OTPType.MAGIC_LINK, _TASK_CREATED_AT + timedelta(seconds=90)
+ ),
+ ]
+ handlers = _stored_code_tools(monkeypatch, rows, page)
+ skyvern_context.set(SkyvernContext(task_id="tsk_1"))
+ try:
+ redirect = await handlers["get_verification_code"]({})
+ opened = await handlers["open_verification_link"]({})
+ monkeypatch.setattr(auth_tools, "_PER_CALL_WAIT_SECONDS", _TIMEOUT_SLICE_SECONDS)
+ after_open = await handlers["get_verification_code"]({})
+ finally:
+ skyvern_context.reset()
+
+ assert redirect.status == "error" and redirect.content == auth_tools._MAGIC_LINK_REDIRECT
+ assert opened.status == "ok"
+ assert "available yet" in after_open.content
+ assert page.goto_calls == [_RESENT_ACTIVATION_LINK]
+
+
+async def test_code_tool_called_again_instead_of_opening_the_link_polls_for_a_code(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ # A model sent to the link tool that asks for a code again may be on a page that wants one (a site that
+ # mails a code and a link): it must get the stored code, not the same redirect on every call.
+ page = _FakePage()
+ rows = [
+ _stored_row("otp_code", "482913", OTPType.TOTP, _TASK_CREATED_AT + timedelta(seconds=30)),
+ _stored_row("otp_link", _ACTIVATION_LINK, OTPType.MAGIC_LINK, _TASK_CREATED_AT + timedelta(seconds=40)),
+ ]
+ handlers = _stored_code_tools(monkeypatch, rows, page)
+ skyvern_context.set(SkyvernContext(task_id="tsk_1"))
+ try:
+ redirect = await handlers["get_verification_code"]({})
+ repeat = await handlers["get_verification_code"]({})
+ finally:
+ skyvern_context.reset()
+
+ assert redirect.content == auth_tools._MAGIC_LINK_REDIRECT
+ assert repeat.content == "verification_code: 482913"
+ assert page.goto_calls == []
+
+
+ ("rows", "page_offered", "expected"),
+ [
+ pytest.param(
+ [
+ _stored_row(
+ "otp_link",
+ "https://other.example.test/activate/synthetictoken8901/",
+ OTPType.MAGIC_LINK,
+ _TASK_CREATED_AT + timedelta(seconds=60),
+ workflow_run_id=None,
+ ),
+ _stored_row("otp_code", "482913", OTPType.TOTP, _TASK_CREATED_AT + timedelta(seconds=30)),
+ ],
+ True,
+ "verification_code: 482913",
+ id="unscoped_link_on_a_shared_identifier",
+ ),
+ pytest.param(
+ [_stored_row("otp_link", _ACTIVATION_LINK, OTPType.MAGIC_LINK, _TASK_CREATED_AT - timedelta(seconds=60))],
+ True,
+ "available yet",
+ id="run_scoped_link_from_before_this_task",
+ ),
+ pytest.param(
+ [_stored_row("otp_link", _ACTIVATION_LINK, OTPType.MAGIC_LINK, _TASK_CREATED_AT + timedelta(seconds=40))],
+ False,
+ "available yet",
+ id="page_free_run_cannot_open_a_link",
+ ),
+ ],
+)
+async def test_code_tool_never_redirects_to_a_stored_link_this_task_must_not_follow(
+ monkeypatch: pytest.MonkeyPatch, rows: list[TOTPCode], page_offered: bool, expected: str
+) -> None:
+ # An identifier shared across runs stores other sites' links unscoped, an earlier block's link is not
+ # this page's, and a page-free run has no link tool: none of them may turn a code wait into a redirect
+ # or a terminal "unsupported link", and a code the page asked for is still delivered.
+ page = _FakePage()
+ handlers = _stored_code_tools(monkeypatch, rows, page, page_offered=page_offered)
+ if expected == "available yet":
+ monkeypatch.setattr(auth_tools, "_PER_CALL_WAIT_SECONDS", _TIMEOUT_SLICE_SECONDS)
+ skyvern_context.set(SkyvernContext(task_id="tsk_1"))
+ try:
+ result = await handlers["get_verification_code"]({})
+ finally:
+ skyvern_context.reset()
+
+ assert expected in result.content
+ assert result.content != auth_tools._MAGIC_LINK_REDIRECT
+ assert page.goto_calls == []
+
+
@pytest.mark.asyncio
async def test_verification_tools_share_one_polling_budget(monkeypatch: pytest.MonkeyPatch) -> None:
# Two tools over one verification source must not double the wait a never-answering source can buy.