Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
Stop re-entering an externally supplied one-time code; generated TOTP unchanged (SKY-18619) (#9026)
LawyZhengb872da0c7f90
diff --git a/skyvern/errors/errors.py b/skyvern/errors/errors.pyindex bd26c17b5..de0b64afb 100644--- a/skyvern/errors/errors.py+++ b/skyvern/errors/errors.py@@ -141,6 +141,14 @@ class TOTPExpiredError(SkyvernDefinedError):reasoning: str = "TOTP verification code has expired during multi-field input sequence."+class OTPOutcomeUnconfirmedError(SkyvernDefinedError):+ error_code: str = "OTP_OUTCOME_UNCONFIRMED"+ reasoning: str = (+ "The one-time code was entered, but the site did not confirm whether it was accepted before the page load "+ "timeout. The same code was not entered again."+ )++class MissingTOTPSourceError(SkyvernDefinedError):error_code: str = "MISSING_TOTP_SOURCE"reasoning: str = (diff --git a/skyvern/forge/agent.py b/skyvern/forge/agent.pyindex 417b9d217..77ba7c49f 100644--- a/skyvern/forge/agent.py+++ b/skyvern/forge/agent.py@@ -43,6 +43,7 @@ from skyvern.constants import (from skyvern.errors.errors import (GetTOTPVerificationCodeError,MissingTOTPSourceError,+ OTPOutcomeUnconfirmedError,ReachMaxRetriesError,ReachMaxStepsError,SkyvernDefinedError,@@ -151,6 +152,7 @@ from skyvern.forge.sdk.core import skyvern_contextfrom skyvern.forge.sdk.core.hashing import diagnostic_fingerprintfrom skyvern.forge.sdk.core.security import generate_skyvern_webhook_signaturefrom skyvern.forge.sdk.core.skyvern_context import (+ ExternalOTPUse,MultiFieldTotpAttempt,MultiFieldTotpRejection,SkyvernContext,@@ -302,6 +304,7 @@ from skyvern.webeye.actions.actions import (HoverAction,InputTextAction,KeypressAction,+ NullAction,ReloadPageAction,SelectOption,SelectOptionAction,@@ -313,8 +316,11 @@ from skyvern.webeye.actions.actions import (from skyvern.webeye.actions.handler import (ActionHandler,_fill_multi_field_totp_group,+ _refresh_stale_web_action_before_dispatch,_register_runtime_otp_value_best_effort,_resolve_multi_field_totp_code,+ otp_dispatch_effect,+ otp_single_dispatch,)from skyvern.webeye.actions.handler_utils import should_stop_batch_after_dropdown_selectfrom skyvern.webeye.actions.models import DetailedAgentStepOutput@@ -329,6 +335,7 @@ from skyvern.webeye.actions.multi_field_totp import (clear_multi_field_totp_boxes,multi_field_totp_candidate_input_count,multi_field_totp_group_identity,+ one_time_code_dispatch_kind,parse_multi_field_totp_rejection,submit_multi_field_totp_retry,)@@ -961,6 +968,244 @@ def _mask_multi_field_totp_rejection_digits(value: str | None) -> str:return re.sub(r"[0-9]", "*", value or "")+_OTP_RESEND_UNANSWERED_REASON = (+ "A new one-time code was requested, but no new code arrived before the page load timeout. "+ "The earlier code was not entered again."+)+++def _external_otp_use(context: SkyvernContext, task_id: str) -> ExternalOTPUse | None:+ """The task's entered external codes, unless the current code group takes a seed-generated TOTP."""+ use = context.external_otp_uses.get(task_id)+ attempt = context.multi_field_totp.get(task_id)+ return None if use is None or (attempt is not None and attempt.code_source != "external") else use+++async def _external_otp_action_kind(+ page: Page, scraped_page: ScrapedPage, use: ExternalOTPUse, action: Action+) -> Literal["submit", "resend"] | None:+ """Whether the action is a click or Enter on the code group's own submit or resend control."""+ if not isinstance(action, (ClickAction, KeypressAction)):+ return None+ try:+ return await one_time_code_dispatch_kind(+ page,+ scraped_page,+ use.expected_digits,+ element_id=action.element_id if isinstance(action, ClickAction) else None,+ keys=action.keys if isinstance(action, KeypressAction) else None,+ )+ except Exception as exc:+ # A control that cannot be tied to the group keeps the ordinary click semantics.+ LOG.debug("Unable to tie an action to the one-time code group", error_type=type(exc).__name__)+ return None+++async def _guard_external_otp_dispatch(+ context: SkyvernContext, task_id: str, page: Page, scraped_page: ScrapedPage, action: Action+) -> tuple[Literal["submit", "resend"] | None, ActionFailure | None]:+ """A press of the group's submit control runs once per entered code; its resend control once per press."""+ use = _external_otp_use(context, task_id)+ kind = await _external_otp_action_kind(page, scraped_page, use, action) if use is not None else None+ if use is None or kind is None or (kind == "submit" and use.current_code_hash is None):+ return None, None+ assert isinstance(action, (ClickAction, KeypressAction))+ if kind == "submit" and use.current_code_hash in use.submitted_code_hashes:+ LOG.info("Refused to press submit again for the same one-time code", task_id=task_id)+ refusal = ActionFailure(+ SkyvernException("The submit control was already pressed for this one-time code."),+ stop_execution_on_failure=False,+ )+ return kind, refusal+ action.repeat = 1+ if kind == "submit" and use.current_code_hash is not None:+ # Recorded before the press and undone only if the press is known not to have reached the page.+ use.submitted_code_hashes = use.submitted_code_hashes | {use.current_code_hash}+ return kind, None+++def _note_external_otp_dispatch(+ context: SkyvernContext,+ task_id: str,+ kind: Literal["submit", "resend"],+ results: list[ActionResult],+ dispatched_at: datetime,+) -> None:+ use = context.external_otp_uses.get(task_id)+ if use is None:+ return+ effect = otp_dispatch_effect.get()+ if kind == "submit" and effect is None and not (results and results[-1].success) and use.current_code_hash:+ use.submitted_code_hashes = use.submitted_code_hashes - {use.current_code_hash}+ elif kind == "resend" and effect == "dispatched":+ # The press reached the page, even if the action then failed or was cancelled: one fetch is authorized.+ use.resend_requested_at = dispatched_at+ use.resend_attempted = True+ context.pop_totp_code(task_id)+ context.totp_codes.pop(f"{task_id}_totp_cache", None)+ LOG.info("Requested a new one-time code", task_id=task_id)+++def _external_otp_terminal_plan(+ task: Task, step: Step, json_response: dict[str, Any], use: ExternalOTPUse+) -> "ReplacedOTPPlan":+ errors = (+ [OTPOutcomeUnconfirmedError(reasoning=use.terminal_reasoning).to_user_defined_error()]+ if use.terminal_error_code and use.terminal_reasoning+ else []+ )+ return ReplacedOTPPlan(+ json_response,+ [+ TerminateAction(+ reasoning=use.terminal_reasoning,+ intention=use.terminal_reasoning,+ errors=errors,+ organization_id=task.organization_id,+ workflow_run_id=task.workflow_run_id,+ task_id=task.task_id,+ step_id=step.step_id,+ step_order=step.order,+ action_order=0,+ )+ ],+ )+++async def _fetch_reissued_external_otp(+ task: Task, context: SkyvernContext, use: ExternalOTPUse, requested_at: datetime+) -> bool:+ """The one bounded fetch a dispatched resend authorizes. It does not prove the site sent a code: it only+ accepts a code not entered before, delivered after the press. A same-digit new code cannot be told apart."""+ if task.organization_id is None or not (task.totp_verification_url or task.totp_identifier):+ return False+ workflow_run = (+ await app.DATABASE.workflow_runs.get_workflow_run(task.workflow_run_id) if task.workflow_run_id else None+ )+ try:+ otp_value = await poll_otp_value(+ organization_id=task.organization_id,+ task_id=task.task_id,+ workflow_id=workflow_run.workflow_id if workflow_run else None,+ workflow_run_id=task.workflow_run_id,+ workflow_permanent_id=workflow_run.workflow_permanent_id if workflow_run else None,+ totp_verification_url=task.totp_verification_url,+ totp_identifier=task.totp_identifier,+ created_after=requested_at,+ expected_otp_type=OTPType.TOTP,+ max_wait_seconds=settings.BROWSER_LOADING_TIMEOUT_MS / 1000,+ rejected_code_hash=use.current_code_hash,+ multi_field_expected_digits=use.expected_digits,+ )+ except (NoTOTPVerificationCodeFound, FailedToGetTOTPVerificationCode):+ otp_value = None+ if (+ otp_value is None+ or otp_value.from_credential_seed+ or context.external_otp_code_used(task.task_id, otp_value.value)+ ):+ LOG.info("No new one-time code arrived after the resend", task_id=task.task_id)+ return False+ context.register_secret_value(otp_value.value)+ attempt = context.multi_field_totp.get(task.task_id)+ if attempt is not None:+ context.reset_attempt(task.task_id)+ attempt.external_code_obtained_at = time.time()+ context.totp_codes[f"{task.task_id}_totp_cache"] = otp_value.value+ LOG.info("Accepted a new one-time code after the resend", task_id=task.task_id)+ return True+++async def _plan_has_work_besides_reentry(+ page: Page,+ scraped_page: ScrapedPage,+ use: ExternalOTPUse,+ attempt: MultiFieldTotpAttempt | None,+ actions: list[Action],+) -> bool:+ """Whether the plan does anything besides typing an entered code again or pressing submit again for it."""+ reentry = _multi_field_totp_action_indices(actions, attempt, allow_partial=True) if attempt is not None else set()+ for index, action in enumerate(actions):+ if index in reentry or action.action_type in (ActionType.WAIT, ActionType.NULL_ACTION):+ continue+ if isinstance(action, InputTextAction) and attempt is not None and action.text == attempt.hint_code:+ continue+ if (+ use.current_code_hash in use.submitted_code_hashes+ and await _external_otp_action_kind(page, scraped_page, use, action) == "submit"+ ):+ continue+ return True+ return False+++async def _external_otp_reuse_plan(+ agent: "ForgeAgent",+ task: Task,+ step: Step,+ scraped_page: ScrapedPage,+ browser_state: BrowserState,+ json_response: dict[str, Any],+) -> "ReplacedOTPPlan | None":+ """When the plan asks for a code and only an entered one is available, never fetch or type it again.++ The plan's other actions still run. If repeating the code is all it can do, the step waits within the+ entry's page-load budget; then the normal completion check decides between completed and unconfirmed.+ """+ context = skyvern_context.current()+ use = _external_otp_use(context, task.task_id) if context else None+ if context is None or use is None:+ return None+ if use.terminal_reasoning is not None:+ return _external_otp_terminal_plan(task, step, json_response, use)+ if use.resend_requested_at is not None:+ requested_at, use.resend_requested_at = use.resend_requested_at, None+ await _fetch_reissued_external_otp(task, context, use, requested_at)+ cached = context.totp_codes.get(f"{task.task_id}_totp_cache")+ if not json_response.get("place_to_enter_verification_code") or (+ cached and not context.external_otp_code_used(task.task_id, cached)+ ):+ return None+ page = await browser_state.must_get_working_page()+ actions = parse_actions(task, step.step_id, step.order, scraped_page, _require_actions_payload(json_response))+ stamp_parsed_actions(actions)+ attempt = context.multi_field_totp.get(task.task_id)+ if await _plan_has_work_besides_reentry(page, scraped_page, use, attempt, actions):+ return ReplacedOTPPlan(json_response, actions)+ if use.deadline is not None and time.monotonic() < use.deadline:+ return ReplacedOTPPlan(+ json_response,+ [+ NullAction(+ reasoning="The one-time code was already entered; waiting for the page to respond.",+ organization_id=task.organization_id,+ workflow_run_id=task.workflow_run_id,+ task_id=task.task_id,+ step_id=step.step_id,+ step_order=step.order,+ action_order=0,+ )+ ],+ )+ completion = await agent.check_user_goal_complete(+ page=page, scraped_page=scraped_page, task=task, step=step, verification_trigger="periodic_after_step"+ )+ if isinstance(completion, CompleteAction):+ completion.organization_id = task.organization_id+ completion.workflow_run_id = task.workflow_run_id+ completion.task_id = task.task_id+ completion.step_id = step.step_id+ completion.step_order = step.order+ completion.action_order = 0+ return ReplacedOTPPlan(json_response, [completion])+ use.terminal_error_code = OTPOutcomeUnconfirmedError.model_fields["error_code"].default+ use.terminal_reasoning = (+ _OTP_RESEND_UNANSWERED_REASON if use.resend_attempted else OTPOutcomeUnconfirmedError().reasoning+ )+ LOG.info("One-time code outcome unconfirmed", task_id=task.task_id, step_id=step.step_id)+ return _external_otp_terminal_plan(task, step, json_response, use)++def _planner_confirmed_verification_code_intent(json_response: dict[str, Any]) -> bool:return (json_response.get("place_to_enter_verification_code") is True@@ -5687,17 +5932,42 @@ class ForgeAgent:action.skip_auto_complete_tab = Trueaction.stop_batch_after_dropdown_select = should_stop_batch_after_dropdown_select(next_action)- results = await ActionHandler.handle_action(- scraped_page=totp_dispatch_page if totp_armed else scraped_page,- task=task,- step=step,- page=current_page,- action=action,- file_download_false_click_eligible=file_download_false_click_eligible,- # Only actions after the first can be stale: an earlier action in this same batch- # may have remounted/reflowed this one's target away from the shared pre-batch scrape.- allow_stale_refresh=action_idx > 0,- )+ dispatch_page = totp_dispatch_page if totp_armed else scraped_page+ if action_idx > 0 and not totp_armed and _external_otp_use(context, task.task_id) is not None:+ # The handler's own stale remap would run after the guard below and hand it a remounted Verify or+ # Resend it cannot classify; remap here so the guard and the click see the same control.+ refreshed = await _refresh_stale_web_action_before_dispatch(dispatch_page, current_page, action)+ if refreshed is not None:+ dispatch_page, action = refreshed+ otp_dispatch, otp_refusal = await _guard_external_otp_dispatch(+ context, task.task_id, current_page, dispatch_page, action+ )+ dispatched_at = naive_utc_now()+ effect_token = otp_dispatch_effect.set(None)+ single_token = otp_single_dispatch.set(otp_dispatch is not None)+ otp_results: list[ActionResult] = []+ try:+ if otp_refusal is not None:+ results = [otp_refusal]+ else:+ results = await ActionHandler.handle_action(+ scraped_page=dispatch_page,+ task=task,+ step=step,+ page=current_page,+ action=action,+ file_download_false_click_eligible=file_download_false_click_eligible,+ # Only actions after the first can be stale: an earlier action in this same batch+ # may have remounted/reflowed this one's target away from the shared pre-batch scrape.+ allow_stale_refresh=action_idx > 0,+ )+ otp_results = results+ finally:+ # Before the context is restored, so a press that reached the page is kept even on cancellation.+ if otp_dispatch is not None and otp_refusal is None:+ _note_external_otp_dispatch(context, task.task_id, otp_dispatch, otp_results, dispatched_at)+ otp_single_dispatch.reset(single_token)+ otp_dispatch_effect.reset(effect_token)await app.AGENT_FUNCTION.post_action_execution(action)detailed_agent_step_output.actions_and_results[action_idx] = (action,@@ -10138,6 +10408,12 @@ class ForgeAgent:context = skyvern_context.ensure_context()previous_rejection = context.multi_field_totp_rejections.get(task.task_id)attempt = context.multi_field_totp.get(task.task_id)+ if attempt is not None and attempt.code_source == "external":+ use = context.external_otp_uses.get(task.task_id)+ if use is not None and use.terminal_reasoning is None:+ use.terminal_reasoning = reasoning+ # Another external code needs a dispatched resend; fetching one automatically would repeat the entry.+ return skipped("external_code_not_retried")if skyvern_context.multi_field_totp_retry_budget_exhausted(task.task_id):assert previous_rejection is not Noneif previous_rejection.submitted_at is None:@@ -11602,6 +11878,10 @@ class ForgeAgent:if not task.organization_id:return json_response, []+ reuse_plan = await _external_otp_reuse_plan(self, task, step, scraped_page, browser_state, json_response)+ if reuse_plan is not None:+ return reuse_plan+if not task.totp_verification_url and not task.totp_identifier:return json_response, []diff --git a/skyvern/forge/sdk/core/skyvern_context.py b/skyvern/forge/sdk/core/skyvern_context.pyindex db1eda026..4bb14b6a1 100644--- a/skyvern/forge/sdk/core/skyvern_context.py+++ b/skyvern/forge/sdk/core/skyvern_context.py@@ -6,6 +6,7 @@ import datetimeimport hashlibimport htmlimport re+import timeimport unicodedatafrom bisect import bisect_left, bisect_rightfrom contextlib import contextmanager@@ -42,6 +43,31 @@ if TYPE_CHECKING:from skyvern.webeye.actions.actions import Action+@dataclass+class ExternalOTPUse:+ """Externally delivered one-time codes already entered in this task, kept apart from ``multi_field_totp``.++ Clearing, remounts, replans, URL changes, ``reset_attempt`` and cache restores rebuild that binding; none+ of them makes an entered code enterable again. Entering, and pressing the group's submit control, are+ recorded separately; whether the site accepted a code is never inferred.+ """++ expected_digits: int+ # Fingerprints of codes whose whole value reached the group, or was sent without a confirmed read-back.+ used_code_hashes: frozenset[str] = field(default_factory=frozenset, repr=False)+ current_code_hash: str | None = field(default=None, repr=False)+ # Codes whose bound submit control or Enter was pressed (or may have been, if the press failed late).+ submitted_code_hashes: frozenset[str] = field(default_factory=frozenset, repr=False)+ # time.monotonic() limit, fixed when the current code was entered, for a step that can only repeat it.+ deadline: float | None = None+ # Start of a dispatched press on the group's resend control; authorizes one fetch.+ resend_requested_at: datetime.datetime | None = None+ resend_attempted: bool = False+ # Latched once the task is to end here, so a failed terminate action cannot reopen code entry.+ terminal_reasoning: str | None = None+ terminal_error_code: str | None = None++@dataclassclass MultiFieldTotpAttempt:box_element_ids: list[str]@@ -731,6 +757,7 @@ class SkyvernContext:field(default_factory=dict, repr=False))multi_field_totp_rejected_candidates: dict[str, set[str]] = field(default_factory=dict, repr=False)+ external_otp_uses: dict[str, ExternalOTPUse] = field(default_factory=dict)active_credential_parameter_key: str | None = Nonelog: list[dict] = field(default_factory=list)hashed_href_map: dict[str, str] = field(default_factory=dict)@@ -1124,6 +1151,23 @@ class SkyvernContext:self.multi_field_totp_mask_values.pop(task_id, None)self.multi_field_totp_prompt_values.pop(task_id, None)self.multi_field_totp_rejected_candidates.pop(task_id, None)+ self.external_otp_uses.pop(task_id, None)++ def note_external_otp_input(+ self, task_id: str, code_hash: str, expected_digits: int, budget_seconds: float+ ) -> None:+ use = self.external_otp_uses.setdefault(task_id, ExternalOTPUse(expected_digits=expected_digits))+ if use.current_code_hash != code_hash:+ use.current_code_hash = code_hash+ use.expected_digits = expected_digits+ use.deadline = time.monotonic() + budget_seconds+ use.used_code_hashes = use.used_code_hashes | {code_hash}++ def external_otp_code_used(self, task_id: str, code: str | None) -> bool:+ use = self.external_otp_uses.get(task_id)+ return (+ code is not None and use is not None and hashlib.sha256(code.encode()).hexdigest() in use.used_code_hashes+ )def clear_multi_field_totp_state(self, task_id: str, *, restore_unverified_external: bool = False) -> None:attempt = self.multi_field_totp.pop(task_id, None)@@ -1139,6 +1183,7 @@ class SkyvernContext:and attempt.filled_code_hash is not Noneand not attempt.fill_verifiedand cached_code+ and not self.external_otp_code_used(task_id, cached_code)):self.totp_codes[task_id] = cached_codediff --git a/skyvern/webeye/actions/handler.py b/skyvern/webeye/actions/handler.pyindex f71a9e84d..3702e4d91 100644--- a/skyvern/webeye/actions/handler.py+++ b/skyvern/webeye/actions/handler.py@@ -277,6 +277,7 @@ from skyvern.webeye.utils.dom import (SkyvernOptionType,bounded_lookup_call,is_element_detached_error,+ is_engine_timeout_error,is_incompatible_text_input_error,is_post_dispatch_click_timeout,resolve_locator,@@ -1654,6 +1655,12 @@ _false_click_download_eligible: ContextVar[bool] = ContextVar("false_click_downl# them. Fails toward retention -- a missed suppression stays committed, degrading to base-equivalent# retention rather than releasing a genuinely-owned claim._browser_dispatch_committed: ContextVar[bool] = ContextVar("browser_dispatch_committed", default=False)+# Set by the step executor around a click or Enter on a one-time code group's submit or resend control: the+# action may reach the page once, so no fallback runs after a click that might already have landed.+otp_single_dispatch: ContextVar[bool] = ContextVar("otp_single_dispatch", default=False)+# What such an action did to the page: "dispatched" once the click or key press returned, "unknown" from the+# first await that may reach the page until then, None when it is known not to have reached the page.+otp_dispatch_effect: ContextVar[str | None] = ContextVar("otp_dispatch_effect", default=None)def _suppress_redundant_desired_click() -> list[ActionResult]:@@ -8254,6 +8261,12 @@ def _record_multi_field_totp_fill(if verified:state.observed_max_filled = state.expected_digitsstate.filled_code_hash = hashlib.sha256(code.encode()).hexdigest()+ context = skyvern_context.current()+ if state.code_source == "external" and context is not None and context.task_id:+ # Recorded here, before any cleanup, restore or result handling, so no later failure undoes it.+ context.note_external_otp_input(+ context.task_id, state.filled_code_hash, state.expected_digits, settings.BROWSER_LOADING_TIMEOUT_MS / 1000+ )state.filled_at = time.time()state.filled_url = filled_urlstate.filled_loader_id = filled_loader_id@@ -8616,6 +8629,10 @@ async def _fill_multi_field_totp_group(result.data = {"totp_group_filled": True, "verified": False, "totp_submission_observed": True}return result+ if state.code_source == "external" and stream_completed and state.observed_max_filled == state.expected_digits:+ # Every digit already reached the boxes; typing them again box by box would enter the code twice.+ return delivered_unverified_or_failure("keyboard_stream_delivered_unconfirmed")+code_refresh_error = await refresh_code_if_expiring()if code_refresh_error is not None:strategy = "per_box_fill_code_refresh_failed"@@ -8697,6 +8714,14 @@ async def _fill_multi_field_totp_group()+def _external_otp_reentry_refused() -> ActionFailure:+ # Not stopping: the batch's other actions (a first Verify, the next page's steps) still run.+ return ActionFailure(+ SkyvernException("This one-time code was already entered and is not entered again."),+ stop_execution_on_failure=False,+ )++def _normalize_dropdown_match_text(value: str) -> str:return re.sub(r"[^a-zA-Z0-9]+", "", value).lower()@@ -9233,6 +9258,12 @@ async def _handle_input_text_action(code = await _resolve_multi_field_totp_code(task, attempt)if isinstance(code, ActionFailure):return [code]+ if (+ attempt.code_source == "external"+ and context is not None+ and context.external_otp_code_used(task.task_id, code)+ ):+ return [_external_otp_reentry_refused()]resolved_hint = codeif not action.element_id:# This is a CUA type action@@ -9298,6 +9329,9 @@ async def _handle_input_text_action(code = await _resolve_multi_field_totp_code(task, attempt)if isinstance(code, ActionFailure):return [code]+ if attempt.code_source == "external" and context.external_otp_code_used(task.task_id, code):+ LOG.info("Refused to enter a one-time code again", task_id=task.task_id)+ return [_external_otp_reentry_refused()]return [await _fill_multi_field_totp_group(page, scraped_page, task, attempt, code)]dom = DomUtil(scraped_page, page)@@ -11499,7 +11533,9 @@ async def handle_keypress_action(task: Task,step: Step,) -> list[ActionResult]:+ otp_dispatch_effect.set("unknown")await handler_utils.keypress(page, action.keys, hold=action.hold, duration=action.duration, repeat=action.repeat)+ otp_dispatch_effect.set("dispatched")return [ActionSuccess()]@@ -12178,6 +12214,8 @@ async def chain_click(engine_selection = resolve_engine_selection_for_task(task, app.BROWSER_MANAGER)# Tracks the return value so the finally block can inspect click success.action_results: list[ActionResult] = []+ # Marked before the first await that may reach the page, so a cancellation mid-click keeps the press.+ otp_dispatch_effect.set("unknown")try:if not await skyvern_element.navigate_to_a_href(page=page):if resolved_href := await skyvern_element.resolve_http_href(page):@@ -12192,10 +12230,19 @@ async def chain_click(await EventStrategyFactory.move_to_element(page, locator)await _locator_click(locator, click_count, timeout=timeout)LOG.info("Chain click: main element click succeeded", action=action, locator=locator, sampling=True)+ otp_dispatch_effect.set("dispatched")action_results = [ActionSuccess()]return action_resultsexcept Exception as e:+ if otp_single_dispatch.get():+ # Only an actionability timeout is known to precede the click; anything else may follow it.+ post_dispatch = is_post_dispatch_click_timeout(e, engine_selection)+ if post_dispatch:+ otp_dispatch_effect.set("dispatched")+ elif is_engine_timeout_error(e, engine_selection):+ otp_dispatch_effect.set(None)+ return [ActionSuccess()] if post_dispatch else [ActionFailure(FailToClick(action.element_id, msg=str(e)))]if is_post_dispatch_click_timeout(e, engine_selection):LOG.info("Chain click: physical click dispatched; navigation-wait timed out — skipping fallback",diff --git a/skyvern/webeye/actions/multi_field_totp.py b/skyvern/webeye/actions/multi_field_totp.pyindex cd082302c..f4e8caad6 100644--- a/skyvern/webeye/actions/multi_field_totp.py+++ b/skyvern/webeye/actions/multi_field_totp.py@@ -19,6 +19,7 @@ from skyvern.exceptions import SkyvernPageAnalysisTimeoutfrom skyvern.forge.sdk.core import skyvern_contextfrom skyvern.forge.sdk.core.skyvern_context import MultiFieldTotpAttemptfrom skyvern.utils.contained_effects import contained_effect+from skyvern.webeye.actions.handler_utils import keys_include_enterfrom skyvern.webeye.scraper.scraped_page import ScrapedPagefrom skyvern.webeye.scraper.scraper import structural_identityfrom skyvern.webeye.utils.document import get_main_document_loader_id@@ -876,6 +877,72 @@ async def find_multi_field_totp_submit_controls(boxes: list[Locator]) -> list[Mureturn [candidate for _, candidate in sorted(candidates, key=lambda candidate: candidate[0])]+# A request for a new code, not text about a missing one ("didn't get a code? help").+_ONE_TIME_CODE_RESEND_LABEL = re.compile(+ r"\b(?:re-?send|send (?:it |the code |a code |the |a )?(?:new code|another code|again)|"+ r"(?:get|request) (?:a )?new code)",+ re.IGNORECASE,+)+_ONE_TIME_CODE_CONTROL_SELECTOR = (+ 'button, a[href], input[type="button"], input[type="submit"], [role="button"], [role="link"]'+)+++def is_one_time_code_resend_label(label: str) -> bool:+ return bool(_ONE_TIME_CODE_RESEND_LABEL.search(label))+++_ONE_TIME_CODE_CONTROL_LABEL_JS = """(node, selector) => node.closest(selector)+ ? node.getAttribute('aria-label') || node.innerText || node.value || ''+ : ''"""+++async def one_time_code_dispatch_kind(+ page: Page,+ scraped_page: ScrapedPage,+ expected_digits: int,+ *,+ element_id: str | None,+ keys: list[str] | None,+) -> Literal["submit", "resend"] | None:+ """Whether a click (``element_id``) or key press (``keys``) about to run submits the code group or asks for a new code.++ Only controls inside the group's widget count; a key press counts only as Enter on a focused control there.+ """+ group = _multi_field_totp_box_group(scraped_page, expected_digits)+ if not group:+ return None+ boxes = await _retry_box_locators(page, scraped_page, group)+ scopes = await _multi_field_totp_widget_scopes(boxes, include_ancestors=True)+ if not scopes:+ return None+ if keys is not None:+ if not keys_include_enter(keys):+ return None+ target = scopes[-1].locator(":focus")+ if await bounded_lookup_call(target.count) != 1:+ return None+ elif element_id and element_id in scraped_page.id_to_css_dict:+ [target] = await _retry_box_locators(page, scraped_page, [element_id])+ scope_handle = await scopes[-1].element_handle(timeout=1000)+ if not await target.evaluate("(node, scope) => scope.contains(node)", scope_handle, timeout=1000):+ return None+ else:+ return None+ label = await target.evaluate(_ONE_TIME_CODE_CONTROL_LABEL_JS, _ONE_TIME_CODE_CONTROL_SELECTOR, timeout=1000)+ if is_one_time_code_resend_label(label):+ return "resend"+ if keys is not None and await target.evaluate(+ "(node, boxes) => boxes.includes(node)", [await box.element_handle(timeout=1000) for box in boxes], timeout=1000+ ):+ return "submit"+ for control in await find_multi_field_totp_submit_controls(boxes):+ handle = await control.locator.element_handle(timeout=1000)+ if await target.evaluate("(node, control) => control.contains(node)", handle, timeout=1000):+ return "submit"+ return None++_MULTI_FIELD_TOTP_WIDGET_FINGERPRINT_JS = """(element) => {if (!element.isConnected) return null;const result = {};diff --git a/tests/unit/test_browser_effect_sink_enumeration.py b/tests/unit/test_browser_effect_sink_enumeration.pyindex 008aafeed..c75ceb483 100644--- a/tests/unit/test_browser_effect_sink_enumeration.py+++ b/tests/unit/test_browser_effect_sink_enumeration.py@@ -50,7 +50,7 @@ _DISCOVERED_BROWSER_API_CALLS = {"wheel": 5,}),- "skyvern/webeye/actions/multi_field_totp.py": Counter({"fill": 1, "click": 2, "press": 1, "evaluate": 5}),+ "skyvern/webeye/actions/multi_field_totp.py": Counter({"fill": 1, "click": 2, "press": 1, "evaluate": 9}),"skyvern/webeye/actions/handler_utils.py": Counter({"dispatch_event": 1, "down": 3, "evaluate": 1, "fill": 3, "press": 1, "up": 3}),@@ -114,6 +114,8 @@ _EVALUATE_CALLERS = {"bind": 1,"refresh": 1,"find_multi_field_totp_submit_controls": 1,+ # read-only checks that a pending click or Enter targets the code group's own submit or resend control+ "one_time_code_dispatch_kind": 4,}),"skyvern/webeye/actions/handler_utils.py": Counter({"_uses_native_value_set_fill": 1}),@@ -271,21 +273,21 @@ def test_discovered_browser_api_lower_bound_is_stable() -> None:}assert observed == _DISCOVERED_BROWSER_API_CALLS- assert sum(sum(methods.values()) for methods in observed.values()) == 213+ assert sum(sum(methods.values()) for methods in observed.values()) == 217handler_candidates = _candidate_signatures("skyvern/webeye/actions/handler.py", _CANDIDATE_METHODS)classified_non_browser = Counter({signature: count for signature, count in handler_candidates.items() if signature in _NON_BROWSER_CANDIDATES})assert classified_non_browser == _NON_BROWSER_CANDIDATESassert sum(_NON_BROWSER_CANDIDATES.values()) == 7- assert sum(sum(methods.values()) for methods in observed.values()) - sum(_NON_BROWSER_CANDIDATES.values()) == 206+ assert sum(sum(methods.values()) for methods in observed.values()) - sum(_NON_BROWSER_CANDIDATES.values()) == 210def test_every_raw_evaluate_call_is_classified() -> None:observed = {path: callers for path in _owned_source_paths() if (callers := _callers_for_method(path, "evaluate"))}assert observed == _EVALUATE_CALLERS- assert sum(sum(callers.values()) for callers in observed.values()) == 42+ assert sum(sum(callers.values()) for callers in observed.values()) == 46def test_every_cdp_dispatch_is_classified_by_exact_command() -> None:diff --git a/tests/unit/test_multi_field_totp.py b/tests/unit/test_multi_field_totp.pyindex c2621cef0..bf0e97311 100644--- a/tests/unit/test_multi_field_totp.py+++ b/tests/unit/test_multi_field_totp.py@@ -8,6 +8,7 @@ import inspectimport jsonimport shutilimport subprocess+import timefrom copy import deepcopyfrom datetime import UTC, datetime, timedeltafrom pathlib import Path@@ -25,6 +26,7 @@ from pydantic import TypeAdapterfrom skyvern.config import settingsfrom skyvern.exceptions import SkyvernPageAnalysisTimeoutfrom skyvern.forge import agent as agent_module+from skyvern.forge import agent_functions as agent_functions_modulefrom skyvern.forge.agent import (ForgeAgent,_first_plan_carries_consumable_totp,@@ -41,12 +43,14 @@ from skyvern.forge.sdk.core.skyvern_context import (from skyvern.forge.sdk.db.utils import hydrate_actionfrom skyvern.forge.sdk.models import Step, StepStatusfrom skyvern.forge.sdk.schemas.tasks import Task+from skyvern.forge.sdk.schemas.totp_codes import TOTPCodefrom skyvern.forge.sdk.services.bitwarden import BitwardenConstantsfrom skyvern.forge.sdk.workflow import service as workflow_servicefrom skyvern.forge.sdk.workflow.context_manager import WorkflowContextManager, WorkflowRunContextfrom skyvern.forge.sdk.workflow.models.parameter import CredentialParameterfrom skyvern.schemas.run_enums import RunEnginefrom skyvern.services import otp_service+from skyvern.services.otp_email import EmailOTPCandidatefrom skyvern.services.otp_service import OTPType, OTPValuefrom skyvern.utils import stall_watchfrom skyvern.utils.action_redaction import REDACTED_INPUT_VALUE, redact_action_for_log@@ -57,6 +61,7 @@ from skyvern.webeye.actions.actions import (ActionStatus,ActionType,ClickAction,+ CompleteAction,InputOrSelectContext,InputTextAction,TerminateAction,@@ -93,7 +98,10 @@ def _submit_candidate(control):@pytest.fixture(autouse=True)def stub_fill_observer_for_dom_fakes(monkeypatch: pytest.MonkeyPatch, request: pytest.FixtureRequest) -> None:- if request.function.__name__ == "test_multi_field_totp_browser_privacy_after_fill":+ if (+ request.function.__name__ == "test_multi_field_totp_browser_privacy_after_fill"+ or request.function.__name__.startswith("test_external_otp_browser")+ ):returnmonkeypatch.setattr(handler,@@ -2350,7 +2358,9 @@ async def test_group_fill_same_page_detach_reresolves_once_then_falls_back(assert not context.totp_codesassert state.filled_code_hash is Noneelse:- assert context.totp_codes == {task.task_id: code}+ # The whole code was sent before the group changed: it is recorded as entered and not handed back.+ assert not context.totp_codes+ assert context.external_otp_code_used(task.task_id, code)assert state.filled_code_hash == hashlib.sha256(code.encode()).hexdigest()assert not state.fill_verifiedwith skyvern_context.scoped(context):@@ -2359,7 +2369,7 @@ async def test_group_fill_same_page_detach_reresolves_once_then_falls_back(step=SimpleNamespace(),scraped_page=_box_page(0),)- assert next_payload["verification_code"] == "*"+ assert code not in json.dumps(next_payload)assert all(entry in (call(0.1), call(0.2)) for entry in sleep.await_args_list)assert sum(entry.args[0] for entry in sleep.await_args_list if entry == call(0.2)) <= 0.4if not accepted:@@ -2736,8 +2746,11 @@ async def test_execute_step_rebound_group_requires_expected_value(assert getattr(state, attribute) == getattr(before_dispatch, attribute)persisted_inputs.append(action_for_multi_field_totp_persistence(action))else:- assert kwargs["scraped_page"] is scraped_page- assert action.element_id in {"open-authenticator", "fixture-submit"}+ # Once an external code is entered, the executor remaps a stale submit itself, before its guard.+ remapped_early = action.element_id == "remounted-submit"+ assert not remapped_early or context.external_otp_uses+ assert kwargs["scraped_page"] is (fresh if remapped_early else scraped_page)+ assert action.element_id in {"open-authenticator", "fixture-submit", "remounted-submit"}return await handler.ActionHandler._handle_action(scraped_page=kwargs["scraped_page"],page=kwargs["page"],@@ -2791,6 +2804,7 @@ async def test_execute_step_rebound_group_requires_expected_value(monkeypatch.setitem(handler.ActionHandler._handled_action_types, handler.ActionType.CLICK, click_handler)remapper = AsyncMock(wraps=handler._refresh_stale_web_action_before_dispatch)monkeypatch.setattr(handler, "_refresh_stale_web_action_before_dispatch", remapper)+ monkeypatch.setattr(agent_module, "_refresh_stale_web_action_before_dispatch", remapper)agent = ForgeAgent()agent.record_artifacts_after_action = AsyncMock(return_value=None)agent._persist_scrape_artifacts = AsyncMock()@@ -3528,10 +3542,20 @@ async def test_rejection_retry_termination_contract(monkeypatch: pytest.MonkeyPafor pending in context.pending_speculative_persist_tasks:await pendingassert not submission_errors, repr(submission_errors)- if scenario in {- "retry",+ # An external code is never fetched again automatically; a new one needs an executed resend.+ external_no_retry = {"retry_external","retry_external_fullwidth",+ "unsupported_punctuation",+ "unsupported_letter",+ "poll_error",+ "used_cached_external",+ }+ if scenario in external_no_retry:+ assert resolve_external.await_args is None+ assert fill.await_args is None+ if scenario in {+ "retry","retry_rebound","retry_fill_remount","retry_submit_timeout",@@ -3558,24 +3582,10 @@ async def test_rejection_retry_termination_contract(monkeypatch: pytest.MonkeyPaassert context.speculative_plans == {}if scenario == "retry_consumed":assert submit.await_args is None- if scenario in {"retry_external", "retry_external_fullwidth"}:- assert credential_resolution.await_args is None- assert context.totp_codes[f"{task.task_id}_totp_cache"] == fresh_code- rejection = context.multi_field_totp_rejections[task.task_id]- assert resolve_external.await_args.kwargs["created_after"] == datetime.fromtimestamp(10, UTC).replace(- tzinfo=None- )- assert resolve_external.await_args.kwargs["rejected_code_hash"] == rejection.rejected_code_hash- assert resolve_external.await_args.kwargs["multi_field_expected_digits"] == attempt.expected_digits- with skyvern_context.scoped(context):- prompt_fields = agent_module._replace_multi_field_totp_prompt_code(- task.task_id, {"navigation_goal": f"Use code {_CODE}; terminate if rejected."}- )- assert _CODE not in prompt_fields["navigation_goal"]else:assert result[2] is Noneskipped = [entry for entry in retry_logs if entry["event"] == "Multi-field TOTP retry skipped"]- if scenario in {"used_rebuilt", "used_cached_external", "used_cached_secret"}:+ if scenario in {"used_rebuilt", "used_cached_secret"}:assert not skippedelse:assert len(skipped) == 1@@ -3592,17 +3602,14 @@ async def test_rejection_retry_termination_contract(monkeypatch: pytest.MonkeyPa"used_unsubmitted": "retry_budget_exhausted","classifier_error": "classifier_error","classifier_timeout": "classifier_error",+ **dict.fromkeys(external_no_retry, "external_code_not_retried"),}.get(scenario, skipped[0]["reason"])assert _CODE not in str(skipped) and fresh_code not in str(skipped)assert update_task.await_args.kwargs["failure_reason"] == (- SECOND_REJECTION_REASON- if scenario in {"used_rebuilt", "used_cached_external", "used_cached_secret"}- else reason+ SECOND_REJECTION_REASON if scenario in {"used_rebuilt", "used_cached_secret"} else reason)assert context.multi_field_totp_rejections == original_ledgerif scenario.startswith("unsupported_"):- assert f"{task.task_id}_totp_cache" not in context.totp_codes- assert any(entry.get("reason") == "unsupported_code_format" for entry in retry_logs)assert supplied_external not in str(retry_logs)if scenario not in {"no_evidence", "clear_failed", "clear_error", "submission_error", "submit_trial_timeout"}:assert clear.await_args is None@@ -3610,7 +3617,6 @@ async def test_rejection_retry_termination_contract(monkeypatch: pytest.MonkeyPaexpected_error = {"submission_error": ("submission_failed", "TypeError"),"clear_error": ("clear_failed", "TimeoutError"),- "poll_error": ("external_code_poll_failed", "RuntimeError"),"submit_trial_timeout": ("submit_control_not_actionable", "TimeoutError"),}.get(scenario)if expected_error:@@ -4052,7 +4058,7 @@ async def test_retry_submit_discovery_stays_with_otp_widget(markup: str, selecte@pytest.mark.asyncio@pytest.mark.parametrize("source", ["db", "email"])@pytest.mark.parametrize("obtained_offset", [0, None, 25])-async def test_external_retry_accepts_code_pushed_before_rejection_decision(+async def test_external_rejection_does_not_fetch_a_code_pushed_before_the_decision(monkeypatch: pytest.MonkeyPatch, source: str, obtained_offset: int | None) -> None:obtained = datetime(2026, 9, 14, tzinfo=UTC)@@ -4150,13 +4156,9 @@ async def test_external_retry_accepts_code_pushed_before_rejection_decision(result = await ForgeAgent()._maybe_retry_multi_field_totp_after_rejection(task, step, page, snapshot, "The one-time code was rejected.")- expected_cutoff = (obtained if obtained_offset == 0 else delivered).replace(tzinfo=None)- assert cutoffs and all(cutoff == expected_cutoff for cutoff in cutoffs)- assert result == multi_field_totp_module.RetryOutcome.RETRIED- assert fill.await_args.args[-1] == fresh.value- assert context.multi_field_totp_rejections[task.task_id].rejected_at == decision- assert context.multi_field_totp_rejections[task.task_id].rejected_code_obtained_at == expected_cutoff- assert state.external_code_obtained_at == Clock.now_value.replace(tzinfo=UTC).timestamp()+ assert result == multi_field_totp_module.RetryOutcome.NO_RETRY+ assert not cutoffs+ fill.assert_not_awaited()@pytest.mark.asyncio@@ -6588,11 +6590,10 @@ async def test_verified_burst_observation_controls_retry_dispatch(outcome = await ForgeAgent()._maybe_retry_multi_field_totp_after_rejection(task, step, page, snapshot, "The submitted one-time code was rejected.")- assert outcome == multi_field_totp_module.RetryOutcome.RETRIED- rejection = context.multi_field_totp_rejections[task.task_id]- assert rejection.retry_used and rejection.submitted_at is not None- assert len(dispatches) == (0 if auto_submit else 1)- result = fill_results[0]+ # A rejected external code is not replaced by a fetched one; that needs an executed resend.+ assert outcome == multi_field_totp_module.RetryOutcome.NO_RETRY+ assert not fill_results and not dispatches+ returnassert isinstance(result, ActionSuccess) and state.fill_verifiedassert result.data.get("totp_submission_observed", False) is auto_submitif auto_submit:@@ -6733,3 +6734,926 @@ async def test_computer_use_code_hand_off_without_a_workflow_run_marks_the_typedpersisted = action_for_multi_field_totp_persistence(action, codes)assert isinstance(persisted, InputTextAction) and persisted.totp_code_requiredassert action.text == ONE_TIME_CODE+++_OTP_PAGE = (+ '<form unique_id="otp-scope" id="otp-scope">Enter the code we sent you'+ + "".join(f'<input unique_id="box-{i}" id="box-{i}" maxlength="1" inputmode="numeric">' for i in range(6))+ + '<label><input unique_id="remember" id="remember" type="checkbox">Remember this device</label>'+ + '<button unique_id="verify" id="verify" type="button">Verify</button>'+ + '<button unique_id="resend" id="resend" type="button">Resend code</button>'+ + '<button unique_id="help" id="help" type="button">Didn\'t get a code? Help</button>'+ + '<div id="status"></div></form>'+)+_OTP_PAGE_JS = """(cfg) => {+ window.__submits = 0;+ window.__resends = 0;+ window.__fullEntries = 0;+ window.__verifyClicks = 0;+ const form = () => document.querySelector('#otp-scope');+ const boxes = () => [...form().querySelectorAll('input[inputmode]')];+ const verify = () => document.querySelector('#verify');+ const submit = () => {+ window.__submits += 1;+ if (cfg.submitStatus) document.querySelector('#status').textContent = cfg.submitStatus;+ if (cfg.disableOnSubmit) boxes().forEach(box => { box.disabled = true; });+ if (cfg.hideAfterSubmit) form().style.display = 'none';+ if (cfg.clear) boxes().forEach(box => { box.value = ''; box.setAttribute('value', ''); });+ };+ // A framework re-render: every control becomes a fresh node, without the markers Skyvern injected.+ const remount = () => form().querySelectorAll('[unique_id]').forEach(node => {+ const fresh = node.cloneNode(true);+ fresh.removeAttribute('unique_id');+ node.replaceWith(fresh);+ });+ if (cfg.enableVerifyWhenFull) verify().disabled = true;+ // Delegated, so the page keeps working after a remount.+ form().addEventListener('input', event => {+ const box = event.target;+ const i = boxes().indexOf(box);+ if (i < 0) return;+ box.setAttribute('value', box.value);+ if (box.value && boxes()[i + 1] && (cfg.stopAdvancingAt === null || i + 1 < cfg.stopAdvancingAt)) {+ boxes()[i + 1].focus();+ }+ if (boxes().every(item => item.value)) {+ window.__fullEntries += 1;+ // A re-render that silently drops the last digit of a complete entry: no submit, no status change.+ if (cfg.dropLastDigitWhenFull) {+ boxes().at(-1).value = '';+ boxes().at(-1).setAttribute('value', '');+ }+ }+ if (cfg.enableVerifyWhenFull) verify().disabled = !boxes().every(item => item.value);+ if (cfg.autoSubmit && boxes().every(item => item.value)) setTimeout(submit, 150);+ });+ form().addEventListener('keydown', event => {+ if (event.key === 'Enter' && boxes().includes(event.target)) submit();+ });+ form().addEventListener('click', event => {+ if (event.target.closest('#verify')) {+ window.__verifyClicks += 1;+ submit();+ }+ if (event.target.closest('#resend')) window.__resends += 1;+ if (event.target.closest('#remember') && cfg.remountOnRemember) remount();+ });+ document.querySelector('#resend').disabled = cfg.resendDisabled;+}"""+_NEW_CODE = "483917"+++def _step_output(scraped: Any, actions: list[Action]) -> Any:+ from skyvern.webeye.actions.models import DetailedAgentStepOutput++ return DetailedAgentStepOutput(+ scraped_page=scraped,+ extract_action_prompt="",+ llm_response=None,+ actions=actions,+ action_results=[],+ actions_and_results=[],+ )+++class _OtpSite:+ """A synthetic code page driven through real acquisition, the OTP hook, the step executor and finalization.++ Codes arrive through the real producer of ``channel``: the connected-inbox lookup (only its mailbox search and+ the content parser are replaced), the webhook (only its network call), or stored codes (only the row query).+ The planner's language model is replaced too.+ """++ def __init__(+ self, monkeypatch: pytest.MonkeyPatch, live_page: Any, task: Task, deliveries: list[str], channel: str = "email"+ ) -> None:+ from skyvern.webeye.scraper.scraped_page import ScrapedPage++ monkeypatch.setattr(ScrapedPage, "generate_scraped_page_without_screenshots", lambda _page: self.scrape())+ self.page = live_page+ self.task = task+ self.context = SkyvernContext(task_id=task.task_id)+ self.agent = ForgeAgent()+ self.deliveries = deliveries+ self.channel = channel+ self.fetch = AsyncMock(side_effect=self._deliver)+ self.last_scrape: Any = None+ self.browser_state = SimpleNamespace(+ get_working_page=AsyncMock(return_value=live_page),+ must_get_working_page=AsyncMock(return_value=live_page),+ engine_selection=None,+ take_post_action_screenshot=AsyncMock(return_value=b""),+ )+ self.task_updates: list[dict[str, Any]] = []++ async def update_task(_task, status=None, **kwargs):+ self.task_updates.append({"status": status, **kwargs})+ return _task++ async def replan(*_a, **_k):+ return {"actions": self.entry_plan()}++ async def build_prompt(_task, step, _browser_state, scraped_page, **_k):+ self.agent._build_navigation_payload(_task, step=step, scraped_page=scraped_page)+ return SimpleNamespace(+ prompt="", prompt_name="extract-action", use_caching=False, without_page_information=True+ )++ monkeypatch.setattr(otp_service, "_get_otp_value_from_url", AsyncMock(side_effect=self._from_webhook))+ inbox = SimpleNamespace(+ name="gmail",+ list_credential_ids=AsyncMock(return_value=["credential-1"]),+ search_recent_otp_messages=AsyncMock(side_effect=self._from_inbox),+ )+ monkeypatch.setattr(agent_functions_module, "build_email_otp_sources", lambda _agent_function: [inbox])+ # The test app stubs AGENT_FUNCTION; bind the real connected-inbox producer to it.+ monkeypatch.setattr(+ otp_service.app.AGENT_FUNCTION,+ "get_otp_value_from_email",+ agent_functions_module.AgentFunction().get_otp_value_from_email,+ )+ monkeypatch.setattr(+ otp_service,+ "parse_otp_login",+ AsyncMock(side_effect=lambda content, *_a, **_k: OTPValue(value=content, type=OTPType.TOTP)),+ )+ monkeypatch.setattr(otp_service.app.DATABASE.otp, "create_otp_code", AsyncMock())+ monkeypatch.setattr(otp_service.app.DATABASE.otp, "get_otp_codes", AsyncMock(side_effect=self._from_store))+ monkeypatch.setattr(otp_service.app.DATABASE.otp, "get_raw_otp_codes", AsyncMock(return_value=[]))+ monkeypatch.setattr(otp_service.app.AGENT_FUNCTION, "enforce_totp_identifier_credential_use", AsyncMock())+ monkeypatch.setattr(+ otp_service, "asyncio", ScopedAsyncio(sleep=lambda seconds: asyncio.sleep(min(seconds, 0.05)))+ )+ monkeypatch.setattr(+ otp_service.app.DATABASE.organizations,+ "get_valid_org_auth_token",+ AsyncMock(return_value=SimpleNamespace(token="token")),+ )+ monkeypatch.setattr(agent_module.app.DATABASE.workflow_runs, "get_workflow_run", AsyncMock(return_value=None))+ monkeypatch.setattr(agent_module, "get_org_aware_primary_llm_api_handler", lambda: replan)+ monkeypatch.setattr(agent_module.LLMAPIHandlerFactory, "get_override_llm_api_handler", lambda *a, **k: replan)+ monkeypatch.setattr(agent_module.service_utils, "is_cua_task", AsyncMock(return_value=False))+ monkeypatch.setattr(self.agent, "_build_extract_action_prompt", build_prompt)+ # The existing completion check is an LLM call; each test decides what it reports.+ self.completion = AsyncMock(return_value=None)+ monkeypatch.setattr(self.agent, "check_user_goal_complete", self.completion)+ monkeypatch.setattr(self.agent, "get_extracted_information_for_task", AsyncMock(return_value=None))+ monkeypatch.setattr(self.agent, "record_artifacts_after_action", AsyncMock())++ def update_step(step, status=None, is_last=None, **_):+ if status is not None:+ step.status = status+ if is_last is not None:+ step.is_last = is_last+ return step++ monkeypatch.setattr(self.agent, "update_step", AsyncMock(side_effect=update_step))+ monkeypatch.setattr(+ agent_module.app.DATABASE.tasks,+ "create_step",+ AsyncMock(+ side_effect=lambda **kw: make_step(+ datetime.now(UTC),+ task,+ step_id=f"retry-{kw['order']}-{kw['retry_index']}",+ status=StepStatus.created,+ order=kw["order"],+ output=None,+ retry_index=kw["retry_index"],+ )+ ),+ )+ monkeypatch.setattr(self.agent, "update_task", update_task)+ monkeypatch.setattr(agent_module, "read_run_cancellation", AsyncMock(return_value=None))+ monkeypatch.setattr(agent_module, "get_or_create_wait_config", AsyncMock(return_value=None))+ monkeypatch.setattr(handler, "get_or_create_wait_config", AsyncMock(return_value=None))+ monkeypatch.setattr(agent_module, "get_wait_time", lambda *_a, **_k: 0.0)+ monkeypatch.setattr(+ agent_module.app.DATABASE.workflow_params,+ "create_action",+ AsyncMock(return_value=SimpleNamespace(action_id="action")),+ )+ monkeypatch.setattr(agent_module.app.AGENT_FUNCTION, "post_action_execution", AsyncMock())+ monkeypatch.setattr(settings, "ENABLE_SECRET_VISUAL_MASKING", False)+ # Cloud test modules register click setup hooks process-wide; this OSS page runs without them.+ monkeypatch.setattr(handler.ActionHandler, "_setup_action_types", {})+ monkeypatch.setattr(handler.ActionHandler, "_teardown_action_types", {})++ async def _deliver(self, channel: str) -> str | None:+ return self.deliveries[-1] if self.deliveries and channel == self.channel else None++ async def _from_webhook(self, *_a, **_k) -> OTPValue | None:+ code = await self.fetch("webhook")+ return OTPValue(value=code, type=OTPType.TOTP) if code else None++ async def _from_inbox(self, **_k) -> list[EmailOTPCandidate]:+ code = await self.fetch("email")+ return [EmailOTPCandidate(message_id=f"message-{len(self.deliveries)}", content=code)] if code else []++ async def _from_store(self, **_k) -> list[TOTPCode]:+ code = await self.fetch("db")+ if not code:+ return []+ now = datetime.now(UTC)+ return [+ TOTPCode(+ totp_code_id=f"code-{len(self.deliveries)}",+ totp_identifier=self.task.totp_identifier or "",+ organization_id=self.task.organization_id or "",+ content=code,+ code=code,+ source="sms",+ otp_type=OTPType.TOTP,+ created_at=now,+ modified_at=now,+ )+ ]++ async def scrape(self) -> Any:+ from skyvern.webeye.scraper.scraped_page import ScrapedPage+ from skyvern.webeye.scraper.scraper import build_element_dict, trim_element_tree+ from skyvern.webeye.utils.document import get_main_document_loader_id+ from skyvern.webeye.utils.page import SkyvernFrame++ frame = await SkyvernFrame.create_instance(self.page)+ elements, tree, _ = await frame.build_tree_from_body("main.frame", 0)+ css, by_id, frames, hashes, hash_ids = build_element_dict(elements)+ self.last_scrape = ScrapedPage(+ elements=elements,+ element_tree=tree,+ element_tree_trimmed=trim_element_tree(deepcopy(tree)),+ id_to_css_dict=css,+ id_to_element_dict=by_id,+ id_to_frame_dict=frames,+ id_to_element_hash=hashes,+ hash_to_element_ids=hash_ids,+ url=self.page.url,+ _browser_state=self.browser_state,+ _clean_up_func=AsyncMock(),+ _scrape_exclude=None,+ # As the production scraper does, so document-continuity checks have the batch's document.+ _document_loader_id=await get_main_document_loader_id(self.page),+ )+ return self.last_scrape++ async def count(self, name: str) -> int:+ return await self.page.evaluate(f"() => window.__{name}")++ def entry_plan(self) -> list[dict[str, Any]]:+ attempt = self.context.multi_field_totp.get(self.task.task_id)+ hint = attempt.hint_code if attempt and attempt.hint_code else _HINT+ # Like the planner, address the boxes by the ids of the latest scrape; a re-render assigns new ones.+ boxes = (_multi_field_totp_box_group(self.last_scrape, 6) if self.last_scrape else None) or [+ f"box-{i}" for i in range(6)+ ]+ return [+ {"action_type": "input_text", "element_id": boxes[i], "text": hint[i], "reasoning": "Enter the code"}+ for i in range(6)+ ]++ async def execute(self, step: Step, actions: list[Action], scraped: Any = None) -> Step | None:+ """Run the step executor, then the same finalizer as ``agent_step``; returns the next step, if any."""+ scraped = scraped or await self.scrape()+ output = _step_output(scraped, actions)+ _, early_return = await self.agent._execute_step_actions(+ task=self.task,+ step=step,+ browser_state=self.browser_state,+ engine=RunEngine.skyvern_v1,+ scraped_page=scraped,+ complete_verification=False,+ actions=actions,+ detailed_agent_step_output=output,+ pdf_auto_download_src=None,+ pdf_auto_download_used_bytes=False,+ file_download_false_click_eligible=False,+ _step_span=MagicMock(),+ artifact_tracker=agent_module._BackgroundArtifactTaskTracker(),+ )+ organization = make_organization(datetime.now(UTC))+ if early_return is not None:+ return await self.agent.handle_failed_step(organization, self.task, early_return[0])+ finalized, _ = await self.agent._finalize_step_execution(+ task=self.task,+ step=step,+ browser_state=self.browser_state,+ engine=RunEngine.skyvern_v1,+ scraped_page=scraped,+ complete_verification=False,+ task_block=None,+ actions=actions,+ detailed_agent_step_output=output,+ )+ _, _, next_step = await self.agent.handle_completed_step(+ organization, self.task, finalized, self.page, complete_verification=False+ )+ return next_step++ @property+ def final_status(self) -> Any:+ return self.task_updates[-1]["status"] if self.task_updates else None++ async def step(+ self,+ step: Step,+ extra: list[dict[str, Any]] | None = None,+ *,+ entry: bool = True,+ code_field: bool = True,+ hydrate: bool = False,+ ) -> list[Action]:+ """One agent step: the OTP hook on a first-pass plan, then the executor on the plan that survives it.++ ``hydrate`` first builds the navigation payload as the step's prompt does, which arms credential codes.+ """+ from skyvern.forge.agent import ReplacedOTPPlan+ from skyvern.webeye.actions.parse_actions import parse_actions++ self.context.step_id = step.step_id+ scraped = await self.scrape()+ if hydrate:+ self.agent._build_navigation_payload(self.task, step=step, scraped_page=scraped)+ plan = {+ "place_to_enter_verification_code": code_field,+ "should_enter_verification_code": code_field,+ "actions": [*(self.entry_plan() if entry else []), *(extra or [])],+ }+ otp = await self.agent.handle_potential_OTP_actions(self.task, step, scraped, self.browser_state, plan)+ if isinstance(otp, ReplacedOTPPlan) or otp[1]:+ planned = otp[1]+ else:+ planned = parse_actions(self.task, step.step_id, step.order, scraped, plan["actions"])+ self.next_step = await self.execute(step, planned, scraped)+ return planned+++async def _launch_otp_page(playwright: Any, **cfg: Any) -> tuple[Any, Any]:+ browser = await playwright.chromium.launch(headless=True, args=["--use-mock-keychain", "--password-store=basic"])+ live_page = await browser.new_page()+ await _open_routed_otp_page(live_page, "/verify-code", **cfg)+ return browser, live_page+++_OTP_PAGE_DEFAULTS = {+ "autoSubmit": True,+ "clear": True,+ "resendDisabled": False,+ "submitStatus": "Verifying…",+ "hideAfterSubmit": False,+ "disableOnSubmit": False,+ "enableVerifyWhenFull": False,+ "stopAdvancingAt": None,+ "dropLastDigitWhenFull": False,+ "remountOnRemember": False,+}+++async def _open_routed_otp_page(live_page: Any, path: str, **cfg: Any) -> None:+ """Serve the code page from a real origin, as a login page would be."""++ async def serve(route: Any) -> None:+ await route.fulfill(status=200, body=f"<html><body>{_OTP_PAGE}</body></html>", content_type="text/html")++ await live_page.route("https://otp.test/**", serve)+ await live_page.goto(f"https://otp.test{path}")+ await live_page.evaluate(_OTP_PAGE_JS, {**_OTP_PAGE_DEFAULTS, **cfg})+++def _otp_task(*, webhook: bool = False, inbox: bool = True) -> tuple[Task, list[Step]]:+ now = datetime.now(UTC)+ task = make_task(now, make_organization(now), navigation_payload={}, url="about:blank")+ task.totp_verification_url = "https://example.test/otp" if webhook else None+ task.totp_identifier = "[email protected]" if inbox else None+ steps = [+ make_step(now, task, step_id=f"step-{i}", status=StepStatus.created, order=i, output=None) for i in range(5)+ ]+ return task, steps+++def _terminal_codes(planned: list[Action]) -> list[str]:+ return [error.error_code for action in planned if isinstance(action, TerminateAction) for error in action.errors]+++def _click(element_id: str, **extra: Any) -> dict[str, Any]:+ return {"action_type": "click", "element_id": element_id, "reasoning": "plan", **extra}+++async def _counts(site: _OtpSite) -> tuple[int, int, int]:+ return (await site.count("fullEntries"), await site.count("verifyClicks"), _fetched(site))+++def _fetched(site: _OtpSite) -> int:+ return sum(1 for entry in site.fetch.await_args_list if entry.args[0] == site.channel)+++async def _run_until_terminal(+ site: _OtpSite, steps: list[Step], *, budget_s: float, hydrate: bool = False+) -> list[Action]:+ """Keep re-planning code entry, as the planner did in the incident, until the task ends."""+ planned: list[Action] = []+ deadline = time.monotonic() + budget_s + 3+ for step in steps:+ planned = await site.step(step, hydrate=hydrate)+ if site.next_step is None or time.monotonic() > deadline:+ return planned+ if [action.action_type for action in planned] == [ActionType.NULL_ACTION]:+ await asyncio.sleep(budget_s / 3)+ return planned+++def _many_steps(task: Task, count: int) -> list[Step]:+ now = datetime.now(UTC)+ return [+ make_step(now, task, step_id=f"step-{i}", status=StepStatus.created, order=i, output=None) for i in range(count)+ ]+++@_skip_no_browser+ "page_reaction",+ [+ {"clear": True},+ {"clear": False, "submitStatus": None},+ {"clear": False, "disableOnSubmit": True},+ {"clear": False, "submitStatus": "Enter the code we just sent"},+ ],+ ids=["silently_cleared", "retained_unchanged", "boxes_disabled", "label_changed"],+)+async def test_external_otp_browser_entered_code_is_not_entered_again(+ monkeypatch: pytest.MonkeyPatch, page_reaction: dict[str, Any]+) -> None:+ from playwright.async_api import async_playwright++ task, _ = _otp_task()+ steps = _many_steps(task, 10)+ monkeypatch.setattr(settings, "BROWSER_LOADING_TIMEOUT_MS", 1500)+ async with async_playwright() as playwright:+ browser, live_page = await _launch_otp_page(playwright, **page_reaction)+ try:+ site = _OtpSite(monkeypatch, live_page, task, [_CODE])+ with skyvern_context.scoped(site.context), structlog.testing.capture_logs() as events:+ await site.step(steps[0], entry=False)+ await live_page.wait_for_function("() => window.__submits === 1")+ planned = await _run_until_terminal(site, steps[1:], budget_s=1.5)+ assert await _counts(site) == (1, 0, 1)+ assert _terminal_codes(planned) == ["OTP_OUTCOME_UNCONFIRMED"]+ assert site.final_status == agent_module.TaskStatus.terminated and site.next_step is None+ assert "not entered again" in site.task_updates[-1]["failure_reason"]+ finally:+ await browser.close()+ assert _CODE not in str(events)+++@_skip_no_browser[email protected]("opaque_autosubmit", [False, True])+async def test_external_otp_browser_first_manual_verify_still_runs_once(+ monkeypatch: pytest.MonkeyPatch, opaque_autosubmit: bool+) -> None:+ from playwright.async_api import async_playwright++ task, steps = _otp_task()+ task.data_extraction_goal = None+ cfg: dict[str, Any] = (+ {"autoSubmit": True, "clear": False, "submitStatus": None}+ if opaque_autosubmit+ else {"autoSubmit": False, "clear": False, "enableVerifyWhenFull": True}+ )+ async with async_playwright() as playwright:+ browser, live_page = await _launch_otp_page(playwright, **cfg)+ try:+ site = _OtpSite(monkeypatch, live_page, task, [_CODE])+ with skyvern_context.scoped(site.context):+ await site.step(steps[0], entry=False)+ await asyncio.sleep(0.4)+ # The plan types the code again and presses Verify: only the press runs, and only once.+ await site.step(steps[1], [_click("verify"), _click("verify")])+ assert await _counts(site) == (1, 1, 1)+ site.completion.return_value = CompleteAction(reasoning="Signed in", verified=True)+ await site.step(steps[2], [{"action_type": "complete", "reasoning": "Signed in"}], entry=False)+ assert site.final_status == agent_module.TaskStatus.completed and site.next_step is None+ assert await _counts(site) == (1, 1, 1)+ finally:+ await browser.close()+++@_skip_no_browser[email protected]("failure", ["partial_entry", "detach_after_full_entry"])+async def test_external_otp_browser_partial_entry_is_repaired_but_full_entry_is_not_replayed(+ monkeypatch: pytest.MonkeyPatch, failure: str+) -> None:+ from playwright.async_api import async_playwright++ task, steps = _otp_task()+ monkeypatch.setattr(settings, "BROWSER_LOADING_TIMEOUT_MS", 1500)+ cfg: dict[str, Any] = {"autoSubmit": False, "clear": False}+ if failure == "partial_entry":+ cfg["stopAdvancingAt"] = 2+ async with async_playwright() as playwright:+ browser, live_page = await _launch_otp_page(playwright, **cfg)+ try:+ site = _OtpSite(monkeypatch, live_page, task, [_CODE])+ if failure == "detach_after_full_entry":+ monkeypatch.setattr(+ handler,+ "_read_multi_field_totp_values",+ AsyncMock(side_effect=PlaywrightError("Element is not attached to the DOM")),+ )+ monkeypatch.setattr(+ handler,+ "_reresolve_multi_field_totp_group_elements",+ AsyncMock(return_value=MultiFieldTotpBindingFailure.UNCONFIRMED),+ )+ with skyvern_context.scoped(site.context):+ await site.step(steps[0], entry=False)+ values = await live_page.locator("#otp-scope input[inputmode]").evaluate_all(+ "bs => bs.map(b => b.value)"+ )+ if failure == "partial_entry":+ # Focus stopped after two digits; the remaining boxes are filled once, in the same action.+ assert values == list(_CODE) and await site.count("fullEntries") == 1+ else:+ # Cleanup after the detach restores no code: the next plans cannot type it again.+ await live_page.locator("#otp-scope input[inputmode]").evaluate_all(+ "bs => bs.forEach(b => b.value = '')"+ )+ await site.step(steps[1])+ await site.step(steps[2])+ assert await site.count("fullEntries") == 1 and _fetched(site) == 1+ finally:+ await browser.close()+++@_skip_no_browser[email protected]("failure", ["after_effect", "cancelled_after_effect", "before_effect", "teardown"])+async def test_external_otp_browser_verify_press_reaches_the_page_once(+ monkeypatch: pytest.MonkeyPatch, failure: str+) -> None:+ from playwright.async_api import TimeoutError as PlaywrightTimeout+ from playwright.async_api import async_playwright++ task, steps = _otp_task()+ real_click = handler.EventStrategyFactory.click_element+ calls = {"count": 0}++ async def click_element(page, locator, **kwargs):+ calls["count"] += 1+ if failure == "before_effect" and calls["count"] == 1:+ raise PlaywrightTimeout("locator.click: Timeout exceeded. waiting for element to be visible, enabled")+ await real_click(page, locator, **kwargs)+ if failure == "after_effect" and calls["count"] == 1:+ raise RuntimeError("Execution context was destroyed after the click")+ if failure == "cancelled_after_effect" and calls["count"] == 1:+ raise asyncio.CancelledError++ async def failing_teardown(*_args, **_kwargs):+ raise RuntimeError("teardown failed after the click")++ async with async_playwright() as playwright:+ browser, live_page = await _launch_otp_page(playwright, autoSubmit=False, clear=False)+ try:+ site = _OtpSite(monkeypatch, live_page, task, [_CODE])+ monkeypatch.setattr(handler.EventStrategyFactory, "click_element", click_element)+ if failure == "teardown":+ monkeypatch.setattr(+ handler.ActionHandler, "_teardown_action_types", {ActionType.CLICK: failing_teardown}+ )+ with skyvern_context.scoped(site.context):+ await site.step(steps[0], entry=False)+ if failure == "cancelled_after_effect":+ with pytest.raises(asyncio.CancelledError):+ await site.step(steps[1], [_click("verify")], entry=False, code_field=False)+ assert handler.otp_single_dispatch.get() is False and handler.otp_dispatch_effect.get() is None+ else:+ await site.step(steps[1], [_click("verify")], entry=False, code_field=False)+ assert await site.count("verifyClicks") == (0 if failure == "before_effect" else 1)+ # Only a press known not to have landed may run again; no fallback follows a possible press.+ await site.step(steps[2], [_click("verify")], entry=False, code_field=False)+ assert await site.count("verifyClicks") == 1+ use = site.context.external_otp_uses[task.task_id]+ assert use.current_code_hash in use.submitted_code_hashes+ finally:+ await browser.close()+++@_skip_no_browser+ ("case", "webhook_first"),+ [+ ("new_code", False),+ # A webhook is configured but has no code, so the inbox fallback delivers.+ ("new_code", True),+ ("old_code_only", False),+ ("no_delivery", False),+ ("disabled_resend", False),+ ("help_instead", False),+ ],+)+async def test_external_otp_browser_only_a_pressed_resend_fetches_once(+ monkeypatch: pytest.MonkeyPatch, case: str, webhook_first: bool+) -> None:+ from playwright.async_api import async_playwright++ task, _ = _otp_task(webhook=webhook_first)+ steps = _many_steps(task, 10)+ monkeypatch.setattr(settings, "BROWSER_LOADING_TIMEOUT_MS", 1500)+ async with async_playwright() as playwright:+ browser, live_page = await _launch_otp_page(playwright, resendDisabled=case == "disabled_resend")+ try:+ site = _OtpSite(monkeypatch, live_page, task, [_CODE])+ with skyvern_context.scoped(site.context):+ await site.step(steps[0], entry=False)+ await live_page.wait_for_function("() => window.__submits === 1")+ if case == "new_code":+ site.deliveries.append(_NEW_CODE)+ elif case == "no_delivery":+ site.deliveries.clear()+ fetches = _fetched(site)+ await site.step(steps[1], [_click("help" if case == "help_instead" else "resend")])+ assert await site.count("resends") == (1 if case in ("new_code", "old_code_only", "no_delivery") else 0)+ await site.step(steps[2])+ if case == "new_code":+ # The fetched code is new, so it is entered once; the old one never again.+ await live_page.wait_for_function("() => window.__submits === 2")+ assert await site.count("fullEntries") == 2 and _fetched(site) == fetches + 1+ return+ planned = await _run_until_terminal(site, steps[3:], budget_s=1.5)+ assert _terminal_codes(planned) == ["OTP_OUTCOME_UNCONFIRMED"]+ fetched = _fetched(site) > fetches+ assert fetched is (case in ("old_code_only", "no_delivery"))+ assert ("new one-time code was requested" in planned[0].reasoning) is fetched+ assert await site.count("fullEntries") == 1 and site.next_step is None+ finally:+ await browser.close()+++@_skip_no_browser[email protected]("later", ["remount_with_help_while_hidden", "new_task_same_code", "seed_totp_factor"])+async def test_external_otp_browser_remount_new_task_and_seed(monkeypatch: pytest.MonkeyPatch, later: str) -> None:+ from playwright.async_api import async_playwright++ task, _ = _otp_task()+ steps = _many_steps(task, 6)+ monkeypatch.setattr(settings, "BROWSER_LOADING_TIMEOUT_MS", 1500)+ cfg: dict[str, Any] = {"hideAfterSubmit": True} if later == "remount_with_help_while_hidden" else {}+ async with async_playwright() as playwright:+ browser, live_page = await _launch_otp_page(playwright, **cfg)+ try:+ site = _OtpSite(monkeypatch, live_page, task, [_CODE])+ with skyvern_context.scoped(site.context):+ await site.step(steps[0], entry=False)+ await live_page.wait_for_function("() => window.__submits === 1")+ if later == "remount_with_help_while_hidden":+ await live_page.evaluate("() => document.querySelector('#help').style.display = 'none'")+ await live_page.evaluate(+ "() => document.body.insertAdjacentHTML('beforeend', "+ '\'<button unique_id="help2" id="help2">Help</button>\')'+ )+ await site.step(steps[1], [_click("help2")], entry=False, code_field=False)+ await live_page.evaluate(+ "() => { const f = document.querySelector('#otp-scope'); f.style.display = ''; "+ "f.querySelectorAll('input[inputmode]').forEach(b => b.replaceWith(b.cloneNode())); }"+ )+ planned = await _run_until_terminal(site, steps[2:], budget_s=1.5)+ assert _terminal_codes(planned) == ["OTP_OUTCOME_UNCONFIRMED"]+ assert await _counts(site) == (1, 0, 1)+ elif later == "seed_totp_factor":+ await _open_routed_otp_page(live_page, "/authenticator", autoSubmit=False, clear=False)+ site.context.multi_field_totp[task.task_id] = MultiFieldTotpAttempt(+ [f"box-{i}" for i in range(6)], 6, "secret", hint_code=_HINT+ )+ site.context.totp_codes[f"{task.task_id}_secret"] = _SEED+ await site.step(steps[1], [_click("verify")])+ assert await site.count("fullEntries") == 1 and await site.count("verifyClicks") == 1+ assert site.final_status is None and site.next_step is not None+ else:+ # A new task (a new block) has its own record: the same code from the source is usable there.+ now = datetime.now(UTC)+ next_task = make_task(now, make_organization(now), task_id="task-next", url="about:blank")+ next_task.totp_identifier = task.totp_identifier+ await _open_routed_otp_page(live_page, "/next-block")+ next_site = _OtpSite(monkeypatch, live_page, next_task, [_CODE])+ with skyvern_context.scoped(next_site.context):+ next_step = make_step(+ now, next_task, step_id="next-0", status=StepStatus.created, order=0, output=None+ )+ await next_site.step(next_step, entry=False)+ await live_page.wait_for_function("() => window.__submits === 1")+ assert await next_site.count("fullEntries") == 1+ finally:+ await browser.close()+++@_skip_no_browser+async def test_external_otp_browser_cancelled_resend_restores_context_and_keeps_its_fact(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ from playwright.async_api import async_playwright++ task, steps = _otp_task()++ async def cancelled_press(*_args, **_kwargs):+ handler.otp_dispatch_effect.set("dispatched")+ raise asyncio.CancelledError++ async with async_playwright() as playwright:+ browser, live_page = await _launch_otp_page(playwright, autoSubmit=False, clear=False)+ try:+ site = _OtpSite(monkeypatch, live_page, task, [_CODE])+ with skyvern_context.scoped(site.context):+ await site.step(steps[0], entry=False)+ monkeypatch.setattr(handler.ActionHandler, "handle_action", cancelled_press)+ with pytest.raises(asyncio.CancelledError):+ await site.execute(steps[1], [ClickAction(element_id="resend")])+ assert handler.otp_single_dispatch.get() is False and handler.otp_dispatch_effect.get() is None+ assert site.context.external_otp_uses[task.task_id].resend_requested_at is not None+ finally:+ await browser.close()+++@_skip_no_browser+ ("source", "channel"),+ [+ ("inbox", "email"),+ ("inbox_after_empty_webhook", "email"),+ ("webhook", "webhook"),+ ("webhook_with_inbox_configured", "webhook"),+ ("stored_row", "db"),+ ("payload", "payload"),+ ],+)+async def test_external_otp_browser_every_supplied_code_is_entered_once(+ monkeypatch: pytest.MonkeyPatch, source: str, channel: str+) -> None:+ from playwright.async_api import async_playwright++ task, _ = _otp_task(webhook="webhook" in source, inbox=source not in ("webhook", "payload"))+ if source == "payload":+ task.navigation_payload = {"otp_code": _CODE}+ steps = _many_steps(task, 8)+ monkeypatch.setattr(settings, "BROWSER_LOADING_TIMEOUT_MS", 1500)+ async with async_playwright() as playwright:+ browser, live_page = await _launch_otp_page(playwright, clear=True)+ try:+ site = _OtpSite(monkeypatch, live_page, task, [_CODE], channel=channel)+ # A payload code is armed when the step's prompt builds the navigation payload, and the planner types it.+ hydrate = source == "payload"+ with skyvern_context.scoped(site.context):+ await site.step(steps[0], entry=hydrate, hydrate=hydrate)+ await live_page.wait_for_function("() => window.__submits === 1")+ planned = await _run_until_terminal(site, steps[1:], budget_s=1.5, hydrate=hydrate)+ # Main types the silently cleared code on every step until the step cap (5 entries, 5 submits).+ observed = (await site.count("fullEntries"), await site.count("submits"))+ assert observed == (1, 1) and _fetched(site) == (0 if channel == "payload" else 1)+ assert _terminal_codes(planned) == ["OTP_OUTCOME_UNCONFIRMED"]+ assert site.final_status == agent_module.TaskStatus.terminated and site.next_step is None+ finally:+ await browser.close()+++@_skip_no_browser[email protected]("press", ["verify", "resend"])+async def test_external_otp_browser_press_on_a_control_remounted_earlier_in_the_batch_is_tracked(+ monkeypatch: pytest.MonkeyPatch, press: str+) -> None:+ from playwright.async_api import async_playwright++ task, _ = _otp_task()+ steps = _many_steps(task, 5)+ monkeypatch.setattr(settings, "BROWSER_LOADING_TIMEOUT_MS", 1500)+ resend = press == "resend"+ async with async_playwright() as playwright:+ browser, live_page = await _launch_otp_page(playwright, autoSubmit=resend, clear=resend, remountOnRemember=True)+ try:+ site = _OtpSite(monkeypatch, live_page, task, [_CODE])+ with skyvern_context.scoped(site.context):+ await site.step(steps[0], entry=False)+ if resend:+ await live_page.wait_for_function("() => window.__submits === 1")+ # One plan: ticking the checkbox re-renders the widget, so the queued press is remapped by the+ # executor's stale-target refresh before it runs.+ await site.step(steps[1], [_click("remember"), _click(press)], entry=False, code_field=False)+ use = site.context.external_otp_uses[task.task_id]+ if not resend:+ assert await site.count("verifyClicks") == 1+ assert use.current_code_hash in use.submitted_code_hashes+ await site.step(steps[2], [_click("verify")], entry=False, code_field=False)+ assert await site.count("verifyClicks") == 1+ else:+ assert await site.count("resends") == 1 and use.resend_requested_at is not None+ site.deliveries.append(_NEW_CODE)+ await site.step(steps[2])+ await live_page.wait_for_function("() => window.__submits === 2")+ # Exactly one new code is entered, once. (The remount also makes the next plan re-resolve the+ # source, which returns that same new code.)+ assert await site.count("fullEntries") == 2+ assert use.used_code_hashes == {+ hashlib.sha256(code.encode()).hexdigest() for code in (_CODE, _NEW_CODE)+ }+ assert site.next_step is not None and site.final_status is None+ finally:+ await browser.close()+++async def test_external_otp_record_does_not_block_a_later_seed_totp(monkeypatch: pytest.MonkeyPatch) -> None:+ task, steps = _otp_task()+ attempt = MultiFieldTotpAttempt([f"box-{i}" for i in range(6)], 6, "secret", hint_code=_HINT)+ context = SkyvernContext(task_id=task.task_id, multi_field_totp={task.task_id: attempt})+ context.note_external_otp_input(task.task_id, hashlib.sha256(b"111111").hexdigest(), 6, 60)+ monkeypatch.setattr(handler, "_resolve_multi_field_totp_code", AsyncMock(return_value="111111"))+ fill = AsyncMock(return_value=ActionSuccess(data={"totp_group_filled": True}))+ monkeypatch.setattr(handler, "_fill_multi_field_totp_group", fill)+ action = InputTextAction(element_id="box-0", text=_HINT, totp_timing_info={"is_totp_sequence": True})+ with skyvern_context.scoped(context):+ [result] = await handler._handle_input_text_action(action, SimpleNamespace(), _box_page(6), task, steps[0])+ assert result.success+ fill.assert_awaited_once()+++class _FreshTotpWindowClock:+ """The handler's clock, shifted so the current 30-second TOTP window has most of its time left."""++ def __init__(self) -> None:+ now = time.time()+ self.offset = now - (now // 30 * 30 + 2)++ def time(self) -> float:+ return time.time() - self.offset++ def __getattr__(self, name: str) -> Any:+ return getattr(time, name)+++@_skip_no_browser[email protected]("seed_code", ["generated", "cached"])+async def test_external_otp_browser_same_digit_seed_after_external_code_keeps_the_original_fill(+ monkeypatch: pytest.MonkeyPatch, seed_code: str+) -> None:+ import pyotp+ from playwright.async_api import async_playwright++ task, _ = _otp_task(webhook=True, inbox=False)+ task.workflow_run_id = "workflow"+ steps = _many_steps(task, 5)+ monkeypatch.setattr(settings, "BROWSER_LOADING_TIMEOUT_MS", 1500)+ clock = _FreshTotpWindowClock()+ monkeypatch.setattr(handler, "time", clock)+ # The webhook delivers exactly the digits the credential seed produces in this window.+ same_digits = pyotp.TOTP(_SEED).at(int(clock.time()))+ workflow = WorkflowRunContext("title", "workflow-id", "permanent-id", "workflow", None)+ workflow.parameters["credentials"] = CredentialParameter.model_construct()+ workflow.values["credentials"] = {"totp": "placeholder_seed1_totp"}+ workflow.secrets["placeholder_seed1_totp"] = BitwardenConstants.TOTP+ workflow.secrets["placeholder_seed1_totp_value"] = _SEED+ manager = WorkflowContextManager.__new__(WorkflowContextManager)+ manager.workflow_run_contexts = {}+ monkeypatch.setattr(agent_module.app, "WORKFLOW_CONTEXT_MANAGER", manager)+ async with async_playwright() as playwright:+ browser, live_page = await _launch_otp_page(playwright, clear=True)+ try:+ site = _OtpSite(monkeypatch, live_page, task, [same_digits], channel="webhook")+ with skyvern_context.scoped(site.context):+ await site.step(steps[0], entry=False)+ await live_page.wait_for_function("() => window.__submits === 1")+ external_use = deepcopy(site.context.external_otp_uses[task.task_id])+ # The agent typed the credential; hydration now arms its seed for the next code group.+ manager.workflow_run_contexts["workflow"] = workflow+ site.context.active_credential_parameter_key = "credentials"+ task.navigation_payload = {"credentials": {"totp": "placeholder_seed1_totp"}}+ # A page without a code group ends the external attempt; no code is resolved on the way.+ await live_page.set_content("<html><body>Signed in. Set up your authenticator next.</body></html>")+ await site.step(steps[1], [], entry=False, code_field=False, hydrate=True)+ assert task.task_id not in site.context.multi_field_totp+ next_step = steps[2]+ if seed_code == "cached":+ await _open_routed_otp_page(live_page, "/authenticator", autoSubmit=False, clear=False)+ await site.step(steps[2], hydrate=True)+ assert await site.count("fullEntries") == 1+ assert site.context.totp_codes[f"{task.task_id}_totp_cache"] == same_digits+ next_step = steps[3]+ # This page drops part of a complete entry without submitting, so the keystream read-back fails.+ await _open_routed_otp_page(+ live_page, "/authenticator", autoSubmit=False, clear=False, dropLastDigitWhenFull=True+ )+ planned = await site.step(next_step, hydrate=True)+ attempt = site.context.multi_field_totp[task.task_id]+ # The original seed handling: the boxes are refilled one by one after the failed read-back.+ assert await site.count("fullEntries") == 2+ assert attempt.code_source == "secret"+ assert [action.action_type for action in planned].count(ActionType.INPUT_TEXT) == 6+ assert site.next_step is not None and site.final_status is None+ # The external record is left as it was: no seed entry, deadline or terminal is added to it.+ assert site.context.external_otp_uses[task.task_id] == external_use+ finally:+ await browser.close()