Skip to content

Automate browser based workflows with AI

Inherit block TOTP config when a static-script run signature omits it (SKY-15221) (#8371)

Shuchang Zheng0acca7abe018
diff --git a/skyvern/services/script_service.py b/skyvern/services/script_service.py
index 44fba37ff..93c25f6b9 100644
--- a/skyvern/services/script_service.py
+++ b/skyvern/services/script_service.py
@@ -2056,6 +2056,75 @@ async def _generate_block_code_from_task(
return ""
+def _render_block_otp_value(raw_value: str | None, label: str | None) -> str | None:
+ """Rendered OTP identifier/URL for a script block, or None when it cannot be resolved.
+ An unrendered template must never reach the totp_codes lookup — it would match
+ nothing for the entire poll and the run would fail as "no code found"."""
+ if not raw_value:
+ return None
+ rendered = raw_value
+ if "{{" in raw_value:
+ try:
+ rendered = _render_template_with_label(raw_value, label)
+ except Exception:
+ LOG.warning("Failed to render block OTP template", label=label, exc_info=True)
+ return None
+ rendered = rendered.strip()
+ if not rendered or "{{" in rendered:
+ return None
+ return rendered
+
+
+def _find_block_definition(blocks: list[Any], label: str) -> Any | None:
+ stack = list(blocks)
+ while stack:
+ block = stack.pop()
+ if getattr(block, "label", None) == label:
+ return block
+ stack.extend(getattr(block, "loop_blocks", None) or [])
+ return None
+
+
+async def _resolve_block_otp_config(
+ label: str | None,
+ totp_identifier: str | None,
+ totp_url: str | None,
+) -> tuple[str | None, str | None]:
+ """OTP config for a script block, inherited from the workflow definition when the
+ call site carries none. A synthesized run_signature (static-script pinning) omits
+ the block's totp fields, which silently strips 2FA polling from the whole block —
+ the workflow definition still knows them, so resolve from there by label."""
+ if totp_identifier or totp_url:
+ return _render_block_otp_value(totp_identifier, label), _render_block_otp_value(totp_url, label)
+ if not label:
+ return None, None
+ context = skyvern_context.current()
+ if not context or not context.workflow_id or not context.organization_id:
+ return None, None
+ try:
+ workflow = await app.DATABASE.workflows.get_workflow(
+ workflow_id=context.workflow_id, organization_id=context.organization_id
+ )
+ except Exception:
+ LOG.warning("Failed to load workflow for block OTP inheritance", label=label, exc_info=True)
+ return None, None
+ if not workflow:
+ return None, None
+ block = _find_block_definition(workflow.workflow_definition.blocks, label)
+ if block is None:
+ return None, None
+ inherited_identifier = _render_block_otp_value(getattr(block, "totp_identifier", None), label)
+ inherited_url = _render_block_otp_value(getattr(block, "totp_verification_url", None), label)
+ if inherited_identifier or inherited_url:
+ LOG.info(
+ "Inherited OTP config from workflow block definition",
+ label=label,
+ has_totp_identifier=bool(inherited_identifier),
+ has_totp_verification_url=bool(inherited_url),
+ )
+ return inherited_identifier, inherited_url
+
+
async def run_task(
prompt: str,
url: str | None = None,
@@ -2078,6 +2147,7 @@ async def run_task(
# Use `label` (the workflow block label) for the block run so the
# framework can match it, and `cache_key` to look up the cached function.
block_label = label or cache_key
+ totp_identifier, totp_url = await _resolve_block_otp_config(block_label, totp_identifier, totp_url)
workflow_run_block_id, task_id, step_id = await _create_workflow_block_run_and_task(
block_type=BlockType.NAVIGATION,
prompt=prompt,
diff --git a/tests/unit/test_script_service_fallback_engine.py b/tests/unit/test_script_service_fallback_engine.py
index 5c74698b6..6ee43476e 100644
--- a/tests/unit/test_script_service_fallback_engine.py
+++ b/tests/unit/test_script_service_fallback_engine.py
@@ -192,6 +192,92 @@ def test_resolver_finds_loop_nested_block_engine() -> None:
assert script_service._resolve_original_block_engine("missing", workflow) is None
+def _make_run_context(values: dict[str, object]) -> MagicMock:
+ workflow_run_context = MagicMock()
+ workflow_run_context.values = dict(values)
+ workflow_run_context.get_block_metadata.return_value = {}
+ workflow_run_context.workflow_title = "test workflow"
+ workflow_run_context.workflow_id = "w_test"
+ workflow_run_context.workflow_permanent_id = "wpid_test"
+ workflow_run_context.workflow_run_id = "wr_test"
+ workflow_run_context.browser_session_id = None
+ return workflow_run_context
+
+
+async def _resolve_otp(
+ workflow: Workflow,
+ values: dict[str, object],
+ totp_identifier: str | None = None,
+ totp_url: str | None = None,
+) -> tuple[tuple[str | None, str | None], MagicMock]:
+ app = _make_app(workflow)
+ app.WORKFLOW_CONTEXT_MANAGER.get_workflow_run_context.return_value = _make_run_context(values)
+ with (
+ patch(f"{MODULE}.app", app),
+ patch(f"{MODULE}.skyvern_context.current", return_value=_make_context()),
+ ):
+ resolved = await script_service._resolve_block_otp_config("my_block", totp_identifier, totp_url)
+ return resolved, app
+
+
+async def test_otp_config_inherited_from_block_definition_when_call_site_omits_it() -> None:
+ # Static-script run signatures omit the block's totp fields (SKY-15221); the task the
+ # script path creates must still poll the identifier the workflow block configured.
+ block = _make_task_block("my_block")
+ block.totp_identifier = "{{ email }}"
+ workflow = _make_workflow([block])
+
+ (identifier, url), _ = await _resolve_otp(workflow, {"email": "[email protected]"})
+
+ assert identifier == "[email protected]"
+ assert url is None
+
+
+async def test_unresolvable_otp_template_yields_none_not_the_literal() -> None:
+ # A literal "{{ email }}" as identifier would match nothing for the whole poll —
+ # worse than no identifier, because the failure reads as "code never arrived".
+ block = _make_task_block("my_block")
+ block.totp_identifier = "{{ email }}"
+ workflow = _make_workflow([block])
+
+ (identifier, _), _ = await _resolve_otp(workflow, {})
+
+ assert identifier is None
+
+
+async def test_call_site_otp_values_pass_through_without_workflow_lookup() -> None:
+ workflow = _make_workflow([_make_task_block("my_block")])
+
+ (identifier, url), app = await _resolve_otp(workflow, {}, totp_identifier="[email protected]")
+
+ assert identifier == "[email protected]"
+ assert url is None
+ app.DATABASE.workflows.get_workflow.assert_not_called()
+
+
+async def test_otp_inheritance_finds_loop_nested_block() -> None:
+ from skyvern.forge.sdk.workflow.models.block import ForLoopBlock
+
+ nested = _make_task_block("my_block")
+ nested.totp_identifier = "{{ email }}"
+ loop = ForLoopBlock(
+ label="outer_loop",
+ loop_blocks=[nested],
+ loop_over=None,
+ loop_variable_reference="items",
+ output_parameter=nested.output_parameter,
+ )
+ workflow = _make_workflow([loop])
+
+ (identifier, _), _ = await _resolve_otp(workflow, {"email": "[email protected]"})
+
+ assert identifier == "[email protected]"
+
+
@pytest.mark.asyncio
async def test_block_screenshot_without_browser_state_is_not_a_warning(monkeypatch: pytest.MonkeyPatch) -> None:
# A block that runs before a browser exists, or never needs one, has no screenshot to take;