Skip to content

Automate browser based workflows with AI

fix(SKY-9822): script generator emits credential TOTP lookup when block has no explicit totp_identifier (#5992)

pedrohsdbdf1d6ca35eb2
diff --git a/skyvern/core/script_generations/deterministic_field_naming.py b/skyvern/core/script_generations/deterministic_field_naming.py
index 57498fc50..7bacabfd2 100644
--- a/skyvern/core/script_generations/deterministic_field_naming.py
+++ b/skyvern/core/script_generations/deterministic_field_naming.py
@@ -336,6 +336,22 @@ def pick_field_names_for_actions(
return picks
+def pick_credential_root_for_block(
+ *,
+ goal_template: str,
+ credential_param_keys: frozenset[str],
+) -> str | None:
+ """Return the credential key for this block, or None if ambiguous."""
+ if not credential_param_keys:
+ return None
+ referenced = extract_jinja_root_names(goal_template) & credential_param_keys
+ if len(referenced) == 1:
+ return next(iter(referenced))
+ if not referenced and len(credential_param_keys) == 1:
+ return next(iter(credential_param_keys))
+ return None
+
+
def infer_credential_subscript_for_emit(
*,
action: dict[str, Any],
diff --git a/skyvern/core/script_generations/generate_script.py b/skyvern/core/script_generations/generate_script.py
index 637ade558..ea15fdb00 100644
--- a/skyvern/core/script_generations/generate_script.py
+++ b/skyvern/core/script_generations/generate_script.py
@@ -21,7 +21,10 @@ from libcst import Attribute, Call, Dict, DictElement, FunctionDef, Name, Param
from skyvern.config import settings
from skyvern.core.script_generations.constants import SCRIPT_TASK_BLOCKS, SCRIPT_TASK_BLOCKS_WITH_COMPLETE_ACTION
-from skyvern.core.script_generations.deterministic_field_naming import infer_credential_subscript_for_emit
+from skyvern.core.script_generations.deterministic_field_naming import (
+ infer_credential_subscript_for_emit,
+ pick_credential_root_for_block,
+)
from skyvern.core.script_generations.generate_workflow_parameters import (
CUSTOM_FIELD_ACTIONS,
generate_workflow_parameters_schema,
@@ -1032,6 +1035,37 @@ def _action_to_stmt(
),
)
)
+ elif (
+ not task.get("totp_verification_url")
+ and not (act.get("totp_timing_info") or {}).get("is_totp_sequence")
+ and (
+ credential_key := pick_credential_root_for_block(
+ goal_template=goal_template,
+ credential_param_keys=credential_param_keys,
+ )
+ )
+ ):
+ # Mirrors BaseTaskBlock.format_potential_template_parameters: emit a
+ # runtime credential lookup so the cached script resolves the
+ # totp_identifier per run instead of hard-coding it. Skipped for
+ # is_totp_sequence — runtime poll would overwrite each single-digit
+ # input with the full code.
+ args.append(
+ cst.Arg(
+ keyword=cst.Name("totp_identifier"),
+ value=cst.Call(
+ func=cst.Attribute(
+ value=cst.Name("context"),
+ attr=cst.Name("credential_totp_identifier"),
+ ),
+ args=[cst.Arg(value=_value(credential_key))],
+ ),
+ whitespace_after_arg=cst.ParenthesizedWhitespace(
+ indent=True,
+ last_line=cst.SimpleWhitespace(INDENT),
+ ),
+ )
+ )
if task.get("totp_verification_url"):
args.append(
cst.Arg(
@@ -1396,6 +1430,15 @@ def _build_block_fn(
cache_key = block.get("label") or block.get("title") or f"block_{block.get('workflow_run_block_id')}"
body_stmts: list[cst.BaseStatement] = []
+ # Restrict credentials to those bound to THIS block's parameters — matches
+ # BaseTaskBlock.get_all_parameters (block.py:872-883), which returns the
+ # block's own `self.parameters`. Workflow-wide keys would conflate
+ # credentials from unrelated blocks in multi-login flows.
+ block_parameter_keys: frozenset[str] = frozenset(
+ p["key"] for p in block.get("parameters") or [] if isinstance(p, dict) and isinstance(p.get("key"), str)
+ )
+ block_credential_param_keys = credential_param_keys & block_parameter_keys
+
# Detect and annotate multi-field TOTP sequences so each fill gets the correct digit index
actions = _annotate_multi_field_totp_sequence(actions)
@@ -1440,7 +1483,7 @@ def _build_block_fn(
use_semantic_selectors=use_semantic_selectors,
block_type=block_type,
goal_template=block.get("navigation_goal") or block.get("data_extraction_goal") or "",
- credential_param_keys=credential_param_keys,
+ credential_param_keys=block_credential_param_keys,
)
)
diff --git a/skyvern/core/script_generations/skyvern_page.py b/skyvern/core/script_generations/skyvern_page.py
index 9e52df448..5b98af85a 100644
--- a/skyvern/core/script_generations/skyvern_page.py
+++ b/skyvern/core/script_generations/skyvern_page.py
@@ -3575,6 +3575,16 @@ class RunContext:
return ctx.loop_metadata.get("current_value")
return None
+ def credential_totp_identifier(self, credential_key: str) -> str | None:
+ """Look up the TOTP identifier registered for a workflow credential parameter."""
+ ctx = skyvern_context.current()
+ if not ctx or not ctx.workflow_run_id:
+ return None
+ workflow_run_context = app.WORKFLOW_CONTEXT_MANAGER.workflow_run_contexts.get(ctx.workflow_run_id)
+ if workflow_run_context is None:
+ return None
+ return workflow_run_context.credential_totp_identifiers.get(credential_key)
+
def loop_item_selector(self) -> str | None:
"""Build a CSS selector to click the current loop item's link on the page.
diff --git a/tests/unit/test_action_to_stmt_credential_totp_inheritance.py b/tests/unit/test_action_to_stmt_credential_totp_inheritance.py
new file mode 100644
index 000000000..829c44778
--- /dev/null
+++ b/tests/unit/test_action_to_stmt_credential_totp_inheritance.py
@@ -0,0 +1,278 @@
+"""SKY-9822 regression tests: generator emits a runtime credential TOTP lookup
+when the block has no explicit totp_identifier but a credential is in scope."""
+
+from __future__ import annotations
+
+from typing import Any
+from unittest.mock import MagicMock, patch
+
+import libcst as cst
+
+from skyvern.core.script_generations.generate_script import _action_to_stmt, _build_block_fn
+
+
+def _render(stmt: cst.BaseStatement) -> str:
+ return cst.Module(body=[stmt]).code
+
+
+def _render_fn(fn: cst.FunctionDef) -> str:
+ return cst.Module(body=[fn]).code
+
+
+def _make_input_action(*, totp_code_required: bool = True) -> dict[str, Any]:
+ return {
+ "action_type": "input_text",
+ "xpath": "//input[@id='InputOTPCode']",
+ "text": "476981",
+ "totp_code_required": totp_code_required,
+ "css_selector": "#InputOTPCode",
+ "attributes": {"id": "InputOTPCode"},
+ }
+
+
+class TestActionToStmtCredentialTOTPInheritance:
+ def test_emits_credential_totp_call_when_block_has_no_identifier_and_one_credential(self) -> None:
+ # Workflow definition's OTP-entry block has no explicit totp_identifier,
+ # but the run has a single credential parameter whose underlying
+ # credential carries a TOTP identifier. Generator must emit a runtime
+ # lookup against that credential rather than skipping.
+ act = _make_input_action()
+ task: dict[str, Any] = {} # block-level totp_identifier / totp_verification_url absent
+
+ stmt = _action_to_stmt(
+ act,
+ task,
+ use_semantic_selectors=True,
+ credential_param_keys=frozenset({"SiteCredentials"}),
+ )
+ code = _render(stmt)
+
+ assert "totp_identifier" in code, f"totp_identifier kwarg missing:\n{code}"
+ assert "context.credential_totp_identifier" in code, f"credential lookup call missing:\n{code}"
+ assert "'SiteCredentials'" in code, f"credential key missing:\n{code}"
+
+ def test_no_emission_when_zero_credentials(self) -> None:
+ # No credential to look up — generator should not invent a call. Same
+ # as today's behavior; preserves backcompat for workflows without
+ # credential params.
+ act = _make_input_action()
+ task: dict[str, Any] = {}
+
+ stmt = _action_to_stmt(
+ act,
+ task,
+ use_semantic_selectors=True,
+ credential_param_keys=frozenset(),
+ )
+ code = _render(stmt)
+
+ assert "credential_totp_identifier" not in code, (
+ f"unexpected credential lookup emitted with no credentials:\n{code}"
+ )
+
+ def test_no_emission_when_multiple_credentials_no_jinja_reference(self) -> None:
+ # Multiple credentials AND no Jinja reference in the goal_template to
+ # disambiguate — skip the emit so we don't pick a credential
+ # arbitrarily. The customer can disambiguate by setting
+ # `totp_identifier=` explicitly on the block. Mirrors the agentic
+ # safety in `try_generate_totp_from_credential`'s multi-no-active
+ # branch.
+ act = _make_input_action()
+ task: dict[str, Any] = {}
+
+ stmt = _action_to_stmt(
+ act,
+ task,
+ use_semantic_selectors=True,
+ credential_param_keys=frozenset({"CredA", "CredB"}),
+ )
+ code = _render(stmt)
+
+ assert "credential_totp_identifier" not in code, (
+ f"unexpected credential lookup emitted with multi-credential ambiguity:\n{code}"
+ )
+
+ def test_picks_correct_credential_when_block_goal_template_jinja_references_one(self) -> None:
+ # Real-world shape: workflow has two login blocks each consuming a
+ # different credential. The block's goal_template references THIS
+ # block's credential via `{{Cred1}}`, so we disambiguate by the
+ # Jinja root referenced in the goal. Without this, multi-login
+ # workflows skip emission entirely.
+ act = _make_input_action()
+ task: dict[str, Any] = {}
+
+ stmt = _action_to_stmt(
+ act,
+ task,
+ use_semantic_selectors=True,
+ goal_template="Enter the 2FA code.\n{'creds': '{{Cred1}}'}",
+ credential_param_keys=frozenset({"Cred1", "Cred2"}),
+ )
+ code = _render(stmt)
+
+ assert "context.credential_totp_identifier" in code, (
+ f"credential lookup missing when Jinja reference disambiguates:\n{code}"
+ )
+ assert "'Cred1'" in code, f"wrong credential key chosen:\n{code}"
+ assert "'Cred2'" not in code, f"unreferenced credential key emitted:\n{code}"
+
+ def test_no_emission_when_multiple_credentials_and_both_jinja_referenced(self) -> None:
+ # Both credentials referenced in the goal — still ambiguous, refuse
+ # to pick. Customer has set up a flow where one block touches both
+ # credentials; can't safely guess which one owns the OTP.
+ act = _make_input_action()
+ task: dict[str, Any] = {}
+
+ stmt = _action_to_stmt(
+ act,
+ task,
+ use_semantic_selectors=True,
+ goal_template="Use {{CredA}} or {{CredB}} to log in.",
+ credential_param_keys=frozenset({"CredA", "CredB"}),
+ )
+ code = _render(stmt)
+
+ assert "credential_totp_identifier" not in code, (
+ f"unexpected credential lookup with both-referenced ambiguity:\n{code}"
+ )
+
+ def test_block_level_identifier_takes_precedence(self) -> None:
+ # If the customer DID set totp_identifier on the block (e.g. a
+ # parameter reference like 'email'), keep emitting that literal. The
+ # credential auto-resolve is a fallback for blocks the customer didn't
+ # explicitly configure — it must not override authored config.
+ act = _make_input_action()
+ task = {"totp_identifier": "email"}
+
+ stmt = _action_to_stmt(
+ act,
+ task,
+ use_semantic_selectors=True,
+ credential_param_keys=frozenset({"SiteCredentials"}),
+ )
+ code = _render(stmt)
+
+ # CST emits kwargs as `name = value` (with surrounding spaces). Look for
+ # the identifier kwarg and the literal value rather than a glued form.
+ assert "totp_identifier" in code and "'email'" in code, (
+ f"customer's authored block-level identifier should be preserved:\n{code}"
+ )
+ assert "credential_totp_identifier" not in code, (
+ f"credential lookup must not override block-level identifier:\n{code}"
+ )
+
+ def test_url_only_block_does_not_get_credential_emission(self) -> None:
+ # Customer configured totp_verification_url but not totp_identifier.
+ # The URL alone is sufficient — runtime polls via URL. Don't also emit
+ # a credential identifier, which would compete.
+ act = _make_input_action()
+ task = {"totp_verification_url": "https://example.invalid/totp"}
+
+ stmt = _action_to_stmt(
+ act,
+ task,
+ use_semantic_selectors=True,
+ credential_param_keys=frozenset({"SiteCredentials"}),
+ )
+ code = _render(stmt)
+
+ assert "totp_url" in code, f"existing totp_url emission must survive:\n{code}"
+ assert "credential_totp_identifier" not in code, (
+ f"credential lookup must not be added when URL is configured:\n{code}"
+ )
+
+ def test_no_emission_when_totp_code_not_required(self) -> None:
+ # The whole emission block is gated on totp_code_required=True. A
+ # regular username/password fill with a credential param in scope
+ # must NOT get a TOTP identifier injected.
+ act = _make_input_action(totp_code_required=False)
+ task: dict[str, Any] = {}
+
+ stmt = _action_to_stmt(
+ act,
+ task,
+ use_semantic_selectors=True,
+ credential_param_keys=frozenset({"SiteCredentials"}),
+ )
+ code = _render(stmt)
+
+ assert "totp_identifier" not in code, f"non-TOTP fill got TOTP identifier injected:\n{code}"
+
+ def test_per_block_filtering_picks_block_bound_credential_without_jinja(self) -> None:
+ # Multi-login workflow: two task blocks, each binds ONE credential via
+ # block.parameters but neither references it in goal_template. Workflow-
+ # wide credential_param_keys = {Cred1, Cred2} would skip emission; per-
+ # block filtering selects each block's own credential. Mirrors the
+ # agentic walk over `block.get_all_parameters(...)`.
+ block = {
+ "label": "login_a",
+ "block_type": "task",
+ "navigation_goal": "Enter the 2FA code.",
+ "parameters": [{"key": "Cred1"}],
+ }
+ actions = [_make_input_action()]
+
+ with patch("skyvern.core.script_generations.generate_script.app") as mock_app:
+ mock_app.AGENT_FUNCTION.build_ats_pipeline_block_fn = MagicMock(return_value=None)
+ fn = _build_block_fn(
+ block=block,
+ actions=actions,
+ use_semantic_selectors=True,
+ credential_param_keys=frozenset({"Cred1", "Cred2"}),
+ )
+
+ code = _render_fn(fn)
+ assert "context.credential_totp_identifier" in code, (
+ f"per-block-bound credential should drive emission:\n{code}"
+ )
+ assert "'Cred1'" in code, f"block's bound credential 'Cred1' missing:\n{code}"
+ assert "'Cred2'" not in code, f"unrelated workflow credential 'Cred2' must not leak:\n{code}"
+
+ def test_per_block_filtering_skips_when_block_binds_no_credential(self) -> None:
+ # Block has no credential parameter bound — matches the agentic case
+ # where get_all_parameters returns no credential, so nothing inherits.
+ # Generator must skip emission even if other blocks in the workflow
+ # bind credentials.
+ block = {
+ "label": "otp_only",
+ "block_type": "task",
+ "navigation_goal": "Enter the 2FA code.",
+ "parameters": [{"key": "SomeOtherParam"}],
+ }
+ actions = [_make_input_action()]
+
+ with patch("skyvern.core.script_generations.generate_script.app") as mock_app:
+ mock_app.AGENT_FUNCTION.build_ats_pipeline_block_fn = MagicMock(return_value=None)
+ fn = _build_block_fn(
+ block=block,
+ actions=actions,
+ use_semantic_selectors=True,
+ credential_param_keys=frozenset({"Cred1"}),
+ )
+
+ code = _render_fn(fn)
+ assert "credential_totp_identifier" not in code, (
+ f"block with no credential binding must not inherit from workflow scope:\n{code}"
+ )
+
+ def test_no_emission_when_is_totp_sequence(self) -> None:
+ # Multi-field OTP forms split the 6-digit code across single-digit
+ # inputs. Each fill receives one digit via get_totp_digit. Emitting a
+ # totp_identifier on these fills triggers the runtime poll path, which
+ # overwrites the single-digit value with the full code — corrupting
+ # every input. Skip emission for is_totp_sequence actions.
+ act = _make_input_action()
+ act["totp_timing_info"] = {"is_totp_sequence": True, "action_index": 0}
+ task: dict[str, Any] = {}
+
+ stmt = _action_to_stmt(
+ act,
+ task,
+ use_semantic_selectors=True,
+ credential_param_keys=frozenset({"SiteCredentials"}),
+ )
+ code = _render(stmt)
+
+ assert "credential_totp_identifier" not in code, (
+ f"is_totp_sequence fills must not get credential lookup (would overwrite single-digit value):\n{code}"
+ )