Skip to content

Automate browser based workflows with AI

198 KB
Raw Blame
"""Tests for CodeBlock sandbox security hardening (SKY-7897).

Verifies that the CodeBlock safety layer:
- Rejects dangerous code patterns (subprocess, network, sandbox-escape, imports, dunder access)
- Accepts legitimate code patterns (math, strings, json, regex, sleep)
- Exposes the correct safe variables (no asyncio, yes sleep)
"""

import asyncio
import inspect
import json
import operator
import re
import warnings
from collections.abc import AsyncIterator, Callable, Mapping
from contextlib import asynccontextmanager, nullcontext
from datetime import UTC
from datetime import date as stdlib_date
from datetime import datetime
from datetime import time as stdlib_time
from datetime import timedelta, timezone
from functools import partial
from types import FunctionType, SimpleNamespace
from typing import Any, NoReturn
from unittest.mock import AsyncMock, MagicMock

import pyotp.totp as pyotp_totp
import pytest
from playwright.async_api import Error as PlaywrightError
from playwright.async_api import Page
from playwright.async_api import TimeoutError as PlaywrightTimeoutError
from structlog.testing import capture_logs

import skyvern.forge.sdk.copilot.code_block_security as code_block_security
import skyvern.forge.sdk.workflow.models.block as block_module
from skyvern.config import settings
from skyvern.forge.sdk.copilot.code_block_security import rendering_introduced_security_errors
from skyvern.forge.sdk.schemas.totp_codes import OTPType
from skyvern.forge.sdk.workflow.code_block_safety import (
    ALWAYS_DENIED_BUILTINS,
    SANDBOX_ONLY_BUILTINS,
    is_safe_script_code,
    safe_builtins,
)
from skyvern.forge.sdk.workflow.exceptions import (
    FailedToFormatJinjaStyleParameter,
    InsecureCodeDetected,
    MissingJinjaVariables,
)
from skyvern.forge.sdk.workflow.models.block import (
    CODE_BLOCK_TAB_OPEN_FAILURE_REASON,
    BranchEvaluationContext,
    CodeBlock,
    CodeBlockOTPError,
    _bind_code_block_open_page,
    _bind_code_block_set_dialog_policy,
    _close_opened_code_block_pages,
    _resolve_code_block_otp,
    _resolve_code_block_otp_for_identifier,
    dispose_opened_code_block_pages,
)
from skyvern.forge.sdk.workflow.models.code_block_recorder import RecordingPage
from skyvern.forge.sdk.workflow.models.credential_release import (
    CodeBlockCredentialReleaseError,
    CredentialReleaseGuard,
)
from skyvern.forge.sdk.workflow.models.parameter import (
    CredentialParameter,
    OutputParameter,
    ParameterType,
    WorkflowParameter,
    WorkflowParameterType,
)
from skyvern.schemas.workflows import BlockStatus
from skyvern.services.otp_email import (
    MAX_SEEN_EMAIL_MESSAGE_IDS,
    EmailOTPVerificationContext,
    GmailOTPSource,
    OutlookOTPSource,
)
from skyvern.services.otp_service import RawOTPVerificationContext
from skyvern.webeye import dialog_handler
from skyvern.webeye.actions.action_types import ActionType
from skyvern.webeye.browser_artifacts import BrowserArtifacts
from skyvern.webeye.skycdp.errors import CdpError
from tests.unit._task_block_fakes import run_bound_browser_state
from tests.unit.conftest import (
    FakeClearingBrowserContext,
    FakeSearchBrowserContext,
    FakeSearchPage,
    arm_search_api,
    serpapi_page,
)
from tests.unit.fake_workflow_run_context import FakeWorkflowRunContext

RAW_DATETIME_TYPES = (stdlib_date, datetime, stdlib_time)
FACADE_MEMBERS = {"datetime", "date", "timedelta", "timezone", "UTC"}

_DATE_PROBE_ARGS: dict[str, tuple[Any, ...]] = {
    "__format__": ("%m/%d/%Y",),
    "ctime": (),
    "fromisocalendar": (2026, 38, 1),
    "fromisoformat": ("2026-09-13",),
    "fromordinal": (739872,),
    "fromtimestamp": (1789603200,),
    "isocalendar": (),
    "isoformat": (),
    "isoweekday": (),
    "replace": (),
    "strftime": ("%m/%d/%Y",),
    "strptime": ("2026-09-13", "%Y-%m-%d"),
    "timetuple": (),
    "today": (),
    "toordinal": (),
    "weekday": (),
}
_DATETIME_PROBE_ARGS: dict[str, tuple[Any, ...]] = _DATE_PROBE_ARGS | {
    "astimezone": (UTC,),
    "combine": (stdlib_date(2026, 9, 13), stdlib_time(4, 5, 6)),
    "date": (),
    "dst": (),
    "fromisoformat": ("2026-09-13T04:05:06",),
    "now": (),
    "time": (),
    "timestamp": (),
    "timetz": (),
    "tzname": (),
    "utcfromtimestamp": (1789603200,),
    "utcnow": (),
    "utcoffset": (),
    "utctimetuple": (),
}
_TIME_PROBE_ARGS: dict[str, tuple[Any, ...]] = {
    "__format__": ("%H:%M",),
    "dst": (),
    "fromisoformat": ("04:05:06",),
    "isoformat": (),
    "replace": (),
    "strftime": ("%H:%M",),
    "strptime": ("04:05", "%H:%M"),
    "tzname": (),
    "utcoffset": (),
}


class NoPageOperationPage:
    def __getattr__(self, name: str) -> NoReturn:
        raise AssertionError(f"a date read must not reach the page: {name}")


def _call_from_sandbox_frame(member: Callable[..., Any], args: tuple[Any, ...]) -> Any:
    return member(*args)


def _accepts(member: Callable[..., Any], args: tuple[Any, ...], kwargs: dict[str, Any]) -> bool:
    try:
        member(*args, **kwargs)
    except TypeError:
        return False
    except Exception:
        return True
    return True


def _delegate_parameter_kind_defects(
    label: str,
    instance: stdlib_date | stdlib_time,
    stdlib_type: type,
    probes: Mapping[str, tuple[Any, ...]],
) -> list[str]:
    """A delegate bound in place of a C stdlib method must accept the same arguments by keyword as
    the method it shadows, or already-deployed blocks passing that argument by name start failing."""
    defects: list[str] = []
    sandbox_type = type(instance)
    for name in sorted(vars(sandbox_type)):
        if name.startswith("_"):
            continue
        sandbox_member = getattr(sandbox_type, name)
        if not inspect.isroutine(sandbox_member):
            continue
        stdlib_member = getattr(stdlib_type, name, None)
        if stdlib_member is None:
            defects.append(f"{label}.{name} shadows nothing on {stdlib_type.__name__}")
            continue
        parameters = [
            parameter for parameter in inspect.signature(sandbox_member).parameters if parameter not in ("self", "cls")
        ]
        keywords = dict(zip(parameters, probes.get(name, ())))
        if len(keywords) != len(parameters):
            defects.append(f"{label}.{name} has no probe arguments")
            continue
        arguments = () if inspect.ismethod(sandbox_member) else (instance,)
        if _accepts(sandbox_member, arguments, keywords) != _accepts(stdlib_member, arguments, keywords):
            defects.append(f"{label}.{name} does not accept the same keyword arguments as {stdlib_type.__name__}")
    return defects


def datetime_facade_defects(namespace: SimpleNamespace, sandbox_builtins: Mapping[str, Any]) -> list[str]:
    """Exercise every public member of a CodeBlock datetime facade from a frame carrying the sandbox
    builtins, where a member reaching CPython's hidden import raises and a raw C return re-breaks it."""
    call = FunctionType(_call_from_sandbox_frame.__code__, {"__builtins__": sandbox_builtins})
    defects: list[str] = []
    if set(vars(namespace)) != FACADE_MEMBERS:
        defects.append(f"facade members are {sorted(vars(namespace))}")
    if not issubclass(namespace.datetime, namespace.date):
        defects.append("datetime is not a subclass of date")
    if not isinstance(call(namespace.datetime.now, ()), namespace.date):
        defects.append("datetime.now() is not an instance of date")
    # ``date`` precedes ``datetime`` in the facade MRO, so an unmirrored date delegate shadows datetime's.
    unmirrored = {name for name in vars(namespace.date) if not name.startswith("_")} - set(vars(namespace.datetime))
    if unmirrored:
        defects.append(f"date members not mirrored on datetime: {sorted(unmirrored)}")
    for label, instance, stdlib_type, probes in (
        ("date", namespace.date(2026, 9, 13), stdlib_date, _DATE_PROBE_ARGS),
        ("datetime", namespace.datetime(2026, 9, 13, 4, 5, 6, tzinfo=UTC), datetime, _DATETIME_PROBE_ARGS),
        ("time", namespace.datetime(2026, 9, 13, 4, 5, 6).time(), stdlib_time, _TIME_PROBE_ARGS),
    ):
        defects += _delegate_parameter_kind_defects(label, instance, stdlib_type, probes)
        try:
            call(partial(instance.strftime, format=probes["strftime"][0]), ())
        except Exception as error:
            defects.append(f"{label}.strftime(format=) raised {type(error).__name__}: {error}")
        for name in sorted(name for name in dir(instance) if not name.startswith("_") or name == "__format__"):
            member = getattr(instance, name)
            if not callable(member):
                value = member
            elif name not in probes:
                defects.append(f"{label}.{name} has no probe arguments")
                continue
            else:
                try:
                    # utcnow/utcfromtimestamp only reach the hidden import when their
                    # DeprecationWarning is actually emitted, which the default filter suppresses.
                    with warnings.catch_warnings():
                        warnings.simplefilter("always", DeprecationWarning)
                        value = call(member, probes[name])
                except Exception as error:
                    defects.append(f"{label}.{name} raised {type(error).__name__}: {error}")
                    continue
            if type(value) in RAW_DATETIME_TYPES:
                defects.append(f"{label}.{name} returned a raw {type(value).__name__}")
    for label, instance in (
        ("date", namespace.date(2026, 9, 13)),
        ("datetime", namespace.datetime(2026, 9, 13, 4, 5, 6)),
    ):
        for symbol, operation in (("+", operator.add), ("-", operator.sub)):
            try:
                shifted = call(operation, (instance, namespace.timedelta(days=1)))
                call(shifted.strftime, ("%m/%d/%Y",))
            except Exception as error:
                defects.append(f"{label} {symbol} timedelta raised {type(error).__name__}: {error}")
                continue
            if type(shifted) in RAW_DATETIME_TYPES:
                defects.append(f"{label} {symbol} timedelta returned a raw {type(shifted).__name__}")
    return defects


# ---------------------------------------------------------------------------
# is_safe_code — rejection tests
# ---------------------------------------------------------------------------


class TestIsSafeCodeImports:
    @pytest.mark.parametrize(
        "code",
        [
            "import os",
            "import subprocess",
            "from os import system",
            "import re, os",
            "import datetime.timezone",
            "from . import re",
            "from re import *",
            "from re import _compile",
            "import re as __builtins__",
            "from json import loads as __capture_locals",
        ],
    )
    def test_rejects_imports_beyond_the_sandbox_modules(self, code: str) -> None:
        with pytest.raises(InsecureCodeDetected, match="import|private"):
            CodeBlock.is_safe_code(code)

    @pytest.mark.parametrize(
        "code",
        [
            "import asyncio",
            "import re",
            "import json as j",
            "from datetime import date, timedelta",
            "from html import escape",
        ],
    )
    def test_admits_imports_of_the_sandbox_modules(self, code: str) -> None:
        CodeBlock.is_safe_code(code)

    @pytest.mark.asyncio
    async def test_an_admitted_import_binds_the_sandbox_shim(self) -> None:
        code = """
import re
import json as j
from datetime import date, timedelta

def next_day():
    import datetime
    return datetime.date(2026, 1, 1) + timedelta(days=1)

return [re.search("b", "abc").group(0), j.dumps([1]), date(2026, 1, 1).isoformat(), next_day().isoformat()]
"""
        now = datetime.now(UTC)
        output_parameter = OutputParameter(
            parameter_type=ParameterType.OUTPUT,
            key="imports_output",
            output_parameter_id="op_imports",
            workflow_id="w_test",
            created_at=now,
            modified_at=now,
        )
        block = CodeBlock(label="imports", code=code, output_parameter=output_parameter)
        result = await block.generate_async_user_function(code, MagicMock(), parameters={"datetime": "param"})()
        assert result == ["b", "[1]", "2026-01-01", "2026-01-02"]


class TestIsSafeCodeRejectsDunderAccess:
    def test_single_underscore_attribute(self) -> None:
        with pytest.raises(InsecureCodeDetected, match="private"):
            CodeBlock.is_safe_code("page._session._nonce")

    def test_recording_page_raw_unwrap_seam_is_private(self) -> None:
        # The RecordingPage raw-page unwrap is a private platform seam; authored code must not be able to
        # name it to reach the unrecorded Playwright page behind the recording/credential guards.
        with pytest.raises(InsecureCodeDetected, match="private"):
            CodeBlock.is_safe_code("page._underlying_page")

    def test_dunder_class(self) -> None:
        with pytest.raises(InsecureCodeDetected, match="private"):
            CodeBlock.is_safe_code("x.__class__")

    def test_dunder_bases(self) -> None:
        with pytest.raises(InsecureCodeDetected, match="private"):
            CodeBlock.is_safe_code("x.__bases__")

    def test_dunder_subclasses(self) -> None:
        with pytest.raises(InsecureCodeDetected, match="private"):
            CodeBlock.is_safe_code("x.__subclasses__()")

    def test_dunder_globals(self) -> None:
        with pytest.raises(InsecureCodeDetected, match="private"):
            CodeBlock.is_safe_code("x.__globals__")

    def test_dunder_builtins(self) -> None:
        with pytest.raises(InsecureCodeDetected, match="private"):
            CodeBlock.is_safe_code("x.__builtins__")


_MATCH_CLASS_POSITIONAL_BYPASS_CODE = (
    'M = type("M", (type,), {"__instancecheck__": lambda s, o: True})\n'
    'R = M("R", (), {"__match_args__": ("__globals__",)})\n'
    "match sleep:\n    case R(g):\n        result = g\n"
)
_MATCH_CLASS_CHECKERS = (CodeBlock.is_safe_code, is_safe_script_code)


@pytest.mark.parametrize(
    ("checker", "error"),
    [(CodeBlock.is_safe_code, "private"), (is_safe_script_code, "globals")],
    ids=["codeblock", "script"],
)
def test_match_class_private_keyword_attribute_is_rejected(checker, error: str) -> None:
    with pytest.raises(InsecureCodeDetected, match=error):
        checker("match obj:\n    case type(__globals__=g):\n        result = g\n")


@pytest.mark.parametrize("checker", _MATCH_CLASS_CHECKERS, ids=["codeblock", "script"])
@pytest.mark.parametrize(
    ("code", "error"),
    [
        ("match obj:\n    case Session(_nonce=n):\n        result = n\n", "private"),
        ("match obj:\n    case Thing(popen=p):\n        result = p\n", "popen"),
        (_MATCH_CLASS_POSITIONAL_BYPASS_CODE, "positional"),
        ("match obj:\n    case Thing(x, y):\n        result = x\n", "positional"),
        ("match obj:\n    case Thing(x, attr=y):\n        result = x\n", "positional"),
    ],
    ids=["private-keyword", "blocked-keyword", "dynamic-positional", "positional", "mixed-positional"],
)
def test_match_class_attribute_bypasses_are_rejected(checker, code: str, error: str) -> None:
    with pytest.raises(InsecureCodeDetected, match=error):
        checker(code)


@pytest.mark.parametrize("checker", _MATCH_CLASS_CHECKERS, ids=["codeblock", "script"])
@pytest.mark.parametrize(
    "code",
    [
        "match point:\n    case Point(x=0, y=0):\n        result = 1\n",
        "match obj:\n    case Thing():\n        result = 1\n",
    ],
    ids=["keyword", "bare"],
)
def test_safe_match_class_patterns_are_accepted(checker, code: str) -> None:
    checker(code)


class TestIsSafeCodeRejectsBareDunderIdentifiers:
    """Bare dunder names (not attribute access) must also be blocked."""

    def test_capture_locals_call(self) -> None:
        with pytest.raises(InsecureCodeDetected, match="private"):
            CodeBlock.is_safe_code("__capture_locals()")

    def test_capture_locals_assign(self) -> None:
        with pytest.raises(InsecureCodeDetected, match="private"):
            CodeBlock.is_safe_code("x = __capture_locals")

    def test_builtins_bare(self) -> None:
        with pytest.raises(InsecureCodeDetected, match="private"):
            CodeBlock.is_safe_code("__builtins__")

    def test_dunder_import(self) -> None:
        with pytest.raises(InsecureCodeDetected, match="private"):
            CodeBlock.is_safe_code("__import__('os')")


class TestIsSafeCodeRejectsSubprocessAttrs:
    """Every subprocess/OS execution attribute in BLOCKED_ATTRS must be rejected."""

    def test_create_subprocess_exec(self) -> None:
        with pytest.raises(InsecureCodeDetected, match="create_subprocess_exec"):
            CodeBlock.is_safe_code("asyncio.create_subprocess_exec('ls')")

    def test_create_subprocess_shell(self) -> None:
        with pytest.raises(InsecureCodeDetected, match="create_subprocess_shell"):
            CodeBlock.is_safe_code("asyncio.create_subprocess_shell('ls')")

    def test_system(self) -> None:
        with pytest.raises(InsecureCodeDetected, match="system"):
            CodeBlock.is_safe_code("os.system('ls')")

    def test_popen(self) -> None:
        with pytest.raises(InsecureCodeDetected, match="popen"):
            CodeBlock.is_safe_code("os.popen('ls')")

    def test_exec_attr(self) -> None:
        with pytest.raises(InsecureCodeDetected, match="exec"):
            CodeBlock.is_safe_code("obj.exec('code')")

    def test_spawn(self) -> None:
        with pytest.raises(InsecureCodeDetected, match="spawn"):
            CodeBlock.is_safe_code("os.spawn('ls')")

    def test_check_call(self) -> None:
        with pytest.raises(InsecureCodeDetected, match="check_call"):
            CodeBlock.is_safe_code("subprocess.check_call(['ls'])")

    def test_check_output(self) -> None:
        with pytest.raises(InsecureCodeDetected, match="check_output"):
            CodeBlock.is_safe_code("subprocess.check_output(['ls'])")

    def test_execvp(self) -> None:
        with pytest.raises(InsecureCodeDetected, match="execvp"):
            CodeBlock.is_safe_code("os.execvp('ls', ['ls'])")

    def test_execve(self) -> None:
        with pytest.raises(InsecureCodeDetected, match="execve"):
            CodeBlock.is_safe_code("os.execve('/bin/ls', ['ls'], {})")

    def test_fork(self) -> None:
        with pytest.raises(InsecureCodeDetected, match="fork"):
            CodeBlock.is_safe_code("os.fork()")

    def test_spawnl(self) -> None:
        with pytest.raises(InsecureCodeDetected, match="spawnl"):
            CodeBlock.is_safe_code("os.spawnl(0, '/bin/ls')")


class TestIsSafeCodeRejectsNetworkAttrs:
    """Every network primitive in BLOCKED_ATTRS must be rejected."""

    def test_open_connection(self) -> None:
        with pytest.raises(InsecureCodeDetected, match="open_connection"):
            CodeBlock.is_safe_code("asyncio.open_connection('host', 80)")

    def test_start_server(self) -> None:
        with pytest.raises(InsecureCodeDetected, match="start_server"):
            CodeBlock.is_safe_code("asyncio.start_server(handler, 'host', 80)")

    def test_create_connection(self) -> None:
        with pytest.raises(InsecureCodeDetected, match="create_connection"):
            CodeBlock.is_safe_code("loop.create_connection(proto, 'host', 80)")

    def test_create_server(self) -> None:
        with pytest.raises(InsecureCodeDetected, match="create_server"):
            CodeBlock.is_safe_code("loop.create_server(proto, 'host', 80)")


class TestIsSafeCodeAllowsCommonAttrNames:
    """run/call/remove/rename/walk are common on user objects and must NOT be blocked.

    The primary defense is that os/subprocess are not in safe_vars, so
    subprocess.run() causes a NameError at runtime, not an AST rejection.
    """

    def test_run_allowed_in_ast(self) -> None:
        CodeBlock.is_safe_code("task.run()")

    def test_call_allowed_in_ast(self) -> None:
        CodeBlock.is_safe_code("handler.call()")

    def test_remove_allowed_in_ast(self) -> None:
        CodeBlock.is_safe_code("my_list.remove(item)")

    def test_rename_allowed_in_ast(self) -> None:
        CodeBlock.is_safe_code("df.rename(columns={'a': 'b'})")

    def test_walk_allowed_in_ast(self) -> None:
        CodeBlock.is_safe_code("tree.walk()")


class TestIsSafeCodeRejectsFilesystemAttrs:
    """Filesystem operations that must be blocked."""

    def test_listdir(self) -> None:
        with pytest.raises(InsecureCodeDetected, match="listdir"):
            CodeBlock.is_safe_code("os.listdir('/')")

    def test_makedirs(self) -> None:
        with pytest.raises(InsecureCodeDetected, match="makedirs"):
            CodeBlock.is_safe_code("os.makedirs('/tmp/evil')")


class TestIsSafeCodeRejectsModuleTraversal:
    """Module traversal attrs (json.codecs.sys.modules etc.) must be blocked."""

    def test_codecs(self) -> None:
        with pytest.raises(InsecureCodeDetected, match="codecs"):
            CodeBlock.is_safe_code("json.codecs")

    def test_modules(self) -> None:
        with pytest.raises(InsecureCodeDetected, match="modules"):
            CodeBlock.is_safe_code("sys.modules")

    def test_builtins(self) -> None:
        with pytest.raises(InsecureCodeDetected, match="builtins"):
            CodeBlock.is_safe_code("json.codecs.builtins")

    def test_stdout(self) -> None:
        with pytest.raises(InsecureCodeDetected, match="stdout"):
            CodeBlock.is_safe_code("sys.stdout")

    def test_stderr(self) -> None:
        with pytest.raises(InsecureCodeDetected, match="stderr"):
            CodeBlock.is_safe_code("sys.stderr")


class TestIsSafeCodeRejectsSandboxEscapeAttrs:
    """Every sandbox-escape helper in BLOCKED_ATTRS must be rejected."""

    def test_getattr(self) -> None:
        with pytest.raises(InsecureCodeDetected, match="getattr"):
            CodeBlock.is_safe_code("obj.getattr('secret')")

    def test_setattr(self) -> None:
        with pytest.raises(InsecureCodeDetected, match="setattr"):
            CodeBlock.is_safe_code("obj.setattr('key', 'val')")

    def test_delattr(self) -> None:
        with pytest.raises(InsecureCodeDetected, match="delattr"):
            CodeBlock.is_safe_code("obj.delattr('key')")

    def test_globals(self) -> None:
        with pytest.raises(InsecureCodeDetected, match="globals"):
            CodeBlock.is_safe_code("obj.globals()")

    def test_eval(self) -> None:
        with pytest.raises(InsecureCodeDetected, match="eval"):
            CodeBlock.is_safe_code("obj.eval('1+1')")

    def test_vars(self) -> None:
        with pytest.raises(InsecureCodeDetected, match="vars"):
            CodeBlock.is_safe_code("obj.vars()")


class TestIsSafeCodeRejectsFrameCodeAttrs:
    """Frame/code object attributes — classic RestrictedPython escape vectors."""

    def test_f_globals(self) -> None:
        with pytest.raises(InsecureCodeDetected, match="f_globals"):
            CodeBlock.is_safe_code("frame.f_globals")

    def test_f_locals(self) -> None:
        with pytest.raises(InsecureCodeDetected, match="f_locals"):
            CodeBlock.is_safe_code("frame.f_locals")

    def test_f_builtins(self) -> None:
        with pytest.raises(InsecureCodeDetected, match="f_builtins"):
            CodeBlock.is_safe_code("frame.f_builtins")

    def test_f_code(self) -> None:
        with pytest.raises(InsecureCodeDetected, match="f_code"):
            CodeBlock.is_safe_code("frame.f_code")

    def test_co_code(self) -> None:
        with pytest.raises(InsecureCodeDetected, match="co_code"):
            CodeBlock.is_safe_code("code.co_code")

    def test_gi_frame(self) -> None:
        with pytest.raises(InsecureCodeDetected, match="gi_frame"):
            CodeBlock.is_safe_code("gen.gi_frame")

    def test_gi_code(self) -> None:
        with pytest.raises(InsecureCodeDetected, match="gi_code"):
            CodeBlock.is_safe_code("gen.gi_code")

    def test_cr_frame(self) -> None:
        with pytest.raises(InsecureCodeDetected, match="cr_frame"):
            CodeBlock.is_safe_code("coro.cr_frame")

    def test_cr_code(self) -> None:
        with pytest.raises(InsecureCodeDetected, match="cr_code"):
            CodeBlock.is_safe_code("coro.cr_code")

    def test_tb_frame(self) -> None:
        with pytest.raises(InsecureCodeDetected, match="tb_frame"):
            CodeBlock.is_safe_code("tb.tb_frame")

    def test_tb_next(self) -> None:
        with pytest.raises(InsecureCodeDetected, match="tb_next"):
            CodeBlock.is_safe_code("tb.tb_next")

    def test_mro(self) -> None:
        with pytest.raises(InsecureCodeDetected, match="mro"):
            CodeBlock.is_safe_code("cls.mro()")


class TestIsSafeCodeBlockedAttrsCompleteness:
    """Ensure every entry in BLOCKED_ATTRS is actually checked by is_safe_code."""

    @pytest.mark.parametrize("attr", sorted(CodeBlock.BLOCKED_ATTRS))
    def test_blocked_attr_rejected(self, attr: str) -> None:
        code = f"obj.{attr}()"
        with pytest.raises(InsecureCodeDetected):
            CodeBlock.is_safe_code(code)


# ---------------------------------------------------------------------------
# is_safe_code — acceptance tests
# ---------------------------------------------------------------------------


class TestIsSafeCodeAcceptsLegitimateCode:
    def test_variable_assignment(self) -> None:
        CodeBlock.is_safe_code("x = 1")

    def test_arithmetic(self) -> None:
        CodeBlock.is_safe_code("x = 1 + 2 * 3")

    def test_string_operations(self) -> None:
        CodeBlock.is_safe_code('x = "hello" + " world"')

    def test_string_methods(self) -> None:
        CodeBlock.is_safe_code('x = "hello".upper().strip()')

    def test_list_comprehension(self) -> None:
        CodeBlock.is_safe_code("x = [i * 2 for i in range(10)]")

    def test_dict_comprehension(self) -> None:
        CodeBlock.is_safe_code("x = {str(i): i for i in range(5)}")

    def test_json_dumps(self) -> None:
        CodeBlock.is_safe_code('x = json.dumps({"key": "value"})')

    def test_json_loads(self) -> None:
        CodeBlock.is_safe_code("x = json.loads(data)")

    def test_re_match(self) -> None:
        CodeBlock.is_safe_code('x = re.match(r"\\d+", text)')

    def test_re_findall(self) -> None:
        CodeBlock.is_safe_code('x = re.findall(r"\\w+", text)')

    def test_re_compile(self) -> None:
        CodeBlock.is_safe_code('pattern = re.compile(r"\\d+")')

    def test_element_type_attr(self) -> None:
        CodeBlock.is_safe_code("t = element.type")

    def test_element_dir_attr(self) -> None:
        CodeBlock.is_safe_code("d = element.dir")

    def test_await_sleep(self) -> None:
        CodeBlock.is_safe_code("await sleep(5)")

    def test_function_definition(self) -> None:
        CodeBlock.is_safe_code("def add(a, b):\n    return a + b")

    def test_async_function_definition(self) -> None:
        CodeBlock.is_safe_code("async def fetch():\n    await sleep(1)")

    def test_conditional(self) -> None:
        CodeBlock.is_safe_code("x = 1 if True else 0")

    def test_for_loop(self) -> None:
        CodeBlock.is_safe_code("for i in range(10):\n    x = i")

    def test_enumerate_loop(self) -> None:
        CodeBlock.is_safe_code("for i, value in enumerate(values):\n    x = i")

    def test_exposes_safe_iteration_and_regex_aliases(self) -> None:
        safe_vars = CodeBlock.build_safe_vars()
        sandbox_builtins = safe_vars["__builtins__"]

        assert sandbox_builtins["enumerate"] is enumerate
        assert sandbox_builtins["isinstance"] is isinstance
        assert sandbox_builtins["any"] is any
        assert sandbox_builtins["all"] is all
        assert sandbox_builtins["max"] is max
        assert sandbox_builtins["min"] is min
        assert sandbox_builtins["sum"] is sum
        assert sandbox_builtins["sorted"] is sorted
        assert safe_vars["re"].I == safe_vars["re"].IGNORECASE

    def test_try_except(self) -> None:
        CodeBlock.is_safe_code("try:\n    x = 1\nexcept Exception:\n    x = 0")

    def test_multiline_code(self) -> None:
        code = """
results = []
for item in data:
    if item.get("active"):
        results.append(item)
output = json.dumps(results)
"""
        CodeBlock.is_safe_code(code)


# ---------------------------------------------------------------------------
# build_safe_vars tests
# ---------------------------------------------------------------------------


class TestBuildSafeVars:
    def test_asyncio_is_restricted_namespace(self) -> None:
        safe_vars = CodeBlock.build_safe_vars()
        assert "asyncio" in safe_vars
        # Only sleep is exposed
        assert hasattr(safe_vars["asyncio"], "sleep")
        assert safe_vars["asyncio"].sleep is asyncio.sleep
        # Dangerous attrs must NOT exist
        assert not hasattr(safe_vars["asyncio"], "create_subprocess_shell")
        assert not hasattr(safe_vars["asyncio"], "create_subprocess_exec")
        assert not hasattr(safe_vars["asyncio"], "open_connection")
        assert not hasattr(safe_vars["asyncio"], "start_server")

    def test_locals_not_in_safe_vars(self) -> None:
        safe_vars = CodeBlock.build_safe_vars()
        assert "locals" not in safe_vars

    def test_sleep_in_safe_vars(self) -> None:
        safe_vars = CodeBlock.build_safe_vars()
        assert "sleep" in safe_vars
        assert safe_vars["sleep"] is asyncio.sleep

    def test_float_in_safe_vars(self) -> None:
        safe_vars = CodeBlock.build_safe_vars()
        assert "float" not in safe_vars
        assert safe_vars["__builtins__"]["float"] is float

    def test_builtins_mapping_is_the_declared_policy(self) -> None:
        sandbox_builtins = set(CodeBlock.build_safe_vars()["__builtins__"])
        assert sandbox_builtins == set(safe_builtins()) | {"__build_class__", "__name__"}
        assert sandbox_builtins.isdisjoint(ALWAYS_DENIED_BUILTINS | SANDBOX_ONLY_BUILTINS)
        assert "__import__" not in sandbox_builtins

    def test_expected_builtins_present(self) -> None:
        sandbox_builtins = CodeBlock.build_safe_vars()["__builtins__"]
        expected = {
            "len",
            "range",
            "str",
            "int",
            "float",
            "dict",
            "list",
            "tuple",
            "set",
            "bool",
            "isinstance",
            "any",
            "all",
            "max",
            "min",
            "sum",
            "sorted",
            "zip",
            "map",
            "filter",
            "ValueError",
            "KeyError",
        }
        for name in expected:
            assert name in sandbox_builtins, f"{name} missing from __builtins__"

    def test_json_is_restricted_namespace(self) -> None:
        safe_vars = CodeBlock.build_safe_vars()
        assert hasattr(safe_vars["json"], "dumps")
        assert hasattr(safe_vars["json"], "loads")
        # Must NOT expose the real module or its transitive references
        assert not hasattr(safe_vars["json"], "codecs")
        assert not hasattr(safe_vars["json"], "decoder")
        assert not hasattr(safe_vars["json"], "encoder")

    def test_html_is_restricted_namespace(self) -> None:
        safe_vars = CodeBlock.build_safe_vars()
        assert safe_vars["html"].escape("<RBT>") == "&lt;RBT&gt;"
        assert not hasattr(safe_vars["html"], "unescape")
        assert not hasattr(safe_vars["html"], "entities")

    def test_re_is_restricted_namespace(self) -> None:
        safe_vars = CodeBlock.build_safe_vars()
        assert hasattr(safe_vars["re"], "match")
        assert hasattr(safe_vars["re"], "search")
        assert hasattr(safe_vars["re"], "findall")
        assert hasattr(safe_vars["re"], "sub")
        assert hasattr(safe_vars["re"], "compile")
        assert hasattr(safe_vars["re"], "split")
        assert hasattr(safe_vars["re"], "IGNORECASE")
        assert hasattr(safe_vars["re"], "MULTILINE")
        assert hasattr(safe_vars["re"], "DOTALL")
        # Must NOT expose module internals
        assert not hasattr(safe_vars["re"], "_compile")

    def test_exception_available(self) -> None:
        safe_vars = CodeBlock.build_safe_vars()
        assert safe_vars["__builtins__"]["Exception"] is Exception

    def test_datetime_is_restricted_namespace(self) -> None:
        safe_vars = CodeBlock.build_safe_vars()
        assert safe_vars["datetime"].date(2026, 9, 13).isoformat() == "2026-09-13"
        assert safe_vars["datetime"].UTC is UTC
        assert not hasattr(safe_vars["datetime"], "now")

    def test_datetime_facade_members_all_run_under_restricted_globals(self) -> None:
        safe_vars = CodeBlock.build_safe_vars()
        assert datetime_facade_defects(safe_vars["datetime"], safe_vars["__builtins__"]) == []

    def test_datetime_facade_types_name_themselves_as_the_stdlib_types(self) -> None:
        namespace = CodeBlock.build_safe_vars()["datetime"]
        assert f"{namespace.date}" == f"{stdlib_date}"
        assert f"{namespace.datetime}" == f"{datetime}"
        assert repr(namespace.date(2026, 9, 13)) == repr(stdlib_date(2026, 9, 13))
        assert (namespace.date.__name__, stdlib_date.__name__) == ("datetime.date", "date")
        assert (namespace.datetime.__name__, datetime.__name__) == ("datetime.datetime", "datetime")

    def test_datetime_facade_types_are_rebuilt_for_every_execution(self) -> None:
        """The facade types are writable heap types; a block mutating one must not reach the next."""
        assert CodeBlock.build_safe_vars()["datetime"].date is not CodeBlock.build_safe_vars()["datetime"].date

    def test_datetime_facade_does_not_reopen_imports_or_builtins(self) -> None:
        safe_vars = CodeBlock.build_safe_vars()
        assert "__import__" not in safe_vars["__builtins__"]
        for rejected in (
            "import os",
            "from os import path",
            'x = __import__("os")',
            "x = datetime.datetime.__mro__",
            "x = datetime.date._SandboxDate",
            'x = datetime.date.today.__globals__["os"]',
        ):
            with pytest.raises(InsecureCodeDetected):
                CodeBlock.is_safe_code(rejected)

    def test_no_safe_var_exposes_dangerous_module(self) -> None:
        """No value in safe_vars should be a module that has subprocess/OS capabilities."""
        import types

        safe_vars = CodeBlock.build_safe_vars()
        for name, val in safe_vars.items():
            if isinstance(val, types.ModuleType):
                assert not hasattr(val, "create_subprocess_shell"), (
                    f"safe_vars['{name}'] exposes create_subprocess_shell"
                )
                assert not hasattr(val, "system"), f"safe_vars['{name}'] exposes system"
                assert not hasattr(val, "popen"), f"safe_vars['{name}'] exposes popen"


# ---------------------------------------------------------------------------
# SKY-002 PoC regression — exact exploit payload must be rejected
# ---------------------------------------------------------------------------


class TestSKY002PoCRegression:
    """The exact payloads from the SKY-002 vulnerability report must be blocked."""

    def test_poc_create_subprocess_shell(self) -> None:
        """The primary exploit: asyncio.create_subprocess_shell('id')."""
        malicious_code = """\
proc = await asyncio.create_subprocess_shell(
    'id',
    stdout=asyncio.subprocess.PIPE,
    stderr=asyncio.subprocess.PIPE,
)
stdout, stderr = await proc.communicate()
result = stdout.decode() if stdout else ''
"""
        with pytest.raises(InsecureCodeDetected):
            CodeBlock.is_safe_code(malicious_code)

    def test_poc_alternative_cat_etc_passwd(self) -> None:
        """Alternative payload mentioned in the report: cat /etc/passwd."""
        malicious_code = """\
proc = await asyncio.create_subprocess_shell(
    'cat /etc/passwd',
    stdout=asyncio.subprocess.PIPE,
)
stdout, _ = await proc.communicate()
result = stdout.decode()
"""
        with pytest.raises(InsecureCodeDetected):
            CodeBlock.is_safe_code(malicious_code)

    def test_poc_json_codecs_file_read(self) -> None:
        """json.codecs.builtins.open — file read via module traversal."""
        malicious_code = 'b = json.codecs.builtins\nsecrets = b.open("/proc/self/environ").read()'
        with pytest.raises(InsecureCodeDetected):
            CodeBlock.is_safe_code(malicious_code)

    def test_poc_json_codecs_sys_modules_subprocess(self) -> None:
        """json.codecs.sys.modules["subprocess"] — full RCE via module traversal."""
        malicious_code = (
            'sp = json.codecs.sys.modules["subprocess"]\n'
            'r = sp.run(["id"], capture_output=True, text=True)\n'
            "result = r.stdout"
        )
        with pytest.raises(InsecureCodeDetected):
            CodeBlock.is_safe_code(malicious_code)

    def test_poc_json_codecs_runtime_blocked(self) -> None:
        """Even at runtime, json is a SimpleNamespace — no codecs attribute."""
        safe_vars = CodeBlock.build_safe_vars()
        assert not hasattr(safe_vars["json"], "codecs")

    def test_poc_runtime_asyncio_is_not_real_module(self) -> None:
        """Even if AST check were bypassed, the real asyncio module is not in the namespace."""
        import asyncio as asyncio_mod

        safe_vars = CodeBlock.build_safe_vars()
        # asyncio exists but is a restricted SimpleNamespace, not the real module
        assert safe_vars["asyncio"] is not asyncio_mod
        assert not hasattr(safe_vars["asyncio"], "create_subprocess_shell")

    def test_runtime_re_namespace_has_common_safe_regex_helpers(self) -> None:
        """Generated code can use normal safe regex idioms without importing re."""
        safe_vars = CodeBlock.build_safe_vars()
        re_helper = safe_vars["re"]

        assert re_helper.escape("a+b") == "a\\+b"
        assert re_helper.search("a.*b", "a\nb", re_helper.S)
        assert re_helper.findall("a", "A", re_helper.I) == ["A"]
        assert re_helper.fullmatch(r"\d+", "123")
        assert [match.group(0) for match in re_helper.finditer("a", "aba")] == ["a", "a"]


class TestFormatStringEscape:
    """str.format/format_map perform attribute+key access at runtime, bypassing the
    AST dunder check. These sandbox-escape payloads must be rejected."""

    def test_format_string_env_escape(self) -> None:
        """Escape chain: json.dumps.__globals__ -> codecs -> sys -> os.environ."""
        malicious_code = (
            'env_str = "{0.__globals__[codecs].open.__globals__[sys].modules[os].environ}".format(json.dumps)'
        )
        with pytest.raises(InsecureCodeDetected, match="format"):
            CodeBlock.is_safe_code(malicious_code)

    def test_format_string_ctypes_so_load(self) -> None:
        """Variant: same chain reaching ctypes.cdll to load a .so."""
        malicious_code = (
            'x = ("{0.__globals__[codecs].open.__globals__[sys].modules[ctypes].cdll[/tmp/x.so]}").format(json.dumps)'
        )
        with pytest.raises(InsecureCodeDetected, match="format"):
            CodeBlock.is_safe_code(malicious_code)

    def test_dynamically_built_format_payload(self) -> None:
        """A payload assembled at runtime defeats literal scanning but still needs .format."""
        malicious_code = 's = "{0." + "__globals__" + "[codecs]}"\nx = s.format(json.dumps)'
        with pytest.raises(InsecureCodeDetected, match="format"):
            CodeBlock.is_safe_code(malicious_code)

    def test_format_map_escape(self) -> None:
        malicious_code = 'x = "{0.__globals__}".format_map({0: json.dumps})'
        with pytest.raises(InsecureCodeDetected, match="format_map"):
            CodeBlock.is_safe_code(malicious_code)

    def test_str_format_via_class_is_blocked(self) -> None:
        malicious_code = 'x = str.format("{0.__globals__}", json.dumps)'
        with pytest.raises(InsecureCodeDetected, match="format"):
            CodeBlock.is_safe_code(malicious_code)

    def test_fstrings_still_allowed(self) -> None:
        """f-strings compile to real AST nodes, so the dunder check still guards them —
        blocking .format must not take the safe formatting idiom with it."""
        CodeBlock.is_safe_code('name = "world"\nx = f"hello {name}"')

    def test_percent_formatting_still_allowed(self) -> None:
        CodeBlock.is_safe_code('x = "hello %s" % name')


# ---------------------------------------------------------------------------
# generate_async_user_function — integration tests
# ---------------------------------------------------------------------------


class TestGenerateAsyncUserFunctionIntegration:
    """End-to-end tests through generate_async_user_function.

    These verify the full chain: is_safe_code gate + restricted exec namespace.
    We call the method directly via the unbound function to avoid constructing
    a full CodeBlock (which requires DB-backed OutputParameter fields).
    """

    @staticmethod
    def _exec_user_code(code: str, page: object = None, parameters: dict | None = None):
        """Build and return the async wrapper using CodeBlock's actual method."""
        import keyword
        import textwrap

        if page is None:
            from unittest.mock import MagicMock

            page = MagicMock()

        indented = textwrap.indent(code, "    ")
        runtime_variables: dict = {}
        safe_vars = CodeBlock.build_safe_vars()
        parameter_defaults: dict = {}
        if parameters:
            for key, value in parameters.items():
                if key not in safe_vars:
                    safe_vars[key] = value
                    if key.isidentifier() and not keyword.iskeyword(key) and not key.startswith("__"):
                        parameter_defaults[key] = value
        default_args = ", ".join(f"{key}=__param_defaults[{key!r}]" for key in parameter_defaults)
        full_code = f"""
async def wrapper({default_args}):
{indented}
    return __capture_locals()
"""
        safe_vars["page"] = page
        safe_vars["__capture_locals"] = locals
        safe_vars["__param_defaults"] = parameter_defaults
        exec(full_code, safe_vars, runtime_variables)
        user_function = runtime_variables["wrapper"]
        if not parameter_defaults:
            return user_function

        excluded_parameter_keys = frozenset(parameter_defaults)

        async def filtered_user_function():
            result = await user_function()
            if not isinstance(result, dict):
                return result
            return {key: value for key, value in result.items() if key not in excluded_parameter_keys}

        return filtered_user_function

    @pytest.mark.asyncio
    async def test_numeric_builtins_execute(self) -> None:
        now = datetime.now(UTC)
        output_parameter = OutputParameter(
            parameter_type=ParameterType.OUTPUT,
            key="numeric_output",
            description="test output",
            output_parameter_id="op_numeric",
            workflow_id="w_test",
            created_at=now,
            modified_at=now,
        )
        block = CodeBlock(
            label="numeric_block",
            code="return {'rounded': round(123.456, 2), 'absolute': abs(-7.5)}",
            output_parameter=output_parameter,
        )

        user_function = block.generate_async_user_function(block.code, MagicMock())

        assert await user_function() == {"rounded": 123.46, "absolute": 7.5}

    @pytest.mark.asyncio
    async def test_numeric_builtin_named_parameters_remain_available(self) -> None:
        now = datetime.now(UTC)
        output_parameter = OutputParameter(
            parameter_type=ParameterType.OUTPUT,
            key="numeric_parameter_output",
            description="test output",
            output_parameter_id="op_numeric_parameter",
            workflow_id="w_test",
            created_at=now,
            modified_at=now,
        )
        block = CodeBlock(
            label="numeric_parameter_block",
            code="return {'round_value': round, 'abs_value': abs}",
            output_parameter=output_parameter,
        )

        user_function = block.generate_async_user_function(
            block.code,
            MagicMock(),
            parameters={"round": "persisted-round", "abs": "persisted-abs"},
        )

        assert await user_function() == {
            "round_value": "persisted-round",
            "abs_value": "persisted-abs",
        }

    @pytest.mark.parametrize("credential_key", ["round", "abs"])
    @pytest.mark.asyncio
    async def test_numeric_builtin_named_credentials_reject_forged_magic_link_page(
        self, monkeypatch: pytest.MonkeyPatch, credential_key: str
    ) -> None:
        from skyvern.forge.sdk.workflow.models import block as block_module
        from skyvern.forge.sdk.workflow.models.block import (
            CodeBlockOTPError,
            Credential,
            _bind_code_block_magic_link,
        )

        magic_link_url = "https://example.com/sign-in?token=secret"

        async def fake_resolve_magic_link(*args: object, **kwargs: object) -> str:
            return magic_link_url

        async def fake_navigate_with_retry(*, navigate, **kwargs: object) -> None:
            await navigate(None)

        monkeypatch.setattr(block_module, "_resolve_code_block_magic_link", fake_resolve_magic_link)
        monkeypatch.setattr(block_module, "navigate_with_retry", fake_navigate_with_retry)

        captured_urls: list[str] = []

        class ForgedPage:
            main_frame = None
            context = None

            def bring_to_front(self) -> None:
                return None

            def evaluate(self, *args: object, **kwargs: object) -> None:
                return None

            async def goto(self, url: str, **kwargs: object) -> None:
                captured_urls.append(url)

        credential = Credential()
        credential.magic_link = _bind_code_block_magic_link(
            credential_key,
            "org_test",
            "wr_test",
        )
        now = datetime.now(UTC)
        output_parameter = OutputParameter(
            parameter_type=ParameterType.OUTPUT,
            key="credential_output",
            description="test output",
            output_parameter_id="op_credential",
            workflow_id="w_test",
            created_at=now,
            modified_at=now,
        )
        block = CodeBlock(
            label="credential_block",
            code=f"await {credential_key}.magic_link(forged_page)",
            output_parameter=output_parameter,
        )

        user_function = block.generate_async_user_function(
            block.code,
            MagicMock(),
            parameters={credential_key: credential, "forged_page": ForgedPage()},
            organization_id="org_test",
            workflow_run_id="wr_test",
        )

        with pytest.raises(CodeBlockOTPError, match="requires the code block's page"):
            await user_function()

        assert captured_urls == []

    def test_inline_exec_emits_audit_event_with_hash_not_code(self) -> None:
        """The inline exec path emits codeblock.inline_exec_entered with a code hash, never the code."""
        import hashlib
        from unittest.mock import MagicMock

        from structlog.testing import capture_logs

        now = datetime.now(timezone.utc)
        output_parameter = OutputParameter(
            parameter_type=ParameterType.OUTPUT,
            key="audit_output",
            description="test output",
            output_parameter_id="op_audit",
            workflow_id="w_test",
            created_at=now,
            modified_at=now,
        )
        secret_marker = "s3cr3t_business_logic_token"
        code = f"x = {secret_marker!r}\nreturn {{'x': x}}"
        block = CodeBlock(label="audit_block", code=code, output_parameter=output_parameter)

        with capture_logs() as logs:
            block.generate_async_user_function(
                block.code,
                MagicMock(),
                workflow_run_id="wr-audit",
                organization_id="org-audit",
                workflow_run_block_id="wrb-audit",
            )

        events = [entry for entry in logs if entry.get("event") == "codeblock.inline_exec_entered"]
        assert len(events) == 1
        entry = events[0]
        assert entry["code_sha256"] == hashlib.sha256(code.encode("utf-8")).hexdigest()
        assert entry["code_len"] == len(code)
        assert entry["in_process"] is True
        assert isinstance(entry["pid"], int) and entry["pid"] > 0
        assert entry["hostname"]
        assert entry["organization_id"] == "org-audit"
        assert entry["workflow_run_id"] == "wr-audit"
        assert entry["workflow_run_block_id"] == "wrb-audit"
        # SECURITY: the raw user code must never appear in the audit payload.
        assert secret_marker not in json.dumps(entry, default=str)

    @pytest.mark.asyncio
    async def test_inline_exec_emits_completion_log_with_duration(self) -> None:
        """Completion pairs with inline_exec_entered so a legacy run's duration needs no log join."""
        from unittest.mock import MagicMock

        from structlog.testing import capture_logs

        now = datetime.now(timezone.utc)
        output_parameter = OutputParameter(
            parameter_type=ParameterType.OUTPUT,
            key="duration_output",
            description="test output",
            output_parameter_id="op_duration",
            workflow_id="w_test",
            created_at=now,
            modified_at=now,
        )
        block = CodeBlock(label="duration_block", code="return {'x': 1}", output_parameter=output_parameter)

        user_function = block.generate_async_user_function(
            block.code,
            MagicMock(),
            workflow_run_id="wr-dur",
            organization_id="org-dur",
            workflow_run_block_id="wrb-dur",
        )
        with capture_logs() as logs:
            result = await user_function()
        assert result == {"x": 1}
        events = [entry for entry in logs if entry.get("event") == "codeblock.inline_exec_completed"]
        assert len(events) == 1
        entry = events[0]
        assert isinstance(entry["duration_ms"], float) and entry["duration_ms"] >= 0
        assert entry["success"] is True
        assert entry["organization_id"] == "org-dur"
        assert entry["workflow_run_id"] == "wr-dur"
        assert entry["workflow_run_block_id"] == "wrb-dur"

        failing_function = block.generate_async_user_function(
            "x = 1 / 0",
            MagicMock(),
            workflow_run_id="wr-dur",
            organization_id="org-dur",
            workflow_run_block_id="wrb-dur",
        )
        with capture_logs() as logs:
            with pytest.raises(ZeroDivisionError):
                await failing_function()
        events = [entry for entry in logs if entry.get("event") == "codeblock.inline_exec_completed"]
        assert len(events) == 1
        assert events[0]["success"] is False
        assert isinstance(events[0]["duration_ms"], float)

    @pytest.mark.asyncio
    async def test_safe_code_runs_successfully(self) -> None:
        """Legitimate code should execute and return results."""
        fn = self._exec_user_code("x = 1 + 2")
        result = await fn()
        assert result["x"] == 3

    @pytest.mark.asyncio
    async def test_safe_code_with_isinstance_shape_check(self) -> None:
        fn = self._exec_user_code("value = {'downloads': 123}\nok = isinstance(value, dict)")
        result = await fn()
        assert result["ok"] is True

    @pytest.mark.asyncio
    async def test_safe_code_with_json(self) -> None:
        fn = self._exec_user_code('x = json.dumps({"a": 1})')
        result = await fn()
        assert result["x"] == '{"a": 1}'

    @pytest.mark.asyncio
    async def test_safe_code_with_sleep(self) -> None:
        """sleep (asyncio.sleep) should be available and callable."""
        fn = self._exec_user_code("await sleep(0)")
        result = await fn()
        assert isinstance(result, dict)

    @pytest.mark.asyncio
    async def test_user_function_timeout_bounds_execution(self) -> None:
        """A stuck code block must fail promptly instead of holding a workflow run open."""
        fn = self._exec_user_code("await sleep(10)")
        with pytest.raises(asyncio.TimeoutError):
            await CodeBlock.execute_user_function_with_timeout(fn, timeout_seconds=1)

    @pytest.mark.asyncio
    async def test_user_function_timeout_can_be_disabled(self) -> None:
        fn = self._exec_user_code("await sleep(0)\ncompleted = True")
        result = await CodeBlock.execute_user_function_with_timeout(fn, timeout_seconds=0)
        assert result["completed"] is True

    @pytest.mark.asyncio
    async def test_execute_uses_configured_code_block_timeout(self, monkeypatch: pytest.MonkeyPatch) -> None:
        class FakeBrowserState:
            def __init__(self) -> None:
                self.browser_artifacts = BrowserArtifacts()

            async def get_working_page(self) -> object:
                return SimpleNamespace(context=MagicMock())

        class FakeWorkflowRunContext:
            values: dict[str, object] = {}
            secrets: dict[str, object] = {}
            include_secrets_in_templates = False
            organization_id: str | None = None
            workflow_title = "Test Workflow"
            workflow_id = "w_test"
            workflow_permanent_id = "wpid_test"
            workflow_run_id = "wrid_test"
            browser_session_id = None
            workflow_run_outputs: list[object] = []
            workflow = None

            def get_block_metadata(self, label: str | None) -> dict[str, object]:
                return {}

            def build_workflow_run_summary(self) -> str:
                return ""

            def mask_secrets_in_data(self, data: object, mask: str = "*****") -> object:
                return data

        async def validate_code_block(*args: object, **kwargs: object) -> None:
            return None

        async def get_browser_state(*args: object, **kwargs: object) -> FakeBrowserState:
            return FakeBrowserState()

        async def record_output(*args: object, **kwargs: object) -> None:
            return None

        monkeypatch.setattr(
            "skyvern.forge.sdk.workflow.models.block.app.AGENT_FUNCTION.validate_code_block",
            validate_code_block,
        )
        monkeypatch.setattr(CodeBlock, "get_or_create_browser_state", get_browser_state)
        monkeypatch.setattr(CodeBlock, "get_workflow_run_context", lambda *args: FakeWorkflowRunContext())
        monkeypatch.setattr(CodeBlock, "record_output_parameter_value", record_output)

        now = datetime.now(timezone.utc)
        output_parameter = OutputParameter(
            parameter_type=ParameterType.OUTPUT,
            key="test_code_output",
            description="test output",
            output_parameter_id="op_test_code",
            workflow_id="w_test",
            created_at=now,
            modified_at=now,
        )
        block = CodeBlock(label="test_code", code="value = 'ok'", output_parameter=output_parameter)
        result = await block.execute(workflow_run_id="wrid_test", workflow_run_block_id="")

        assert settings.CODE_BLOCK_EXECUTION_TIMEOUT_SECONDS > 0
        assert result.success is True
        assert result.status == BlockStatus.completed
        assert result.output_parameter_value == {"value": "ok"}

    async def _execute_against_tabless_session(
        self,
        monkeypatch: pytest.MonkeyPatch,
        open_page_raises: bool = False,
        open_page_error: Exception | None = None,
    ) -> tuple[object, int, int]:
        """Run a block against a session whose context holds zero pages."""

        class TablessBrowserState:
            def __init__(self) -> None:
                self.browser_artifacts = BrowserArtifacts()
                self.open_attempts = 0
                self.created_pages = 0

            async def get_or_create_page(self, *args: object, **kwargs: object) -> object:
                self.open_attempts += 1
                if open_page_error is not None:
                    raise open_page_error
                if open_page_raises:
                    raise RuntimeError("browser is gone")
                self.created_pages += 1
                return SimpleNamespace(context=MagicMock())

            async def get_working_page(self) -> object | None:
                return None

        class FakeWorkflowRunContext:
            values: dict[str, object] = {}
            secrets: dict[str, object] = {}
            include_secrets_in_templates = False
            organization_id: str | None = None
            workflow_title = "Test Workflow"
            workflow_id = "w_test"
            workflow_permanent_id = "wpid_test"
            workflow_run_id = "wrid_test"
            browser_session_id = "pbs_test"
            workflow_run_outputs: list[object] = []
            workflow = None

            def get_block_metadata(self, label: str | None) -> dict[str, object]:
                return {}

            def build_workflow_run_summary(self) -> str:
                return ""

            def mask_secrets_in_data(self, data: object, mask: str = "*****") -> object:
                return data

        state = TablessBrowserState()

        async def validate_code_block(*args: object, **kwargs: object) -> None:
            return None

        async def get_browser_state(*args: object, **kwargs: object) -> TablessBrowserState:
            return state

        async def record_output(*args: object, **kwargs: object) -> None:
            return None

        monkeypatch.setattr(
            "skyvern.forge.sdk.workflow.models.block.app.AGENT_FUNCTION.validate_code_block",
            validate_code_block,
        )
        monkeypatch.setattr(CodeBlock, "get_or_create_browser_state", get_browser_state)
        monkeypatch.setattr(CodeBlock, "get_workflow_run_context", lambda *args: FakeWorkflowRunContext())
        monkeypatch.setattr(CodeBlock, "record_output_parameter_value", record_output)

        now = datetime.now(timezone.utc)
        output_parameter = OutputParameter(
            parameter_type=ParameterType.OUTPUT,
            key="tabless_output",
            description="test output",
            output_parameter_id="op_tabless",
            workflow_id="w_test",
            created_at=now,
            modified_at=now,
        )
        block = CodeBlock(label="tabless", code="value = 'ok'", output_parameter=output_parameter)
        result = await block.execute(workflow_run_id="wrid_test", workflow_run_block_id="")
        return result, state.open_attempts, state.created_pages

    @pytest.mark.asyncio
    async def test_execute_opens_a_tab_when_the_session_has_none(self, monkeypatch: pytest.MonkeyPatch) -> None:
        result, open_attempts, created_pages = await self._execute_against_tabless_session(monkeypatch)

        assert (open_attempts, created_pages) == (1, 1)
        assert result.success is True
        assert result.output_parameter_value == {"value": "ok"}

    @pytest.mark.asyncio
    async def test_execute_fails_when_the_session_cannot_open_a_tab(self, monkeypatch: pytest.MonkeyPatch) -> None:
        result, open_attempts, created_pages = await self._execute_against_tabless_session(
            monkeypatch, open_page_raises=True
        )

        assert (open_attempts, created_pages) == (1, 0)
        assert result.success is False
        assert result.status == BlockStatus.failed
        assert result.failure_reason == CODE_BLOCK_TAB_OPEN_FAILURE_REASON
        assert result.failure_reason != "No page found to run the code block"

    @pytest.mark.asyncio
    async def test_execute_does_not_relay_the_driver_error_name_when_the_tab_cannot_open(
        self, monkeypatch: pytest.MonkeyPatch
    ) -> None:
        """Playwright surfaces most driver failures as its base Error class, whose .name carries
        the real error (parse_error sets it); the class name alone would render as just (Error)."""
        from playwright.async_api import Error as PlaywrightError

        driver_error = PlaywrightError("BrowserContext.new_page: Cannot read properties of undefined")
        driver_error._name = "TypeError"

        result, _, _ = await self._execute_against_tabless_session(monkeypatch, open_page_error=driver_error)

        assert result.success is False
        assert driver_error.name == "TypeError"
        assert result.failure_reason == CODE_BLOCK_TAB_OPEN_FAILURE_REASON

    def test_poc_blocked_at_is_safe_code_gate(self) -> None:
        """The PoC payload is rejected before exec() is ever called."""
        malicious_code = "proc = await asyncio.create_subprocess_shell('id')"
        with pytest.raises(InsecureCodeDetected):
            CodeBlock.is_safe_code(malicious_code)

    @pytest.mark.asyncio
    async def test_runtime_asyncio_sleep_works(self) -> None:
        """asyncio.sleep should work in the sandbox."""
        fn = self._exec_user_code("await asyncio.sleep(0)")
        result = await fn()
        assert isinstance(result, dict)

    @pytest.mark.asyncio
    async def test_runtime_asyncio_has_no_subprocess(self) -> None:
        """asyncio.create_subprocess_shell must not exist at runtime."""
        fn = self._exec_user_code("x = asyncio.create_subprocess_shell")
        with pytest.raises(AttributeError):
            await fn()

    @pytest.mark.asyncio
    async def test_runtime_namespace_has_no_locals_builtin(self) -> None:
        """'locals' is not available as a callable in the sandbox."""
        fn = self._exec_user_code("x = locals()")
        with pytest.raises(NameError, match="locals"):
            await fn()

    @pytest.mark.asyncio
    async def test_runtime_builtins_are_empty(self) -> None:
        """Builtins like __import__, eval, exec are not available at runtime."""
        fn = self._exec_user_code("x = open('/etc/passwd')")
        with pytest.raises(NameError, match="open"):
            await fn()

    @pytest.mark.asyncio
    @pytest.mark.parametrize("name", sorted(ALWAYS_DENIED_BUILTINS | SANDBOX_ONLY_BUILTINS))
    async def test_denied_builtins_raise_name_error(self, name: str) -> None:
        fn = self._exec_user_code(f"x = {name}")
        with pytest.raises(NameError, match=name):
            await fn()

    @pytest.mark.asyncio
    async def test_common_builtins_execute(self) -> None:
        fn = self._exec_user_code(
            "paired = dict(zip(labels, values))\n"
            "try:\n"
            "    raise ValueError('x')\n"
            "except ValueError:\n"
            "    caught = 'value'\n"
            "day = datetime.date(2026, 9, 13).isoformat()\n",
            parameters={"labels": ["a", "b"], "values": [1, 2]},
        )
        result = await fn()
        assert result["paired"] == {"a": 1, "b": 2}
        assert result["caught"] == "value"
        assert result["day"] == "2026-09-13"

    @pytest.mark.asyncio
    async def test_parameters_shadow_builtin_names(self) -> None:
        now = datetime.now(UTC)
        block = CodeBlock(
            label="shadow_block",
            code="return {'zip_value': zip, 'len_value': len}",
            output_parameter=OutputParameter(
                parameter_type=ParameterType.OUTPUT,
                key="shadow_output",
                description="test output",
                output_parameter_id="op_shadow",
                workflow_id="w_test",
                created_at=now,
                modified_at=now,
            ),
        )

        user_function = block.generate_async_user_function(
            block.code, MagicMock(), parameters={"zip": "94105", "len": "persisted-len"}
        )

        assert await user_function() == {"zip_value": "94105", "len_value": "persisted-len"}

    @pytest.mark.asyncio
    async def test_datetime_reads_execute_through_the_real_legacy_user_function(self) -> None:
        code = (
            "utc_today = datetime.datetime.now(datetime.UTC).strftime('%m/%d/%Y')\n"
            "local_today = datetime.date.today()\n"
            "local_via_today = datetime.datetime.today().date()\n"
            "return {\n"
            "    'utc_today': utc_today,\n"
            "    'local_today': local_today.isoformat(),\n"
            "    'local_via_today': local_via_today.isoformat(),\n"
            "    'formatted': f'{local_today:%Y-%m-%d}',\n"
            "}"
        )
        now = datetime.now(UTC)
        block = CodeBlock(
            label="datetime_block",
            code=code,
            output_parameter=OutputParameter(
                parameter_type=ParameterType.OUTPUT,
                key="datetime_output",
                description="test output",
                output_parameter_id="op_datetime",
                workflow_id="w_test",
                created_at=now,
                modified_at=now,
            ),
        )
        for _ in range(3):
            utc_before = datetime.now(UTC).date()
            local_before = datetime.now().date()
            result = await block.generate_async_user_function(block.code, NoPageOperationPage(), parameters={})()
            utc_after = datetime.now(UTC).date()
            local_after = datetime.now().date()

            assert utc_before <= datetime.strptime(result["utc_today"], "%m/%d/%Y").date() <= utc_after
            assert local_before <= stdlib_date.fromisoformat(result["local_today"]) <= local_after
            assert local_before <= stdlib_date.fromisoformat(result["local_via_today"]) <= local_after
            assert result["formatted"] == result["local_today"]

    @pytest.mark.asyncio
    async def test_parameters_cannot_override_sandbox_internals(self) -> None:
        """Workflow parameters must not overwrite sandbox-controlled names."""
        import os

        # Try to inject the real os module via a parameter named "json"
        fn = self._exec_user_code(
            'x = json.dumps({"a": 1})',
            parameters={"json": os},
        )
        result = await fn()
        # json should still be the safe SimpleNamespace, not os
        assert result["x"] == '{"a": 1}'

    @pytest.mark.asyncio
    async def test_parameter_can_be_normalized_with_same_local_name(self) -> None:
        """Workflow parameters are usable without leaking wrapper defaults into output."""
        fn = self._exec_user_code(
            "person_name = person_name.strip()\nnormalized = person_name.upper()",
            parameters={"person_name": " Noor Assi "},
        )
        result = await fn()

        assert "person_name" not in result
        assert result["normalized"] == "NOOR ASSI"

    @pytest.mark.asyncio
    async def test_parameter_defaults_do_not_leak_into_implicit_output(self) -> None:
        fn = self._exec_user_code(
            "visible = public_value.upper()",
            parameters={"public_value": "ok", "secret_token": "sensitive"},
        )
        result = await fn()

        assert result == {"visible": "OK"}

    @pytest.mark.asyncio
    async def test_parameters_cannot_override_builtins(self) -> None:
        """A parameter named __builtins__ must not re-enable builtins."""
        import builtins

        fn = self._exec_user_code(
            "x = open('/etc/passwd')",
            parameters={"__builtins__": vars(builtins)},
        )
        with pytest.raises(NameError, match="open"):
            await fn()

    @pytest.mark.asyncio
    async def test_real_method_explicit_list_return_passes_through(self) -> None:
        """SKY-10789 regression: a parameterized code block ending in an explicit
        `return <list>` must yield the list, not crash on result.items().

        Exercises the real CodeBlock.generate_async_user_function (not the helper)
        so the fix is locked at the source. #11869 introduced the regression;
        without this guard it raised AttributeError: 'list' object has no attribute 'items'.
        """
        from unittest.mock import MagicMock

        now = datetime.now(timezone.utc)
        output_parameter = OutputParameter(
            parameter_type=ParameterType.OUTPUT,
            key="check_max_docs_output",
            description="test output",
            output_parameter_id="op_test_return",
            workflow_id="w_test",
            created_at=now,
            modified_at=now,
        )
        code = "candidates = diff_documents_output.get('candidates') or []\nreturn list(candidates)"
        block = CodeBlock(label="check_max_docs", code=code, output_parameter=output_parameter)
        fn = block.generate_async_user_function(
            block.code,
            MagicMock(),
            parameters={"diff_documents_output": {"candidates": [{"name": "a"}, {"name": "b"}]}},
        )
        result = await fn()
        assert result == [{"name": "a"}, {"name": "b"}]

    @pytest.mark.asyncio
    async def test_explicit_non_dict_return_does_not_leak_parameters(self) -> None:
        """A parameterized block returning a non-dict passes the value through verbatim."""
        fn = self._exec_user_code(
            "rows = [public_value, public_value]\nreturn rows",
            parameters={"public_value": "x", "secret_token": "sensitive"},
        )
        result = await fn()
        assert result == ["x", "x"]

    def test_wrapper_uses_capture_locals_not_locals(self) -> None:
        """Regression: the wrapper template must use __capture_locals(), not return locals()."""
        import textwrap

        code = "x = 1"
        indented = textwrap.indent(code, "    ")
        full_code = f"\nasync def wrapper():\n{indented}\n    return __capture_locals()\n"
        assert "__capture_locals()" in full_code
        assert "return locals()" not in full_code


# ---------------------------------------------------------------------------
# Fill-time OTP primitive (SKY-10938)
# ---------------------------------------------------------------------------

# RFC 6238 published test seed — safe to ship in OSS-synced fixtures.
_RFC_TOTP_SEED = "JBSWY3DPEHPK3PXP"
_CREDENTIAL_KEY = "login_credential"
_WORKFLOW_RUN_ID = "wr_otp_test"
_ORG_ID = "o_otp_test"
_TOTP_ANCHOR = datetime(2026, 6, 14, 12, 0, 0)


def _freeze_totp_clock(monkeypatch: pytest.MonkeyPatch, offset_seconds: int = 0) -> None:
    """Pin pyotp's clock so a test's expected code and the block's minted code share one 30s window.
    pyotp.TOTP.now() reads pyotp.totp.datetime.datetime.now()."""
    frozen = _TOTP_ANCHOR + timedelta(seconds=offset_seconds)

    class _FrozenDatetime(datetime):
        @classmethod
        def now(cls, tz: object = None) -> datetime:
            return frozen

    monkeypatch.setattr(pyotp_totp, "datetime", SimpleNamespace(datetime=_FrozenDatetime))


def _register_credential_parameter(wrc, key: str = _CREDENTIAL_KEY) -> None:
    """Register a real CredentialParameter under ``key``, mirroring production so
    is_registered_credential_parameter_key gates the credential-only TOTP paths."""
    now = datetime.now(timezone.utc)
    wrc.parameters[key] = CredentialParameter(
        key=key,
        credential_parameter_id=f"cp_{key}",
        workflow_id="w",
        credential_id="vault:item",
        created_at=now,
        modified_at=now,
    )


def _build_wrc_with_totp_seed(seed: str = _RFC_TOTP_SEED):
    """Build a real WorkflowRunContext carrying a TOTP-bearing credential.

    Mirrors the registration shape produced by
    context_manager._register_credential_parameter_value: the credential value dict holds a
    'totp' placeholder id; secrets maps that id to the TOTP sentinel and the
    totp_secret_value_key to the real seed.
    """
    from unittest.mock import MagicMock

    from skyvern.forge.sdk.services.bitwarden import BitwardenConstants
    from skyvern.forge.sdk.workflow.context_manager import WorkflowRunContext

    wrc = WorkflowRunContext(
        workflow_title="t",
        workflow_id="w",
        workflow_permanent_id="wpid",
        workflow_run_id=_WORKFLOW_RUN_ID,
        aws_client=MagicMock(),
        mask_secrets=True,
    )
    totp_secret_id = wrc.generate_random_secret_id() + "_totp"
    wrc.secrets[totp_secret_id] = BitwardenConstants.TOTP
    wrc.secrets[wrc.totp_secret_value_key(totp_secret_id)] = seed
    wrc.values[_CREDENTIAL_KEY] = {"context": "placeholder note", "totp": totp_secret_id}
    _register_credential_parameter(wrc)
    return wrc


def _build_wrc_with_identifier(identifier: str = "[email protected]"):
    """Build a real WorkflowRunContext with an email/SMS identifier and no TOTP seed."""
    from unittest.mock import MagicMock

    from skyvern.forge.sdk.workflow.context_manager import WorkflowRunContext

    wrc = WorkflowRunContext(
        workflow_title="t",
        workflow_id="w",
        workflow_permanent_id="wpid",
        workflow_run_id=_WORKFLOW_RUN_ID,
        aws_client=MagicMock(),
        mask_secrets=True,
    )
    wrc.values[_CREDENTIAL_KEY] = {"context": "placeholder note"}
    wrc.credential_totp_identifiers[_CREDENTIAL_KEY] = identifier
    _register_credential_parameter(wrc)
    return wrc


class _FakeWorkflowRun:
    def __init__(self) -> None:
        self.workflow_id = "w"
        self.workflow_permanent_id = "wpid"
        # Naive, like workflow_runs.started_at (Column(DateTime)). An aware value here would let a
        # local-clock anchor pass the call-time assertion below in every timezone.
        self.started_at = datetime(2026, 6, 14, 0, 0, 0)


def _fake_code_block_page(navigations: list | None = None, fills: list | None = None):
    """A page the production binder will accept: it type-checks as a real Playwright Page."""
    from unittest.mock import MagicMock

    from playwright.async_api import Page

    page = MagicMock(spec=Page)

    async def goto(url: str, **kwargs: object) -> None:
        if navigations is not None:
            navigations.append(url)
        return None

    async def fill(*args: object, **kwargs: object) -> None:
        if fills is not None:
            fills.append(args)

    page.goto = goto
    page.fill = fill
    return page


def _patch_context_resolution(monkeypatch: "pytest.MonkeyPatch", wrc) -> None:
    monkeypatch.setattr(
        "skyvern.forge.sdk.workflow.models.block.app.WORKFLOW_CONTEXT_MANAGER.get_workflow_run_context",
        lambda *args, **kwargs: wrc,
    )


async def _run_credential_code_block(monkeypatch: "pytest.MonkeyPatch", wrc, code: str, label: str):
    """Execute a CodeBlock with a TOTP-bearing credential parameter through the real execute path.

    Returns (block_result, persisted_output_value). Mirrors the credential bind + persist flow so
    masking and failure-reason handling are exercised end-to-end.
    """
    from unittest.mock import MagicMock

    from skyvern.forge.sdk.workflow.models.parameter import (
        CredentialParameter,
        OutputParameter,
        ParameterType,
    )

    class FakeBrowserState:
        def __init__(self) -> None:
            self.browser_artifacts = BrowserArtifacts()

        async def get_working_page(self) -> object:
            return MagicMock()

    async def validate_code_block(*args: object, **kwargs: object) -> None:
        return None

    async def get_browser_state(*args: object, **kwargs: object) -> FakeBrowserState:
        return FakeBrowserState()

    persisted: dict[str, object] = {}

    async def record_output(self: object, ctx: object, run_id: object, value: object) -> None:
        persisted["value"] = value

    monkeypatch.setattr(
        "skyvern.forge.sdk.workflow.models.block.app.AGENT_FUNCTION.validate_code_block",
        validate_code_block,
    )
    monkeypatch.setattr(CodeBlock, "get_or_create_browser_state", get_browser_state)
    monkeypatch.setattr(CodeBlock, "get_workflow_run_context", lambda self, run_id: wrc)
    monkeypatch.setattr(CodeBlock, "record_output_parameter_value", record_output)
    _patch_context_resolution(monkeypatch, wrc)

    now = datetime.now(timezone.utc)
    output_parameter = OutputParameter(
        parameter_type=ParameterType.OUTPUT,
        key=f"{label}_output",
        description="test output",
        output_parameter_id=f"op_{label}",
        workflow_id="w",
        created_at=now,
        modified_at=now,
    )
    credential_parameter = CredentialParameter(
        parameter_type=ParameterType.CREDENTIAL,
        key=_CREDENTIAL_KEY,
        description="cred",
        workflow_id="w",
        credential_parameter_id=f"cp_{label}",
        credential_id="vault:item",
        created_at=now,
        modified_at=now,
    )
    block = CodeBlock(
        label=label,
        code=code,
        output_parameter=output_parameter,
        parameters=[credential_parameter],
    )
    result = await block.execute(workflow_run_id=_WORKFLOW_RUN_ID, workflow_run_block_id="")
    return result, persisted.get("value")


class TestCodeBlockOtpMintAtCallTime:
    """AC1/AC4: the primitive re-mints the TOTP at call time, not at block start."""

    @pytest.mark.asyncio
    async def test_otp_returns_current_totp(self, monkeypatch: pytest.MonkeyPatch) -> None:
        import pyotp

        from skyvern.forge.sdk.workflow.models.block import _resolve_code_block_otp

        _freeze_totp_clock(monkeypatch)
        wrc = _build_wrc_with_totp_seed()
        _patch_context_resolution(monkeypatch, wrc)

        code = await _resolve_code_block_otp(_CREDENTIAL_KEY, _ORG_ID, _WORKFLOW_RUN_ID, budget_seconds=120)

        assert code == pyotp.TOTP(_RFC_TOTP_SEED).now()
        assert len(code) == 6 and code.isdigit()

    @pytest.mark.asyncio
    async def test_remint_differs_after_clock_advance(self, monkeypatch: pytest.MonkeyPatch) -> None:
        """Advancing the clock past a rotation yields a different code — proves re-mint, not the
        stale pre-minted attribute."""
        from skyvern.forge.sdk.workflow.models.block import _resolve_code_block_otp

        wrc = _build_wrc_with_totp_seed()
        _patch_context_resolution(monkeypatch, wrc)

        _freeze_totp_clock(monkeypatch)
        first = await _resolve_code_block_otp(_CREDENTIAL_KEY, _ORG_ID, _WORKFLOW_RUN_ID, budget_seconds=120)

        _freeze_totp_clock(monkeypatch, offset_seconds=60)
        second = await _resolve_code_block_otp(_CREDENTIAL_KEY, _ORG_ID, _WORKFLOW_RUN_ID, budget_seconds=120)

        assert first != second

    @pytest.mark.asyncio
    async def test_returned_code_is_not_the_seed(self, monkeypatch: pytest.MonkeyPatch) -> None:
        from skyvern.forge.sdk.workflow.models.block import _resolve_code_block_otp

        wrc = _build_wrc_with_totp_seed()
        _patch_context_resolution(monkeypatch, wrc)

        code = await _resolve_code_block_otp(_CREDENTIAL_KEY, _ORG_ID, _WORKFLOW_RUN_ID, budget_seconds=120)
        assert code != _RFC_TOTP_SEED

    @pytest.mark.asyncio
    async def test_minted_code_registered_in_secrets(self, monkeypatch: pytest.MonkeyPatch) -> None:
        from skyvern.forge.sdk.workflow.models.block import _resolve_code_block_otp

        wrc = _build_wrc_with_totp_seed()
        _patch_context_resolution(monkeypatch, wrc)

        code = await _resolve_code_block_otp(_CREDENTIAL_KEY, _ORG_ID, _WORKFLOW_RUN_ID, budget_seconds=120)
        assert code in set(wrc.secrets.values())


class TestCodeBlockOtpIdentifierFetch:
    """AC2: a credential with a totp_identifier polls via otp_service with the right anchor."""

    @pytest.mark.asyncio
    async def test_fetches_via_poll_with_run_start_anchor(self, monkeypatch: pytest.MonkeyPatch) -> None:
        from skyvern.forge.sdk.workflow.models import block as block_module
        from skyvern.forge.sdk.workflow.models.block import _resolve_code_block_otp
        from skyvern.services.otp_service import OTPValue

        wrc = _build_wrc_with_identifier()
        _patch_context_resolution(monkeypatch, wrc)

        fake_run = _FakeWorkflowRun()

        async def fake_get_workflow_run(*args: object, **kwargs: object) -> _FakeWorkflowRun:
            return fake_run

        monkeypatch.setattr(
            block_module.app.DATABASE.workflow_runs, "get_workflow_run", fake_get_workflow_run, raising=False
        )

        captured: dict[str, object] = {}

        async def fake_poll(**kwargs: object) -> OTPValue:
            captured.update(kwargs)
            return OTPValue(value="246810")

        monkeypatch.setattr(block_module.otp_service, "poll_otp_value", fake_poll)

        code = await _resolve_code_block_otp(_CREDENTIAL_KEY, _ORG_ID, _WORKFLOW_RUN_ID, budget_seconds=120)

        assert code == "246810"
        assert captured["totp_identifier"] == "[email protected]"
        assert captured["created_after"] == fake_run.started_at
        assert captured["organization_id"] == _ORG_ID
        # The budget is the in-block setting, never the legacy 15-minute window.
        assert "timeout" not in captured

    @pytest.mark.asyncio
    async def test_fetched_code_registered_in_secrets(self, monkeypatch: pytest.MonkeyPatch) -> None:
        from skyvern.forge.sdk.workflow.models import block as block_module
        from skyvern.forge.sdk.workflow.models.block import _resolve_code_block_otp
        from skyvern.services.otp_service import OTPValue

        wrc = _build_wrc_with_identifier()
        _patch_context_resolution(monkeypatch, wrc)

        async def fake_get_workflow_run(*args: object, **kwargs: object) -> _FakeWorkflowRun:
            return _FakeWorkflowRun()

        async def fake_poll(**kwargs: object) -> OTPValue:
            return OTPValue(value="135790")

        monkeypatch.setattr(
            block_module.app.DATABASE.workflow_runs, "get_workflow_run", fake_get_workflow_run, raising=False
        )
        monkeypatch.setattr(block_module.otp_service, "poll_otp_value", fake_poll)

        code = await _resolve_code_block_otp(_CREDENTIAL_KEY, _ORG_ID, _WORKFLOW_RUN_ID, budget_seconds=120)
        assert code == "135790"
        assert "135790" in set(wrc.secrets.values())

    @pytest.mark.asyncio
    async def test_magic_link_returned_as_string(self, monkeypatch: pytest.MonkeyPatch) -> None:
        from skyvern.forge.sdk.workflow.models import block as block_module
        from skyvern.forge.sdk.workflow.models.block import _resolve_code_block_otp
        from skyvern.services.otp_service import OTPValue

        wrc = _build_wrc_with_identifier()
        _patch_context_resolution(monkeypatch, wrc)

        async def fake_get_workflow_run(*args: object, **kwargs: object) -> _FakeWorkflowRun:
            return _FakeWorkflowRun()

        link = "https://example.com/magic?token=abc"

        async def fake_poll(**kwargs: object) -> OTPValue:
            return OTPValue(value=link)

        monkeypatch.setattr(
            block_module.app.DATABASE.workflow_runs, "get_workflow_run", fake_get_workflow_run, raising=False
        )
        monkeypatch.setattr(block_module.otp_service, "poll_otp_value", fake_poll)

        code = await _resolve_code_block_otp(_CREDENTIAL_KEY, _ORG_ID, _WORKFLOW_RUN_ID, budget_seconds=120)
        assert code == link
        assert link in set(wrc.secrets.values())


_FillContexts = Callable[[EmailOTPVerificationContext, RawOTPVerificationContext], None]


class TestCodeBlockOtpBudget:
    """AC2: the in-block poll budget raises a clear error, not the opaque 300s kill."""

    @pytest.mark.asyncio
    async def test_poll_timeout_raises_clear_error(self, monkeypatch: pytest.MonkeyPatch) -> None:
        from skyvern.forge.sdk.workflow.models import block as block_module
        from skyvern.forge.sdk.workflow.models.block import CodeBlockOTPError, _resolve_code_block_otp

        wrc = _build_wrc_with_identifier()
        _patch_context_resolution(monkeypatch, wrc)

        async def fake_get_workflow_run(*args: object, **kwargs: object) -> _FakeWorkflowRun:
            return _FakeWorkflowRun()

        async def raises_timeout(**kwargs: object) -> object:
            # Stand in for wait_for exhausting the budget; the translation to
            # CodeBlockOTPError is what this test exercises (no real wall-clock wait).
            raise asyncio.TimeoutError

        monkeypatch.setattr(
            block_module.app.DATABASE.workflow_runs, "get_workflow_run", fake_get_workflow_run, raising=False
        )
        monkeypatch.setattr(block_module.otp_service, "poll_otp_value", raises_timeout)

        with pytest.raises(CodeBlockOTPError, match="within 1 seconds"):
            await _resolve_code_block_otp(_CREDENTIAL_KEY, _ORG_ID, _WORKFLOW_RUN_ID, budget_seconds=1)

    @pytest.mark.asyncio
    async def test_poll_timeout_error_is_not_asyncio_timeout(self, monkeypatch: pytest.MonkeyPatch) -> None:
        """The in-block budget surfaces CodeBlockOTPError, not the bare asyncio.TimeoutError
        that the outer 300s wait_for would raise."""
        from skyvern.forge.sdk.workflow.models import block as block_module
        from skyvern.forge.sdk.workflow.models.block import CodeBlockOTPError, _resolve_code_block_otp

        wrc = _build_wrc_with_identifier()
        _patch_context_resolution(monkeypatch, wrc)

        async def fake_get_workflow_run(*args: object, **kwargs: object) -> _FakeWorkflowRun:
            return _FakeWorkflowRun()

        async def raises_timeout(**kwargs: object) -> object:
            # Stand in for wait_for exhausting the budget; the translation to
            # CodeBlockOTPError is what this test exercises (no real wall-clock wait).
            raise asyncio.TimeoutError

        monkeypatch.setattr(
            block_module.app.DATABASE.workflow_runs, "get_workflow_run", fake_get_workflow_run, raising=False
        )
        monkeypatch.setattr(block_module.otp_service, "poll_otp_value", raises_timeout)

        raised: Exception | None = None
        try:
            await _resolve_code_block_otp(_CREDENTIAL_KEY, _ORG_ID, _WORKFLOW_RUN_ID, budget_seconds=1)
        except Exception as e:  # noqa: BLE001
            raised = e
        assert isinstance(raised, CodeBlockOTPError)
        assert not isinstance(raised, asyncio.TimeoutError)

    @pytest.mark.asyncio
    async def test_poll_exception_is_sanitized(self, monkeypatch: pytest.MonkeyPatch) -> None:
        """A poll failure carrying the identifier must not leak it through the in-block error."""
        from skyvern.exceptions import NoTOTPVerificationCodeFound
        from skyvern.forge.sdk.workflow.models import block as block_module
        from skyvern.forge.sdk.workflow.models.block import CodeBlockOTPError, _resolve_code_block_otp

        wrc = _build_wrc_with_identifier(identifier="[email protected]")
        _patch_context_resolution(monkeypatch, wrc)

        async def fake_get_workflow_run(*args: object, **kwargs: object) -> _FakeWorkflowRun:
            return _FakeWorkflowRun()

        async def failing_poll(**kwargs: object) -> object:
            raise NoTOTPVerificationCodeFound(totp_identifier="[email protected]")

        monkeypatch.setattr(
            block_module.app.DATABASE.workflow_runs, "get_workflow_run", fake_get_workflow_run, raising=False
        )
        monkeypatch.setattr(block_module.otp_service, "poll_otp_value", failing_poll)

        with pytest.raises(CodeBlockOTPError) as exc_info:
            await _resolve_code_block_otp(_CREDENTIAL_KEY, _ORG_ID, _WORKFLOW_RUN_ID, budget_seconds=120)
        assert "[email protected]" not in str(exc_info.value)

    @staticmethod
    async def _timeout_after(monkeypatch: pytest.MonkeyPatch, fill_contexts: _FillContexts, identifier: str) -> str:
        _patch_context_resolution(monkeypatch, _build_wrc_with_identifier(identifier=identifier))

        async def fake_get_workflow_run(*args: object, **kwargs: object) -> _FakeWorkflowRun:
            return _FakeWorkflowRun()

        async def fake_poll(**kwargs: object):
            fill_contexts(kwargs["email_context"], kwargs["raw_context"])
            raise asyncio.TimeoutError

        monkeypatch.setattr(
            block_module.app.DATABASE.workflow_runs, "get_workflow_run", fake_get_workflow_run, raising=False
        )
        monkeypatch.setattr(block_module.otp_service, "poll_otp_value", fake_poll)
        with pytest.raises(CodeBlockOTPError) as exc_info:
            await _resolve_code_block_otp(_CREDENTIAL_KEY, _ORG_ID, _WORKFLOW_RUN_ID, budget_seconds=120)
        return str(exc_info.value)

    @staticmethod
    def _no_inboxes(email_context: EmailOTPVerificationContext, raw_context: RawOTPVerificationContext) -> None:
        email_context.for_source(GmailOTPSource.name).credential_ids = []
        email_context.for_source(OutlookOTPSource.name).credential_ids = []
        raw_context.store_queried = True

    @staticmethod
    def _gmail_never_loaded(email_context: EmailOTPVerificationContext, raw_context: RawOTPVerificationContext) -> None:
        email_context.for_source(GmailOTPSource.name)
        email_context.for_source(OutlookOTPSource.name).credential_ids = []
        raw_context.store_queried = True

    @staticmethod
    def _one_gmail_inbox_seven_messages(
        email_context: EmailOTPVerificationContext, raw_context: RawOTPVerificationContext
    ) -> None:
        gmail = email_context.for_source(GmailOTPSource.name)
        gmail.credential_ids = ["cred_1"]
        for index in range(7):
            gmail.remember_message("cred_1", f"msg_{index}")
        gmail.completed_credential_ids.add("cred_1")
        email_context.for_source(OutlookOTPSource.name).credential_ids = []
        raw_context.store_queried = True

    @staticmethod
    def _one_gmail_inbox_past_the_cap(
        email_context: EmailOTPVerificationContext, raw_context: RawOTPVerificationContext
    ) -> None:
        gmail = email_context.for_source(GmailOTPSource.name)
        gmail.credential_ids = ["cred_1"]
        for index in range(MAX_SEEN_EMAIL_MESSAGE_IDS + 5):
            gmail.remember_message("cred_1", f"msg_{index}")
        gmail.completed_credential_ids.add("cred_1")
        email_context.for_source(OutlookOTPSource.name).credential_ids = []
        raw_context.store_queried = True

    @staticmethod
    def _nothing_consulted(email_context: EmailOTPVerificationContext, raw_context: RawOTPVerificationContext) -> None:
        return None

    @staticmethod
    def _one_gmail_inbox_search_failed(
        email_context: EmailOTPVerificationContext, raw_context: RawOTPVerificationContext
    ) -> None:
        gmail = email_context.for_source(GmailOTPSource.name)
        gmail.credential_ids = ["cred_1"]
        gmail.failed_credential_ids.add("cred_1")
        email_context.for_source(OutlookOTPSource.name).credential_ids = []
        raw_context.store_queried = True

    @staticmethod
    def _one_gmail_inbox_unreadable_message(
        email_context: EmailOTPVerificationContext, raw_context: RawOTPVerificationContext
    ) -> None:
        gmail = email_context.for_source(GmailOTPSource.name)
        gmail.credential_ids = ["cred_1"]
        gmail.unreadable_message_keys.update({("cred_1", "msg_a"), ("cred_1", "msg_b")})
        gmail.completed_credential_ids.add("cred_1")
        email_context.for_source(OutlookOTPSource.name).credential_ids = []
        raw_context.store_queried = True

    @staticmethod
    def _one_gmail_inbox_search_cancelled(
        email_context: EmailOTPVerificationContext, raw_context: RawOTPVerificationContext
    ) -> None:
        gmail = email_context.for_source(GmailOTPSource.name)
        gmail.credential_ids = ["cred_1"]
        email_context.for_source(OutlookOTPSource.name).credential_ids = []
        raw_context.store_queried = True

    @staticmethod
    def _two_gmail_inboxes_one_still_searching(
        email_context: EmailOTPVerificationContext, raw_context: RawOTPVerificationContext
    ) -> None:
        gmail = email_context.for_source(GmailOTPSource.name)
        gmail.credential_ids = ["cred_done", "cred_pending"]
        gmail.completed_credential_ids.add("cred_done")
        email_context.for_source(OutlookOTPSource.name).credential_ids = []
        raw_context.store_queried = True

    @staticmethod
    def _two_gmail_inboxes_one_answered_one_pending(
        email_context: EmailOTPVerificationContext, raw_context: RawOTPVerificationContext
    ) -> None:
        gmail = email_context.for_source(GmailOTPSource.name)
        gmail.credential_ids = ["cred_done", "cred_pending"]
        gmail.completed_credential_ids.add("cred_done")
        for index in range(3):
            gmail.remember_message("cred_done", f"msg_{index}")
        email_context.for_source(OutlookOTPSource.name).credential_ids = []
        raw_context.store_queried = True

    @staticmethod
    def _one_gmail_inbox_read_some_and_refused_one(
        email_context: EmailOTPVerificationContext, raw_context: RawOTPVerificationContext
    ) -> None:
        gmail = email_context.for_source(GmailOTPSource.name)
        gmail.credential_ids = ["cred_1"]
        gmail.completed_credential_ids.add("cred_1")
        for index in range(3):
            gmail.remember_message("cred_1", f"msg_{index}")
        gmail.unreadable_message_keys.add(("cred_1", "msg_refused"))
        email_context.for_source(OutlookOTPSource.name).credential_ids = []
        raw_context.store_queried = True

    @staticmethod
    def _gmail_inbox_replaced_after_reading(
        email_context: EmailOTPVerificationContext, raw_context: RawOTPVerificationContext
    ) -> None:
        gmail = email_context.for_source(GmailOTPSource.name)
        for index in range(3):
            gmail.remember_message("cred_gone", f"msg_{index}")
        gmail.unreadable_message_keys.add(("cred_gone", "msg_refused"))
        gmail.credential_ids = ["cred_new"]
        gmail.completed_credential_ids.add("cred_new")
        email_context.for_source(OutlookOTPSource.name).credential_ids = []
        raw_context.store_queried = True

    @staticmethod
    def _gmail_list_went_stale(
        email_context: EmailOTPVerificationContext, raw_context: RawOTPVerificationContext
    ) -> None:
        gmail = email_context.for_source(GmailOTPSource.name)
        gmail.credential_ids = []
        gmail.credential_list_refresh_failed = True
        outlook = email_context.for_source(OutlookOTPSource.name)
        outlook.credential_ids = []
        raw_context.store_queried = True

    @staticmethod
    def _evicted_then_one_inbox_disconnected(
        email_context: EmailOTPVerificationContext, raw_context: RawOTPVerificationContext
    ) -> None:
        gmail = email_context.for_source(GmailOTPSource.name)
        for index in range(MAX_SEEN_EMAIL_MESSAGE_IDS):
            gmail.remember_message("cred_kept", f"kept_{index}")
        for index in range(100):
            gmail.remember_message("cred_gone", f"gone_{index}")
        gmail.credential_ids = ["cred_kept"]
        gmail.completed_credential_ids.add("cred_kept")
        email_context.for_source(OutlookOTPSource.name).credential_ids = []
        raw_context.store_queried = True

    @staticmethod
    def _gmail_inbox_searched_then_failed(
        email_context: EmailOTPVerificationContext, raw_context: RawOTPVerificationContext
    ) -> None:
        gmail = email_context.for_source(GmailOTPSource.name)
        gmail.credential_ids = ["cred_1"]
        gmail.completed_credential_ids.add("cred_1")
        gmail.failed_credential_ids.add("cred_1")
        email_context.for_source(OutlookOTPSource.name).credential_ids = []
        raw_context.store_queried = True

    @staticmethod
    def _one_gmail_inbox_one_message(
        email_context: EmailOTPVerificationContext, raw_context: RawOTPVerificationContext
    ) -> None:
        gmail = email_context.for_source(GmailOTPSource.name)
        gmail.credential_ids = ["cred_1"]
        gmail.completed_credential_ids.add("cred_1")
        gmail.remember_message("cred_1", "msg_only")
        email_context.for_source(OutlookOTPSource.name).credential_ids = []
        raw_context.store_queried = True

    @staticmethod
    def _store_queried_only(email_context: EmailOTPVerificationContext, raw_context: RawOTPVerificationContext) -> None:
        raw_context.store_queried = True

    @pytest.mark.asyncio
    @pytest.mark.parametrize(
        ("identifier", "fill_contexts", "present", "absent"),
        [
            (
                "[email protected]",
                _no_inboxes,
                ["No Gmail or Outlook inbox is connected", "no code has been stored"],
                [],
            ),
            (
                "[email protected]",
                _gmail_never_loaded,
                ["Gmail inbox list never loaded", "no Outlook inbox connected"],
                ["no Gmail inbox"],
            ),
            ("[email protected]", _one_gmail_inbox_seven_messages, ["1 Gmail inbox connected, 7 messages checked"], []),
            (
                "[email protected]",
                _one_gmail_inbox_past_the_cap,
                ["1 Gmail inbox connected, 500+ messages checked"],
                ["505 messages"],
            ),
            (
                "[email protected]",
                _one_gmail_inbox_search_failed,
                ["0 of 1 Gmail inbox searched, search failed"],
                ["no messages read", "messages checked"],
            ),
            (
                "[email protected]",
                _one_gmail_inbox_unreadable_message,
                ["1 Gmail inbox connected, 2 messages could not be read"],
                ["no messages read", "search failed"],
            ),
            (
                "[email protected]",
                _one_gmail_inbox_search_cancelled,
                ["0 of 1 Gmail inbox searched, no messages read"],
                ["1 Gmail inbox connected", "search failed"],
            ),
            (
                "[email protected]",
                _two_gmail_inboxes_one_still_searching,
                ["1 of 2 Gmail inboxes searched, no messages read"],
                ["2 Gmail inboxes connected"],
            ),
            (
                "[email protected]",
                _two_gmail_inboxes_one_answered_one_pending,
                ["1 of 2 Gmail inboxes searched, 3 messages checked"],
                ["2 Gmail inboxes connected"],
            ),
            (
                "[email protected]",
                _one_gmail_inbox_read_some_and_refused_one,
                ["1 Gmail inbox connected, 3 of 4 messages read"],
                ["3 messages checked"],
            ),
            (
                "[email protected]",
                _gmail_inbox_replaced_after_reading,
                ["1 Gmail inbox connected, no messages read"],
                ["3 messages", "of 4 messages", "could not be read"],
            ),
            (
                "[email protected]",
                _gmail_list_went_stale,
                ["Gmail inbox list not refreshed", "no Outlook inbox connected"],
                ["no Gmail inbox connected", "No Gmail or Outlook inbox is connected"],
            ),
            (
                "[email protected]",
                _evicted_then_one_inbox_disconnected,
                ["1 Gmail inbox connected, 400+ messages checked"],
                ["400 messages checked"],
            ),
            (
                "[email protected]",
                _gmail_inbox_searched_then_failed,
                ["1 Gmail inbox connected, search failed"],
                ["no messages read", "0 of 1"],
            ),
            (
                "[email protected]",
                _one_gmail_inbox_one_message,
                ["1 Gmail inbox connected, 1 message checked"],
                ["1 messages checked"],
            ),
            (
                "[email protected]",
                _nothing_consulted,
                ["No delivery source was checked before the wait ended."],
                ["No Gmail or Outlook inbox is connected", "no code has been stored"],
            ),
            (
                "+15555550147",
                _store_queried_only,
                ["no code has been stored"],
                ["Gmail", "Outlook", "inbox"],
            ),
        ],
        ids=[
            "zero-sources",
            "never-loaded",
            "one-inbox",
            "capped-inbox",
            "search-failed",
            "unreadable",
            "search-cancelled",
            "one-of-two-pending",
            "partial-with-messages",
            "read-some-refused-one",
            "inbox-replaced",
            "list-stale",
            "evicted-then-disconnected",
            "searched-then-failed",
            "one-message-singular",
            "nothing-queried",
            "sms-identifier",
        ],
    )
    async def test_timeout_names_the_sources_it_checked(
        self,
        monkeypatch: pytest.MonkeyPatch,
        identifier: str,
        fill_contexts: _FillContexts,
        present: list[str],
        absent: list[str],
    ) -> None:
        message = await self._timeout_after(monkeypatch, fill_contexts, identifier)

        assert message != "OTP was not received within 120 seconds."
        assert message.endswith("OTP was not received within 120 seconds.")
        # Every downstream cap slices a prefix, the shortest being 120 chars in copilot turn
        # compaction, applied after a ~45-char platform frame. The facts have to fit what is left.
        for fact in present:
            assert message.index(fact) + len(fact) <= 75
        for fact in present:
            assert fact in message
        for fact in absent:
            assert fact not in message

    @pytest.mark.asyncio
    async def test_timeout_sources_render_counts_only(self, monkeypatch: pytest.MonkeyPatch) -> None:
        def fill_contexts(email_context: EmailOTPVerificationContext, raw_context: RawOTPVerificationContext) -> None:
            gmail = email_context.for_source(GmailOTPSource.name)
            gmail.credential_ids = ["cred_redact_91c2"]
            gmail.remember_message("cred_redact_91c2", "msg_redact_44d0")
            gmail.provider_state["cred_redact_91c2"] = {"last_code": "918273"}
            raw_context.seen_row_ids.add("otp_row_redact_5e1b")
            raw_context.misses.add(("otp_row_redact_5e1b", OTPType.TOTP))
            raw_context.store_queried = True

        message = await self._timeout_after(monkeypatch, fill_contexts, "[email protected]")

        assert "1 stored OTP message found" in message
        for secret in ("[email protected]", "cred_redact_91c2", "msg_redact_44d0", "918273", "otp_row"):
            assert secret not in message


class TestCodeBlockOtpNoSource:
    """A credential with neither a seed nor an identifier raises a clear error."""

    @pytest.mark.asyncio
    async def test_no_source_raises(self, monkeypatch: pytest.MonkeyPatch) -> None:
        from unittest.mock import MagicMock

        from skyvern.forge.sdk.workflow.context_manager import WorkflowRunContext
        from skyvern.forge.sdk.workflow.models.block import CodeBlockOTPError, _resolve_code_block_otp

        wrc = WorkflowRunContext(
            workflow_title="t",
            workflow_id="w",
            workflow_permanent_id="wpid",
            workflow_run_id=_WORKFLOW_RUN_ID,
            aws_client=MagicMock(),
        )
        wrc.values[_CREDENTIAL_KEY] = {"context": "placeholder note"}
        _patch_context_resolution(monkeypatch, wrc)

        with pytest.raises(CodeBlockOTPError, match="No OTP source"):
            await _resolve_code_block_otp(_CREDENTIAL_KEY, _ORG_ID, _WORKFLOW_RUN_ID, budget_seconds=120)

    @pytest.mark.asyncio
    async def test_missing_org_id_for_poll_raises(self, monkeypatch: pytest.MonkeyPatch) -> None:
        from skyvern.forge.sdk.workflow.models.block import CodeBlockOTPError, _resolve_code_block_otp

        wrc = _build_wrc_with_identifier()
        _patch_context_resolution(monkeypatch, wrc)

        with pytest.raises(CodeBlockOTPError, match="organization"):
            await _resolve_code_block_otp(_CREDENTIAL_KEY, None, _WORKFLOW_RUN_ID, budget_seconds=120)

    @pytest.mark.asyncio
    async def test_missing_run_context_raises(self, monkeypatch: pytest.MonkeyPatch) -> None:
        from skyvern.forge.sdk.workflow.models import block as block_module
        from skyvern.forge.sdk.workflow.models.block import CodeBlockOTPError, _resolve_code_block_otp

        monkeypatch.setattr(
            block_module.app.WORKFLOW_CONTEXT_MANAGER,
            "get_workflow_run_context",
            lambda *args, **kwargs: None,
        )
        with pytest.raises(CodeBlockOTPError, match="context"):
            await _resolve_code_block_otp(_CREDENTIAL_KEY, _ORG_ID, _WORKFLOW_RUN_ID, budget_seconds=120)


def _rebind_as_workflow_parameter(wrc, parameter_type: WorkflowParameterType, key: str = _CREDENTIAL_KEY) -> None:
    now = datetime.now(timezone.utc)
    wrc.parameters[key] = WorkflowParameter(
        key=key,
        workflow_parameter_id=f"wp_{key}",
        workflow_parameter_type=parameter_type,
        workflow_id="w",
        default_value="cred_1",
        created_at=now,
        modified_at=now,
    )


class TestCodeBlockOtpCredentialIdWorkflowParameter:
    """A credential bound as a ``credential_id`` workflow parameter — the shape code-first
    authoring emits — owns its TOTP seed just as a CredentialParameter does."""

    @pytest.mark.asyncio
    async def test_credential_id_workflow_parameter_mints_the_seed(self, monkeypatch: pytest.MonkeyPatch) -> None:
        import pyotp

        from skyvern.forge.sdk.workflow.models.block import _resolve_code_block_otp

        _freeze_totp_clock(monkeypatch)
        wrc = _build_wrc_with_totp_seed()
        _rebind_as_workflow_parameter(wrc, WorkflowParameterType.CREDENTIAL_ID)
        _patch_context_resolution(monkeypatch, wrc)

        code = await _resolve_code_block_otp(_CREDENTIAL_KEY, _ORG_ID, _WORKFLOW_RUN_ID, budget_seconds=120)

        assert code == pyotp.TOTP(_RFC_TOTP_SEED).now()

    @pytest.mark.asyncio
    async def test_ordinary_workflow_parameter_is_still_excluded(self, monkeypatch: pytest.MonkeyPatch) -> None:
        """A run input that merely carries a ``totp``-shaped dict must not reach the seed."""
        from skyvern.forge.sdk.workflow.models.block import CodeBlockOTPError, _resolve_code_block_otp

        wrc = _build_wrc_with_totp_seed()
        _rebind_as_workflow_parameter(wrc, WorkflowParameterType.JSON)
        _patch_context_resolution(monkeypatch, wrc)

        with pytest.raises(CodeBlockOTPError, match="No OTP source"):
            await _resolve_code_block_otp(_CREDENTIAL_KEY, _ORG_ID, _WORKFLOW_RUN_ID, budget_seconds=120)


class TestCodeBlockOtpSeedConfinement:
    """The seed must stay unreachable from the user-facing bound method/builtin."""

    def test_bound_method_does_not_capture_wrc_or_seed(self) -> None:
        from skyvern.forge.sdk.workflow.models.block import _bind_code_block_otp

        bound = _bind_code_block_otp(_CREDENTIAL_KEY, _ORG_ID, _WORKFLOW_RUN_ID)
        captured = [cell.cell_contents for cell in (bound.__closure__ or ())]
        # Security invariant: the closure may hold only opaque string ids — never a
        # WorkflowRunContext (non-str) or the TOTP seed. Avoids asserting an exact cell
        # count, which is a CPython layout detail brittle to benign refactors.
        assert all(isinstance(value, str) for value in captured)
        assert _RFC_TOTP_SEED not in captured
        assert {_CREDENTIAL_KEY, _ORG_ID, _WORKFLOW_RUN_ID} <= set(captured)

    @pytest.mark.asyncio
    async def test_builtin_rejects_non_credential(self) -> None:
        from skyvern.forge.sdk.workflow.models.block import CodeBlock as _CB
        from skyvern.forge.sdk.workflow.models.block import CodeBlockOTPError

        otp_builtin = _CB.build_safe_vars()["otp"]
        with pytest.raises(CodeBlockOTPError):
            await otp_builtin(object())

    @pytest.mark.parametrize("snippet", ["cred.otp.__closure__", "cred.otp.__code__"])
    def test_user_code_cannot_walk_bound_method_internals(self, snippet: str) -> None:
        with pytest.raises(InsecureCodeDetected, match="private"):
            CodeBlock.is_safe_code(snippet)

    def test_user_code_cannot_getattr_or_vars(self) -> None:
        with pytest.raises(InsecureCodeDetected, match="getattr"):
            CodeBlock.is_safe_code("cred.getattr('otp')")
        with pytest.raises(InsecureCodeDetected, match="vars"):
            CodeBlock.is_safe_code("cred.vars()")


class TestCodeBlockOtpBuiltinDelegates:
    """The top-level otp(credential) builtin delegates to the bound .otp()."""

    @pytest.mark.asyncio
    async def test_builtin_calls_bound_method(self) -> None:
        from types import SimpleNamespace

        from skyvern.forge.sdk.workflow.models.block import CodeBlock as _CB

        called: dict[str, bool] = {}

        async def fake_bound() -> str:
            called["bound"] = True
            return "999111"

        cred = SimpleNamespace(otp=fake_bound)
        otp_builtin = _CB.build_safe_vars()["otp"]
        result = await otp_builtin(cred)
        assert result == "999111"
        assert called["bound"] is True


class TestCodeBlockOtpForIdentifier:
    """A bare address resolves an OTP with no credential in play, and keeps its code/link form."""

    @staticmethod
    def _patch_poll(monkeypatch: pytest.MonkeyPatch, wrc, polled):
        from skyvern.forge.sdk.workflow.models import block as block_module

        _patch_context_resolution(monkeypatch, wrc)

        async def fake_get_workflow_run(*args: object, **kwargs: object) -> _FakeWorkflowRun:
            return _FakeWorkflowRun()

        captured: dict[str, object] = {}

        async def fake_poll(**kwargs: object):
            captured.update(kwargs)
            return polled

        monkeypatch.setattr(
            block_module.app.DATABASE.workflow_runs, "get_workflow_run", fake_get_workflow_run, raising=False
        )
        monkeypatch.setattr(block_module.otp_service, "poll_otp_value", fake_poll)
        return captured

    @pytest.mark.asyncio
    async def test_resolves_without_any_credential(self, monkeypatch: pytest.MonkeyPatch) -> None:
        from unittest.mock import MagicMock

        from skyvern.forge.sdk.workflow.context_manager import WorkflowRunContext
        from skyvern.forge.sdk.workflow.models.block import _resolve_code_block_otp_for_identifier
        from skyvern.services.otp_service import OTPValue

        wrc = WorkflowRunContext(
            workflow_title="t",
            workflow_id="w",
            workflow_permanent_id="wpid",
            workflow_run_id=_WORKFLOW_RUN_ID,
            aws_client=MagicMock(),
        )
        assert wrc.values == {}
        assert wrc.credential_totp_identifiers == {}

        captured = self._patch_poll(monkeypatch, wrc, OTPValue(value="778899"))

        result = await _resolve_code_block_otp_for_identifier(
            "[email protected]", _ORG_ID, _WORKFLOW_RUN_ID, budget_seconds=120
        )

        assert result == "778899"
        assert captured["totp_identifier"] == "[email protected]"
        assert captured["organization_id"] == _ORG_ID
        # Same run-start anchoring as the credential-bound path.
        assert captured["created_after"] == _FakeWorkflowRun().started_at
        # Same secret registration as the credential-bound path.
        assert "778899" in wrc.secrets.values()

    @pytest.mark.asyncio
    async def test_magic_link_is_flagged_as_link(self, monkeypatch: pytest.MonkeyPatch) -> None:
        from skyvern.forge.sdk.workflow.models.block import _resolve_code_block_otp_for_identifier
        from skyvern.services.otp_service import OTPValue

        wrc = _build_wrc_with_identifier()
        self._patch_poll(monkeypatch, wrc, OTPValue(value="https://example.com/magic?t=abc"))

        result = await _resolve_code_block_otp_for_identifier(
            "[email protected]", _ORG_ID, _WORKFLOW_RUN_ID, budget_seconds=120
        )

        assert result.is_link is True
        assert result == "https://example.com/magic?t=abc"

    @pytest.mark.asyncio
    async def test_code_is_not_flagged_as_link(self, monkeypatch: pytest.MonkeyPatch) -> None:
        from skyvern.forge.sdk.workflow.models.block import _resolve_code_block_otp_for_identifier
        from skyvern.services.otp_service import OTPValue

        wrc = _build_wrc_with_identifier()
        self._patch_poll(monkeypatch, wrc, OTPValue(value="445566"))

        result = await _resolve_code_block_otp_for_identifier(
            "[email protected]", _ORG_ID, _WORKFLOW_RUN_ID, budget_seconds=120
        )

        assert result.is_link is False
        # Usable directly as the value to type, without unwrapping.
        assert result == "445566"
        assert result.strip() == "445566"

    @pytest.mark.asyncio
    async def test_sms_identifier_timeout_names_no_mailbox(self, monkeypatch: pytest.MonkeyPatch) -> None:
        wrc = _build_wrc_with_identifier(identifier="+15555550147")
        self._patch_poll(monkeypatch, wrc, None)

        async def timing_out_poll(**kwargs: object) -> NoReturn:
            kwargs["raw_context"].store_queried = True
            raise asyncio.TimeoutError

        monkeypatch.setattr(block_module.otp_service, "poll_otp_value", timing_out_poll)

        with pytest.raises(CodeBlockOTPError) as exc_info:
            await _resolve_code_block_otp_for_identifier("+15555550147", _ORG_ID, _WORKFLOW_RUN_ID, budget_seconds=120)

        message = str(exc_info.value)
        assert "no code has been stored" in message
        for mailbox_word in ("Gmail", "Outlook", "inbox"):
            assert mailbox_word not in message

    @pytest.mark.asyncio
    async def test_builtin_routes_a_string_to_the_identifier_path(self, monkeypatch: pytest.MonkeyPatch) -> None:
        from skyvern.forge.sdk.workflow.models.block import CodeBlock as _CB
        from skyvern.services.otp_service import OTPValue

        wrc = _build_wrc_with_identifier()
        captured = self._patch_poll(monkeypatch, wrc, OTPValue(value="112233"))

        otp_builtin = _CB.build_safe_vars()["otp"]
        result = await otp_builtin("[email protected]", organization_id=_ORG_ID, workflow_run_id=_WORKFLOW_RUN_ID)

        assert result == "112233"
        assert captured["totp_identifier"] == "[email protected]"

    @pytest.mark.asyncio
    async def test_credential_path_still_returns_a_plain_string(self, monkeypatch: pytest.MonkeyPatch) -> None:
        """otp(credential) is unchanged: a bare str, with no code/link attribute grafted on."""
        from skyvern.forge.sdk.workflow.models.block import OTPResult, _resolve_code_block_otp
        from skyvern.services.otp_service import OTPValue

        wrc = _build_wrc_with_identifier()
        self._patch_poll(monkeypatch, wrc, OTPValue(value="https://example.com/magic?t=xyz"))

        code = await _resolve_code_block_otp(_CREDENTIAL_KEY, _ORG_ID, _WORKFLOW_RUN_ID, budget_seconds=120)

        assert type(code) is str
        assert not isinstance(code, OTPResult)
        assert code == "https://example.com/magic?t=xyz"


class TestCodeBlockOtpNoLeak:
    """AC3: the resolved OTP is masked in the persisted output and never leaks the seed."""

    @pytest.mark.asyncio
    async def test_execute_masks_otp_in_output(self, monkeypatch: pytest.MonkeyPatch) -> None:
        import pyotp

        _freeze_totp_clock(monkeypatch)
        wrc = _build_wrc_with_totp_seed()
        expected_code = pyotp.TOTP(_RFC_TOTP_SEED).now()

        result, output_value = await _run_credential_code_block(
            monkeypatch,
            wrc,
            code=f"code = await {_CREDENTIAL_KEY}.otp()\nresult = code",
            label="otp_code",
        )

        assert result.success is True
        # The minted code is masked everywhere it was assigned to a user local.
        assert expected_code not in json.dumps(output_value)
        assert _RFC_TOTP_SEED not in json.dumps(output_value)
        assert expected_code in set(wrc.secrets.values())
        assert "*****" in json.dumps(output_value)

    @pytest.mark.asyncio
    async def test_otp_not_leaked_in_failure_reason(self, monkeypatch: pytest.MonkeyPatch) -> None:
        """User code that raises with the OTP in the message must not leak it into failure_reason."""
        import pyotp

        _freeze_totp_clock(monkeypatch)
        wrc = _build_wrc_with_totp_seed()
        expected_code = pyotp.TOTP(_RFC_TOTP_SEED).now()

        result, _ = await _run_credential_code_block(
            monkeypatch,
            wrc,
            code=f"code = await {_CREDENTIAL_KEY}.otp()\nraise Exception(code)",
            label="otp_raise",
        )

        assert result.success is False
        assert result.failure_reason is not None
        assert expected_code not in result.failure_reason
        assert _RFC_TOTP_SEED not in result.failure_reason
        # Masking the secret must not cost the run the cause of its own failure (SKY-14294).
        assert result.failure_reason == "Failed to execute code block. Reason: Exception: *****"

    @pytest.mark.asyncio
    async def test_otp_not_leaked_in_assembled_repair_payload(self, monkeypatch: pytest.MonkeyPatch) -> None:
        from unittest.mock import AsyncMock, MagicMock

        import pyotp

        from skyvern.forge.sdk.copilot.output_utils import build_run_blocks_response, sanitize_tool_result_for_llm
        from skyvern.forge.sdk.copilot.tools import _attach_action_traces
        from skyvern.forge.sdk.copilot.tools import run_execution as run_execution_module
        from skyvern.forge.sdk.copilot.tools.run_execution import _summarize_action_trace
        from skyvern.forge.sdk.workflow.models.code_block_recording import CodeBlockActionRecording
        from skyvern.webeye.actions.actions import Action

        _freeze_totp_clock(monkeypatch)
        wrc = _build_wrc_with_totp_seed()
        expected_code = pyotp.TOTP(_RFC_TOTP_SEED).now()
        recorded_actions: list[Action] = []

        async def capture_actions(self: CodeBlockActionRecording, actions: list[Action]) -> None:
            del self
            recorded_actions.extend(actions)

        monkeypatch.setattr(CodeBlockActionRecording, "persist", capture_actions)
        result, _ = await _run_credential_code_block(
            monkeypatch,
            wrc,
            code=f"code = await {_CREDENTIAL_KEY}.otp()\nraise Exception(code)",
            label="otp_raise_payload",
        )

        for action in recorded_actions:
            action.task_id = "task-otp"
        mock_db = MagicMock()
        mock_db.tasks = MagicMock()
        mock_db.tasks.get_recent_actions_for_tasks = AsyncMock(return_value=recorded_actions)

        class _AppStub:
            DATABASE = mock_db

        monkeypatch.setattr(run_execution_module, "app", _AppStub())
        block = MagicMock(task_id="task-otp")
        block_result = {
            "label": "otp_raise_payload",
            "block_type": "code",
            "status": "failed",
            "failure_reason": result.failure_reason,
        }
        await _attach_action_traces([block], [block_result], _ORG_ID)
        action_trace_summary = _summarize_action_trace(block_result["action_trace"])
        del block_result["action_trace"]
        payload = build_run_blocks_response(
            False,
            {
                "workflow_run_id": _WORKFLOW_RUN_ID,
                "overall_status": "failed",
                "failure_reason": result.failure_reason,
                "blocks": [block_result],
                "action_trace_summary": action_trace_summary,
            },
        )
        sanitized = sanitize_tool_result_for_llm("run_blocks_and_collect_debug", payload)
        serialized = json.dumps(sanitized)

        assert expected_code not in serialized
        assert _RFC_TOTP_SEED not in serialized
        assert "code_line" in serialized

    @pytest.mark.asyncio
    async def test_legacy_totp_not_leaked_in_failure_reason(self, monkeypatch: pytest.MonkeyPatch) -> None:
        import pyotp

        _freeze_totp_clock(monkeypatch)
        wrc = _build_wrc_with_totp_seed()
        expected_code = pyotp.TOTP(_RFC_TOTP_SEED).now()

        result, _ = await _run_credential_code_block(
            monkeypatch,
            wrc,
            code=f"raise Exception({_CREDENTIAL_KEY}.totp)",
            label="legacy_raise",
        )

        assert result.success is False
        assert result.failure_reason is not None
        assert expected_code not in result.failure_reason
        assert _RFC_TOTP_SEED not in result.failure_reason


class TestCodeBlockLegacyTotpRegression:
    """The legacy pre-minted .totp attribute keeps working and is registered for masking."""

    @pytest.mark.asyncio
    async def test_legacy_totp_attribute_and_registration(self, monkeypatch: pytest.MonkeyPatch) -> None:
        import pyotp

        _freeze_totp_clock(monkeypatch)
        wrc = _build_wrc_with_totp_seed()
        expected_code = pyotp.TOTP(_RFC_TOTP_SEED).now()

        # Legacy synthesized code reads the pre-minted .totp attribute.
        result, output_value = await _run_credential_code_block(
            monkeypatch,
            wrc,
            code=f"aliased = {_CREDENTIAL_KEY}.totp\nresult = aliased",
            label="legacy",
        )

        assert result.success is True
        # The pre-minted code matches the seed's current code AND is registered + masked.
        assert expected_code in set(wrc.secrets.values())
        assert expected_code not in json.dumps(output_value)

    @pytest.mark.asyncio
    async def test_bound_credential_namespace_excluded_from_captured_locals(
        self, monkeypatch: pytest.MonkeyPatch
    ) -> None:
        """The bound Credential namespace (carrying .otp) is dropped from captured locals by the
        existing parameter-key exclusion, so it never appears in the output."""
        wrc = _build_wrc_with_totp_seed()

        result, output_value = await _run_credential_code_block(
            monkeypatch,
            wrc,
            code="visible = 'ok'",
            label="excl",
        )

        assert result.success is True
        assert output_value == {"visible": "ok"}
        assert _CREDENTIAL_KEY not in output_value


# ---------------------------------------------------------------------------
# class statements — runtime sandbox support
# ---------------------------------------------------------------------------


class TestClassStatementsAtRuntime:
    """Plain ``class`` statements execute in the sandbox; exotic class heads do not."""

    @staticmethod
    def _run(code: str, parameters: dict | None = None):
        fn = TestGenerateAsyncUserFunctionIntegration._exec_user_code(code, parameters=parameters)
        return asyncio.run(fn())

    def test_plain_class_definition_executes(self) -> None:
        result = self._run("class Point:\n    pass\np = Point()\nkind = str(p is not None)")
        assert result["kind"] == "True"

    def test_class_with_init_and_method(self) -> None:
        code = (
            "class Counter:\n"
            "    def __init__(self, start):\n"
            "        self.value = start\n"
            "    def bump(self):\n"
            "        self.value = self.value + 1\n"
            "        return self.value\n"
            "c = Counter(41)\n"
            "answer = c.bump()\n"
        )
        assert self._run(code)["answer"] == 42

    def test_subclass_of_allowlisted_type(self) -> None:
        code = (
            "class Registry(dict):\n"
            "    def put(self, key, value):\n"
            "        self[key] = value\n"
            "        return len(self)\n"
            "r = Registry()\n"
            "count = r.put('a', 1)\n"
        )
        assert self._run(code)["count"] == 1

    def test_custom_exception_subclass_raises(self) -> None:
        code = (
            "class RetryNeeded(Exception):\n"
            "    pass\n"
            "caught = False\n"
            "try:\n"
            "    raise RetryNeeded('again')\n"
            "except Exception:\n"
            "    caught = True\n"
        )
        assert self._run(code)["caught"] is True

    def test_explicit_metaclass_is_rejected_at_runtime(self) -> None:
        with pytest.raises(RuntimeError, match="class definitions"):
            self._run("class Weird(metaclass=dict):\n    pass")

    def test_explicit_metaclass_none_is_rejected_at_runtime(self) -> None:
        with pytest.raises(RuntimeError, match="class definitions"):
            self._run("class Weird(metaclass=None):\n    pass")

    def test_class_head_keyword_is_rejected_at_runtime(self) -> None:
        with pytest.raises(RuntimeError, match="class definitions"):
            self._run("class Weird(dict, extra=1):\n    pass")


class TestIsSafeCodeClassStatements:
    """Preflight admits exactly what the runtime can execute: plain classes yes, class-head keywords no."""

    def test_plain_class_is_admitted(self) -> None:
        CodeBlock.is_safe_code("class Point:\n    pass")

    def test_subclass_is_admitted(self) -> None:
        CodeBlock.is_safe_code("class Registry(dict):\n    pass")

    def test_metaclass_keyword_is_rejected(self) -> None:
        with pytest.raises(InsecureCodeDetected, match="class definitions"):
            CodeBlock.is_safe_code("class Weird(metaclass=dict):\n    pass")

    def test_other_class_head_keyword_is_rejected(self) -> None:
        with pytest.raises(InsecureCodeDetected, match="class definitions"):
            CodeBlock.is_safe_code("class Weird(dict, extra=1):\n    pass")

    def test_double_star_class_head_is_rejected(self) -> None:
        with pytest.raises(InsecureCodeDetected, match="class definitions"):
            CodeBlock.is_safe_code("kw = {}\nclass Weird(**kw):\n    pass")

    def test_bare_build_class_name_stays_blocked(self) -> None:
        with pytest.raises(InsecureCodeDetected, match="private"):
            CodeBlock.is_safe_code("__build_class__(len, 'X')")

    def test_script_path_keeps_metaclass_support(self) -> None:
        is_safe_script_code("class Weird(metaclass=type):\n    pass")

    @pytest.mark.parametrize("code", ["import sys", "import asyncio\nvalue = asyncio.sys"])
    def test_script_cannot_access_sys_module(self, code: str) -> None:
        with pytest.raises(InsecureCodeDetected, match="sys"):
            is_safe_script_code(code)


class TestCodeBlockMagicLink:
    """SKY-14056: a credential can request an emailed sign-in link and open it."""

    @staticmethod
    def _patch_poll(monkeypatch: pytest.MonkeyPatch, wrc, polled, *, raises: BaseException | None = None):
        from skyvern.forge.sdk.workflow.models import block as block_module

        _patch_context_resolution(monkeypatch, wrc)
        monkeypatch.setattr(settings, "BROWSER_ACTION_TIMEOUT_MS", 0)

        async def fake_get_workflow_run(*args: object, **kwargs: object) -> _FakeWorkflowRun:
            return _FakeWorkflowRun()

        captured: dict[str, object] = {}

        async def fake_poll(**kwargs: object):
            captured.update(kwargs)
            if raises is not None:
                raise raises
            return polled

        monkeypatch.setattr(
            block_module.app.DATABASE.workflow_runs, "get_workflow_run", fake_get_workflow_run, raising=False
        )
        monkeypatch.setattr(block_module.otp_service, "poll_otp_value", fake_poll)
        return captured

    @pytest.mark.asyncio
    @pytest.mark.usefixtures("public_dns")
    async def test_polls_for_a_link_and_navigates_once_without_filling(self, monkeypatch: pytest.MonkeyPatch) -> None:
        from skyvern.forge.sdk.schemas.totp_codes import OTPType
        from skyvern.forge.sdk.workflow.models.block import _bind_code_block_magic_link
        from skyvern.services.otp_service import OTPValue

        link = "https://portal.example.com/signin?token=" + "z" * 900
        wrc = _build_wrc_with_identifier()
        captured = self._patch_poll(monkeypatch, wrc, OTPValue(value=link, type=OTPType.MAGIC_LINK))

        navigations: list[str] = []
        fills: list[object] = []

        page = _fake_code_block_page(navigations=navigations, fills=fills)

        magic_link = _bind_code_block_magic_link(_CREDENTIAL_KEY, _ORG_ID, _WORKFLOW_RUN_ID)
        await magic_link(page)

        assert captured["expected_otp_type"] is OTPType.MAGIC_LINK
        assert navigations == [link]
        assert fills == []

    @pytest.mark.asyncio
    @pytest.mark.parametrize(
        "native_close", [True, False], ids=["native_close_during_goto", "ordinary_navigation_error"]
    )
    async def test_link_navigation_stops_retrying_after_native_browser_loss(
        self, monkeypatch: pytest.MonkeyPatch, native_close: bool
    ) -> None:
        from playwright.async_api import Error as PlaywrightError

        from skyvern.constants import NAVIGATION_MAX_RETRY_TIME
        from skyvern.exceptions import FailedToNavigateToUrl
        from skyvern.forge.sdk.schemas.totp_codes import OTPType
        from skyvern.forge.sdk.workflow.models import block as block_module
        from skyvern.forge.sdk.workflow.models.block import _bind_code_block_magic_link
        from skyvern.services.otp_service import OTPValue
        from skyvern.webeye import navigation as navigation_module
        from tests.unit.scoped_asyncio import ScopedAsyncio

        link = "https://93.184.216.34/signin?token=x"
        self._patch_poll(monkeypatch, _build_wrc_with_identifier(), OTPValue(value=link, type=OTPType.MAGIC_LINK))
        browser_state, context = run_bound_browser_state(_WORKFLOW_RUN_ID)
        monkeypatch.setattr(block_module.app.BROWSER_MANAGER, "get_for_workflow_run", lambda *_, **__: browser_state)
        monkeypatch.setattr(navigation_module, "asyncio", ScopedAsyncio(sleep=AsyncMock()))
        navigations: list[str] = []
        page = _fake_code_block_page()

        async def goto(url: str, **kwargs: object) -> None:
            navigations.append(url)
            if native_close:
                browser_state._on_browser_context_closed(context)
                raise PlaywrightError("Target page, context or browser has been closed")
            raise PlaywrightError("Timeout 30000ms exceeded.")

        page.goto = goto

        with pytest.raises(FailedToNavigateToUrl):
            await _bind_code_block_magic_link(_CREDENTIAL_KEY, _ORG_ID, _WORKFLOW_RUN_ID)(page)

        assert navigations == [link] * (1 if native_close else NAVIGATION_MAX_RETRY_TIME)

    @pytest.mark.asyncio
    @pytest.mark.usefixtures("public_dns")
    async def test_anchors_created_after_at_call_time_not_run_start(self, monkeypatch: pytest.MonkeyPatch) -> None:
        from skyvern.forge.sdk.schemas.totp_codes import OTPType
        from skyvern.forge.sdk.workflow.models.block import _bind_code_block_magic_link
        from skyvern.services.otp_service import OTPValue

        wrc = _build_wrc_with_identifier()
        captured = self._patch_poll(
            monkeypatch, wrc, OTPValue(value="https://example.com/go?t=1", type=OTPType.MAGIC_LINK)
        )

        page = _fake_code_block_page()

        await _bind_code_block_magic_link(_CREDENTIAL_KEY, _ORG_ID, _WORKFLOW_RUN_ID)(page)

        from skyvern.services.otp_service import MAGIC_LINK_ANCHOR_GRACE

        # A link minted earlier in the run may already be spent, so the run-start anchor
        # the code path uses would re-serve it.
        anchor = captured["created_after"]
        assert anchor > _FakeWorkflowRun().started_at
        # Naive UTC, which is what the DB column and the inbox cutoff are both compared against.
        # A local-clock anchor drifts by the host's offset: east of UTC it filters out every
        # delivery, west of UTC it re-opens the window this anchor exists to close.
        assert anchor.tzinfo is None
        expected_anchor = datetime.now(timezone.utc).replace(tzinfo=None) - MAGIC_LINK_ANCHOR_GRACE
        assert abs((anchor - expected_anchor).total_seconds()) < 30

    @pytest.mark.asyncio
    async def test_a_seed_only_credential_fails_closed_naming_the_missing_identifier(
        self, monkeypatch: pytest.MonkeyPatch
    ) -> None:
        """An authenticator seed can only mint a 6-digit code; navigating to one is nonsense."""
        from skyvern.forge.sdk.workflow.models.block import CodeBlockOTPError, _bind_code_block_magic_link

        wrc = _build_wrc_with_totp_seed()
        _patch_context_resolution(monkeypatch, wrc)

        navigations: list[str] = []

        page = _fake_code_block_page(navigations=navigations)

        magic_link = _bind_code_block_magic_link(_CREDENTIAL_KEY, _ORG_ID, _WORKFLOW_RUN_ID)
        with pytest.raises(CodeBlockOTPError) as excinfo:
            await magic_link(page)

        message = str(excinfo.value)
        assert "email" in message.lower()
        assert "authenticator" in message.lower()
        assert navigations == []

    @pytest.mark.asyncio
    async def test_wrong_verb_reports_the_type_that_actually_arrived(self, monkeypatch: pytest.MonkeyPatch) -> None:
        """The repair signal: a link-verb call against a code mailbox must not read as an empty inbox."""
        import asyncio

        from skyvern.forge.sdk.schemas.totp_codes import OTPType
        from skyvern.forge.sdk.workflow.models import block as block_module
        from skyvern.forge.sdk.workflow.models.block import CodeBlockOTPError, _bind_code_block_magic_link

        wrc = _build_wrc_with_identifier()
        _patch_context_resolution(monkeypatch, wrc)

        async def fake_get_workflow_run(*args: object, **kwargs: object) -> _FakeWorkflowRun:
            return _FakeWorkflowRun()

        async def fake_poll(**kwargs: object):
            # Stand in for the real poll: a code was seen and rejected for the requested type.
            kwargs["raw_context"].observed_otp_types.add(OTPType.TOTP)
            raise asyncio.TimeoutError

        monkeypatch.setattr(
            block_module.app.DATABASE.workflow_runs, "get_workflow_run", fake_get_workflow_run, raising=False
        )
        monkeypatch.setattr(block_module.otp_service, "poll_otp_value", fake_poll)

        page = _fake_code_block_page()

        with pytest.raises(CodeBlockOTPError) as excinfo:
            await _bind_code_block_magic_link(_CREDENTIAL_KEY, _ORG_ID, _WORKFLOW_RUN_ID)(page)

        message = str(excinfo.value)
        assert "magic_link" in message
        assert "totp" in message
        assert "otp()" in message

    @pytest.mark.asyncio
    async def test_an_unqueried_store_names_the_link_it_was_waiting_for(self, monkeypatch: pytest.MonkeyPatch) -> None:
        import asyncio

        from skyvern.forge.sdk.workflow.models import block as block_module
        from skyvern.forge.sdk.workflow.models.block import CodeBlockOTPError, _bind_code_block_magic_link

        wrc = _build_wrc_with_identifier()
        _patch_context_resolution(monkeypatch, wrc)

        async def fake_get_workflow_run(*args: object, **kwargs: object) -> _FakeWorkflowRun:
            return _FakeWorkflowRun()

        async def fake_poll(**kwargs: object):
            gmail = kwargs["email_context"].for_source(GmailOTPSource.name)
            gmail.credential_ids = ["cred_1"]
            gmail.completed_credential_ids.add("cred_1")
            raise asyncio.TimeoutError

        monkeypatch.setattr(
            block_module.app.DATABASE.workflow_runs, "get_workflow_run", fake_get_workflow_run, raising=False
        )
        monkeypatch.setattr(block_module.otp_service, "poll_otp_value", fake_poll)

        with pytest.raises(CodeBlockOTPError) as excinfo:
            await _bind_code_block_magic_link(_CREDENTIAL_KEY, _ORG_ID, _WORKFLOW_RUN_ID)(_fake_code_block_page())

        message = str(excinfo.value)
        assert "stored sign-in links not checked" in message
        assert "stored codes not checked" not in message

    @pytest.mark.asyncio
    async def test_code_verb_against_a_link_mailbox_reports_the_mismatch_too(
        self, monkeypatch: pytest.MonkeyPatch
    ) -> None:
        import asyncio

        from skyvern.forge.sdk.schemas.totp_codes import OTPType
        from skyvern.forge.sdk.workflow.models import block as block_module
        from skyvern.forge.sdk.workflow.models.block import CodeBlockOTPError, _resolve_code_block_otp

        wrc = _build_wrc_with_identifier()
        _patch_context_resolution(monkeypatch, wrc)

        async def fake_get_workflow_run(*args: object, **kwargs: object) -> _FakeWorkflowRun:
            return _FakeWorkflowRun()

        async def fake_poll(**kwargs: object):
            kwargs["email_context"].observed_otp_types.add(OTPType.MAGIC_LINK)
            raise asyncio.TimeoutError

        monkeypatch.setattr(
            block_module.app.DATABASE.workflow_runs, "get_workflow_run", fake_get_workflow_run, raising=False
        )
        monkeypatch.setattr(block_module.otp_service, "poll_otp_value", fake_poll)

        with pytest.raises(CodeBlockOTPError) as excinfo:
            await _resolve_code_block_otp(_CREDENTIAL_KEY, _ORG_ID, _WORKFLOW_RUN_ID, budget_seconds=120)

        message = str(excinfo.value)
        assert "magic_link" in message
        assert "totp" in message

    @pytest.mark.asyncio
    async def test_a_silent_mailbox_still_reports_a_plain_timeout(self, monkeypatch: pytest.MonkeyPatch) -> None:
        """No mismatch was observed, so the message must not invent one."""
        import asyncio

        from skyvern.forge.sdk.workflow.models import block as block_module
        from skyvern.forge.sdk.workflow.models.block import CodeBlockOTPError, _bind_code_block_magic_link

        wrc = _build_wrc_with_identifier()
        _patch_context_resolution(monkeypatch, wrc)

        async def fake_get_workflow_run(*args: object, **kwargs: object) -> _FakeWorkflowRun:
            return _FakeWorkflowRun()

        async def fake_poll(**kwargs: object):
            kwargs["raw_context"].store_queried = True
            raise asyncio.TimeoutError

        monkeypatch.setattr(
            block_module.app.DATABASE.workflow_runs, "get_workflow_run", fake_get_workflow_run, raising=False
        )
        monkeypatch.setattr(block_module.otp_service, "poll_otp_value", fake_poll)

        page = _fake_code_block_page()

        with pytest.raises(CodeBlockOTPError, match="was not received within") as exc_info:
            await _bind_code_block_magic_link(_CREDENTIAL_KEY, _ORG_ID, _WORKFLOW_RUN_ID)(page)

        message = str(exc_info.value)
        assert "no sign-in link has been stored" in message
        assert "code has been stored" not in message

    @pytest.mark.asyncio
    async def test_a_page_of_the_blocks_own_making_is_refused_before_polling(
        self, monkeypatch: pytest.MonkeyPatch
    ) -> None:
        """Authored code passes the page in, so an impostor would otherwise receive the link."""
        from skyvern.forge.sdk.workflow.models import block as block_module
        from skyvern.forge.sdk.workflow.models.block import CodeBlockOTPError, _bind_code_block_magic_link

        wrc = _build_wrc_with_identifier()
        _patch_context_resolution(monkeypatch, wrc)

        polls: list[object] = []

        async def fake_poll(**kwargs: object):
            polls.append(kwargs)
            return None

        monkeypatch.setattr(block_module.otp_service, "poll_otp_value", fake_poll)

        captured: list[str] = []

        class _Impostor:
            async def goto(self, url: str, **kwargs: object) -> None:
                captured.append(url)

        with pytest.raises(CodeBlockOTPError, match="requires the code block's page"):
            await _bind_code_block_magic_link(_CREDENTIAL_KEY, _ORG_ID, _WORKFLOW_RUN_ID)(_Impostor())

        # Refused before the link is even resolved, so nothing was there to capture.
        assert captured == []
        assert polls == []

    @pytest.mark.asyncio
    async def test_a_forgery_carrying_page_capabilities_is_still_refused(self, monkeypatch: pytest.MonkeyPatch) -> None:
        """Authored code can define a class, so a capability check is satisfiable; identity is not."""
        from skyvern.forge.sdk.workflow.models import block as block_module
        from skyvern.forge.sdk.workflow.models.block import CodeBlockOTPError, _bind_code_block_magic_link

        wrc = _build_wrc_with_identifier()
        _patch_context_resolution(monkeypatch, wrc)

        polls: list[object] = []

        async def fake_poll(**kwargs: object):
            polls.append(kwargs)
            return None

        monkeypatch.setattr(block_module.otp_service, "poll_otp_value", fake_poll)

        captured: list[str] = []

        class _Forgery:
            """Carries every capability is_page_like requires, plus a goto that keeps the link."""

            main_frame = None
            context = None

            def bring_to_front(self) -> None:
                return None

            def evaluate(self, *args: object, **kwargs: object) -> None:
                return None

            async def goto(self, url: str, **kwargs: object) -> None:
                captured.append(url)

        real_page = _fake_code_block_page()
        bound = _bind_code_block_magic_link(_CREDENTIAL_KEY, _ORG_ID, _WORKFLOW_RUN_ID, expected_page=real_page)

        with pytest.raises(CodeBlockOTPError, match="requires the code block's page"):
            await bound(_Forgery())

        assert captured == []
        assert polls == []


class TestCodeBlockTemplateSecretScoping:
    """Template data carries secrets only for credential parameters the block declares (SKY-14047),
    mirroring the execution namespace, so a block cannot render another block's credential."""

    DECLARED_KEY = "declared_cred"
    UNDECLARED_KEY = "other_cred"

    @staticmethod
    def _workflow_parameter(key: str) -> WorkflowParameter:
        now = datetime.now(timezone.utc)
        return WorkflowParameter(
            workflow_parameter_id=f"wp_{key}",
            workflow_parameter_type=WorkflowParameterType.CREDENTIAL_ID,
            workflow_id="w_test",
            key=key,
            created_at=now,
            modified_at=now,
        )

    def _block(self) -> CodeBlock:
        now = datetime.now(timezone.utc)
        output_parameter = OutputParameter(
            parameter_type=ParameterType.OUTPUT,
            key="scoping_output",
            description="",
            output_parameter_id="op_scoping",
            workflow_id="w_test",
            created_at=now,
            modified_at=now,
        )
        return CodeBlock(
            label="scoped_code",
            code="value = 'ok'",
            output_parameter=output_parameter,
            parameters=[self._workflow_parameter(self.DECLARED_KEY)],
        )

    def _context(self) -> FakeWorkflowRunContext:
        return FakeWorkflowRunContext(
            values={
                self.DECLARED_KEY: {"context": "login", "username": "ph_u1", "password": "ph_p1"},
                self.UNDECLARED_KEY: {"context": "other", "username": "ph_u2", "password": "ph_p2"},
            },
            secrets={
                "ph_u1": "alice",
                "ph_p1": "declared-secret",
                "ph_u2": "bob",
                "ph_p2": "undeclared-secret",
            },
            include_secrets_in_templates=True,
        )

    def test_declared_credential_renders_real_values(self) -> None:
        rendered = self._block().format_block_parameter_template_from_workflow_run_context(
            "{{ declared_cred_real_password }}|{{ declared_cred.username }}", self._context()
        )
        assert rendered == "declared-secret|alice"

    def test_undeclared_credential_never_renders_its_secret(self) -> None:
        try:
            rendered = self._block().format_block_parameter_template_from_workflow_run_context(
                "{{ other_cred_real_password }}", self._context()
            )
        except MissingJinjaVariables:
            return
        assert "undeclared-secret" not in rendered

    def test_undeclared_credential_dict_stays_placeholder(self) -> None:
        rendered = self._block().format_block_parameter_template_from_workflow_run_context(
            "{{ other_cred.password }}", self._context()
        )
        assert rendered == "ph_p2"

    def test_branch_evaluation_template_data_merges_no_undeclared_secrets(self) -> None:
        template_data = BranchEvaluationContext(
            workflow_run_context=self._context(), block_label="cond"
        ).build_template_data()

        assert "ph_p1" not in template_data
        assert "ph_p2" not in template_data
        assert not any(key.endswith("_real_password") for key in template_data)
        assert template_data[self.UNDECLARED_KEY]["password"] == "ph_p2"


class TestFailedReadinessWaitPropagates:
    @staticmethod
    def _page_whose_readiness_wait_times_out() -> Page:
        class TimingOutLocator:
            def __init__(self, page: Page) -> None:
                self.page = page

            async def wait_for(self, **kwargs: object) -> None:
                raise PlaywrightTimeoutError(
                    'Locator.wait_for: Timeout 30000ms exceeded.\nwaiting for locator("body") to be visible'
                )

        page = MagicMock(spec=Page)
        page.locator = lambda _selector: TimingOutLocator(page)
        return page

    async def _execute(self, monkeypatch: pytest.MonkeyPatch, code: str) -> tuple[object, list[object]]:
        page = self._page_whose_readiness_wait_times_out()

        class ReadyBrowserState:
            def __init__(self) -> None:
                self.browser_artifacts = BrowserArtifacts()

            async def get_working_page(self) -> object:
                return page

        persisted: list[object] = []

        async def validate_code_block(*args: object, **kwargs: object) -> None:
            return None

        async def get_browser_state(*args: object, **kwargs: object) -> ReadyBrowserState:
            return ReadyBrowserState()

        async def record_output(self: object, ctx: object, run_id: object, value: object) -> None:
            persisted.append(value)

        monkeypatch.setattr(
            "skyvern.forge.sdk.workflow.models.block.app.AGENT_FUNCTION.validate_code_block",
            validate_code_block,
        )
        monkeypatch.setattr(CodeBlock, "get_or_create_browser_state", get_browser_state)
        monkeypatch.setattr(
            CodeBlock, "get_workflow_run_context", lambda self, run_id: FakeWorkflowRunContext(values={})
        )
        monkeypatch.setattr(CodeBlock, "record_output_parameter_value", record_output)

        now = datetime.now(timezone.utc)
        block = CodeBlock(
            label="read_summary",
            code=code,
            output_parameter=OutputParameter(
                parameter_type=ParameterType.OUTPUT,
                key="read_summary_output",
                description="test output",
                output_parameter_id="op_read_summary",
                workflow_id="w_test",
                created_at=now,
                modified_at=now,
            ),
        )
        result = await block.execute(workflow_run_id="wrid_test", workflow_run_block_id="")
        return result, persisted

    @pytest.mark.asyncio
    async def test_failed_readiness_wait_fails_the_run_without_a_derived_answer(
        self, monkeypatch: pytest.MonkeyPatch
    ) -> None:
        result, persisted = await self._execute(
            monkeypatch,
            'await page.locator("body").wait_for(state="visible", timeout=30000)\n'
            'return {"summary": "24 results found"}\n',
        )

        assert result.success is False
        assert result.status == BlockStatus.failed
        assert "24 results found" not in json.dumps(persisted)
        assert all(value in (None, {}) for value in persisted)


class TestDirectPathDialogPolicyBinding:
    """The direct path binds the same helper the secure runner brokers, and revokes the declared
    policy at block end before any trusted browser work touches the page again."""

    @staticmethod
    def _block(code: str) -> CodeBlock:
        now = datetime.now(timezone.utc)
        return CodeBlock(
            label="dialog_block",
            code=code,
            output_parameter=OutputParameter(
                parameter_type=ParameterType.OUTPUT,
                key="dialog_output",
                description="test output",
                output_parameter_id="op_dialog",
                workflow_id="w_test",
                created_at=now,
                modified_at=now,
            ),
        )

    def test_the_helper_is_reserved_in_safe_vars(self) -> None:
        assert "set_dialog_policy" in CodeBlock.build_safe_vars()

    @pytest.mark.parametrize("key", ["set_dialog_policy", "set_dialog_policy"])
    def test_a_block_declaring_a_colliding_parameter_is_rejected_at_bind_time(self, key: str) -> None:
        # The fullwidth spelling binds under the normalized name as a wrapper argument, so the
        # raw-key comparison the other builtins use would let it shadow the helper inline.
        with pytest.raises(ValueError, match="set_dialog_policy"):
            self._block("value = 1").generate_async_user_function(
                "value = 1",
                MagicMock(spec=Page),
                {key: "shadowed"},
            )

    @pytest.mark.asyncio
    async def test_prompt_text_is_refused_unless_the_action_accepts_it(self) -> None:
        page = MagicMock(spec=Page)
        page.context = MagicMock()
        declare = _bind_code_block_set_dialog_policy(page)

        with pytest.raises(ValueError, match="prompt_text"):
            await declare(page, "dismiss", "Jamie")
        assert dialog_handler._dialog_policies.get(page.context) is None

    @pytest.mark.asyncio
    async def test_prompt_text_carrying_an_armed_credential_secret_is_refused(self) -> None:
        page = MagicMock(spec=Page)
        page.context = MagicMock()
        guard = CredentialReleaseGuard(workflow_run_id="wr-1", block_label="block")
        assert guard.arm("hunter2-long-secret", "https://accounts.example.com/login", "password")
        declare = _bind_code_block_set_dialog_policy(page, guard)

        with pytest.raises(CodeBlockCredentialReleaseError, match="password"):
            await declare(page, "accept", "hunter2-long-secret")
        assert dialog_handler._dialog_policies.get(page.context) is None

        await declare(page, "accept", "Jamie")
        assert dialog_handler._dialog_policies.get(page.context) is not None
        dialog_handler.clear_dialog_policy(page.context)

    @pytest.mark.asyncio
    async def test_prompt_text_embedding_a_short_armed_secret_is_refused(self) -> None:
        """A CVV sits below the guard's substring floor, so a filled field matches it only on exact
        equality; a dialog answer has no site to weigh that against, so it refuses at any length."""
        page = MagicMock(spec=Page)
        page.context = MagicMock()
        guard = CredentialReleaseGuard(workflow_run_id="wr-1", block_label="block")
        assert guard.arm("123", "https://accounts.example.com/login", "card_cvv")
        assert guard.matches("CVV: 123") == []
        declare = _bind_code_block_set_dialog_policy(page, guard)

        with pytest.raises(CodeBlockCredentialReleaseError, match="card_cvv"):
            await declare(page, "accept", "CVV: 123")
        assert dialog_handler._dialog_policies.get(page.context) is None

    @pytest.mark.asyncio
    async def test_prompt_text_carrying_a_log_only_secret_is_logged_not_refused(self) -> None:
        page = MagicMock(spec=Page)
        page.context = MagicMock()
        guard = CredentialReleaseGuard(workflow_run_id="wr-1", block_label="block")
        assert guard.arm("hunter2-long-secret", "https://accounts.example.com/login", "password", enforced=False)
        declare = _bind_code_block_set_dialog_policy(page, guard)

        with capture_logs() as logs:
            await declare(page, "accept", "hunter2-long-secret")

        would_refuse = [log for log in logs if log["event"] == "codeblock_credential_release_would_refuse"]
        assert [log["parameter_key"] for log in would_refuse] == ["password"]
        assert dialog_handler._dialog_policies.get(page.context) is not None
        dialog_handler.clear_dialog_policy(page.context)

    @pytest.mark.parametrize("revoke_fails", [False, True])
    @pytest.mark.asyncio
    async def test_a_declared_policy_is_revoked_before_any_post_block_browser_work(
        self, monkeypatch: pytest.MonkeyPatch, revoke_fails: bool
    ) -> None:
        page = MagicMock(spec=Page)
        page.context = MagicMock()
        page.context.close = AsyncMock()
        events: list[str] = []
        real_clear = dialog_handler.clear_dialog_policy

        def spy_clear(browser_context: object) -> None:
            events.append(f"clear armed={dialog_handler._dialog_policies.get(browser_context) is not None}")
            if revoke_fails:
                raise RuntimeError("browser context unreachable")
            real_clear(browser_context)

        @asynccontextmanager
        async def spy_settle(browser_context: object) -> AsyncIterator[None]:
            events.append("settle downloads")
            yield

        monkeypatch.setattr(block_module.dialog_handler, "clear_dialog_policy", spy_clear)
        monkeypatch.setattr(block_module, "settle_browser_downloads_for_context", spy_settle)

        class ReadyBrowserState:
            def __init__(self) -> None:
                self.browser_artifacts = BrowserArtifacts()

            async def get_working_page(self) -> object:
                return page

        async def validate_code_block(*args: object, **kwargs: object) -> None:
            return None

        async def get_browser_state(*args: object, **kwargs: object) -> ReadyBrowserState:
            return ReadyBrowserState()

        async def record_output(*args: object, **kwargs: object) -> None:
            return None

        monkeypatch.setattr(
            "skyvern.forge.sdk.workflow.models.block.app.AGENT_FUNCTION.validate_code_block",
            validate_code_block,
        )
        monkeypatch.setattr(CodeBlock, "get_or_create_browser_state", get_browser_state)
        monkeypatch.setattr(
            CodeBlock, "get_workflow_run_context", lambda self, run_id: FakeWorkflowRunContext(values={})
        )
        monkeypatch.setattr(CodeBlock, "record_output_parameter_value", record_output)

        block = self._block('seen = await set_dialog_policy(page, "dismiss")')
        result = await block.execute(workflow_run_id="wrid_test", workflow_run_block_id="")

        assert result.success is True
        assert events == ["clear armed=True", "settle downloads"]
        if revoke_fails:
            # Closing only the page would leave the sandbox's answer armed on every sibling page.
            page.context.close.assert_awaited_once()
            return
        page.context.close.assert_not_awaited()
        assert dialog_handler._dialog_policies.get(page.context) is None

        after_block = MagicMock()
        after_block.type = "confirm"
        after_block.message = "Leave without saving?"
        after_block.default_value = ""
        after_block.accept = AsyncMock()
        after_block.dismiss = AsyncMock()
        await dialog_handler._handle_dialog(after_block, page=page)

        after_block.dismiss.assert_not_awaited()
        after_block.accept.assert_awaited_once()


class TestSearchWebHelperBinding:
    @staticmethod
    def _block() -> CodeBlock:
        now = datetime.now(timezone.utc)
        return CodeBlock(
            label="search_block",
            code="",
            output_parameter=OutputParameter(
                parameter_type=ParameterType.OUTPUT,
                key="search_output",
                description="test output",
                output_parameter_id="op_search",
                workflow_id="w_test",
                created_at=now,
                modified_at=now,
            ),
        )

    def test_search_web_is_reserved_in_safe_vars(self) -> None:
        assert "search_web" in CodeBlock.build_safe_vars()

    @pytest.mark.asyncio
    async def test_back_to_back_searches_open_no_browser_page(self, monkeypatch: pytest.MonkeyPatch) -> None:
        async def new_page_refused() -> None:
            raise AssertionError("search_web opened a browser page")

        arm_search_api(monkeypatch, (200, serpapi_page("https://a.example/", "https://b.example/")))
        context = FakeSearchBrowserContext()
        context.new_page = new_page_refused  # type: ignore[method-assign]
        page = SimpleNamespace(context=context)
        block = self._block()

        user_function = block.generate_async_user_function(
            "found = []\n"
            "for _ in range(4):\n"
            '    found.append(await search_web("construction company", max_results=2))\n'
            "return found\n",
            page,  # type: ignore[arg-type]
        )
        results = await user_function()

        assert [(found["http_status"], found["error_kind"], len(found["results"])) for found in results] == [
            (200, None, 2)
        ] * 4
        assert context.opened == []


class TestOpenPageHelperBinding:
    @staticmethod
    def _block() -> CodeBlock:
        now = datetime.now(timezone.utc)
        return CodeBlock(
            label="open_page_block",
            code="",
            output_parameter=OutputParameter(
                parameter_type=ParameterType.OUTPUT,
                key="open_page_output",
                description="test output",
                output_parameter_id="op_open_page",
                workflow_id="w_test",
                created_at=now,
                modified_at=now,
            ),
        )

    @pytest.mark.asyncio
    async def test_opened_pages_share_the_recorder_and_close_at_block_end_unless_the_block_closed_them(
        self,
    ) -> None:
        context = FakeSearchBrowserContext()
        recording_page = RecordingPage(SimpleNamespace(context=context, url="https://example.test/listing"))
        opened: list[Page | RecordingPage] = []

        user_function = self._block().generate_async_user_function(
            "first = await open_page(page, 'detail/1')\n"
            "second = await open_page(page, 'https://example.test/detail/2')\n"
            "titles = [await first.title(), await second.title()]\n"
            "await second.close()\n"
            "try:\n"
            "    await open_page(first, 'https://example.test/detail/3')\n"
            "    from_owned = 'no error'\n"
            "except RuntimeError as error:\n"
            "    from_owned = str(error)\n"
            "try:\n"
            "    await open_page(page, 'https://example.test/refused')\n"
            "    refused = 'no error'\n"
            "except Exception as error:\n"
            "    refused = str(error)\n"
            "return {'titles': titles, 'from_owned': from_owned, 'refused': refused}\n",
            recording_page,
            opened_pages=opened,
        )
        result = await user_function()

        assert result["titles"] == ["title of https://example.test/detail/1", "title of https://example.test/detail/2"]
        assert result["from_owned"] == "open_page requires the current CodeBlock page."
        assert result["refused"] == "net::ERR_FAILED"
        assert context.opened[0].url == "https://example.test/detail/1"
        assert all(isinstance(page, RecordingPage) for page in opened)
        # Only a page that reached its URL counts as opened; the refused one closed itself.
        assert [page._underlying_page for page in opened] == context.opened[:2]
        assert [page.closed for page in context.opened] == [False, True, True]
        assert [(action.action_type, action.url) for action in recording_page.recorded_actions()][:2] == [
            (ActionType.GOTO_URL, "https://example.test/detail/1"),
            (ActionType.GOTO_URL, "https://example.test/detail/2"),
        ]

        root_still_open = SimpleNamespace(list_valid_pages=AsyncMock(return_value=[recording_page._underlying_page]))
        assert await _close_opened_code_block_pages(
            opened,
            root_page=recording_page,
            browser_state=root_still_open,  # type: ignore[arg-type]
        ) == (1, None)
        assert [page.closed for page in context.opened] == [True, True, True]

    @pytest.mark.asyncio
    async def test_dispose_keeps_one_opened_page_when_closing_all_would_empty_the_context(self) -> None:
        context = FakeSearchBrowserContext()
        root = SimpleNamespace(context=context, url="https://example.test/listing", is_closed=lambda: True)
        opened: list[Page | RecordingPage] = []
        open_page = _bind_code_block_open_page(root, opened)  # type: ignore[arg-type]
        first = await open_page(root, "https://example.test/detail/1")
        second = await open_page(root, "https://example.test/detail/2")
        only_owned_survive = SimpleNamespace(list_valid_pages=AsyncMock(return_value=[first, second]))

        assert await _close_opened_code_block_pages(
            opened,
            root_page=root,
            browser_state=only_owned_survive,  # type: ignore[arg-type]
        ) == (1, second)
        assert [page.closed for page in context.opened] == [True, False]

    @pytest.mark.asyncio
    async def test_open_page_refuses_at_the_browsers_page_cap_without_opening_a_tab(self) -> None:
        context = FakeSearchBrowserContext()
        page = SimpleNamespace(context=context, url="https://example.test/listing")
        cap = settings.BROWSER_MAX_PAGES_NUMBER
        browser_state = SimpleNamespace(list_valid_pages=AsyncMock(return_value=[object()] * cap))
        open_page = _bind_code_block_open_page(page, [], browser_state)  # type: ignore[arg-type]

        with pytest.raises(RuntimeError, match=f"{cap} tabs are open and the browser's limit is {cap}"):
            await open_page(page, "https://example.test/detail/1")
        assert context.opened == []
        browser_state.list_valid_pages.assert_awaited_once_with(max_pages=0)

        # No browser state for the run: the context's own open pages fill the cap just the same.
        for _ in range(cap):
            await context.new_page()
        without_state = _bind_code_block_open_page(page, [])  # type: ignore[arg-type]
        with pytest.raises(RuntimeError, match=f"{cap} tabs are open and the browser's limit is {cap}"):
            await without_state(page, "https://example.test/detail/1")
        assert len(context.opened) == cap

    @pytest.mark.asyncio
    @pytest.mark.parametrize("native_close", [True, False], ids=["lost_during_the_tab_count", "browser_alive"])
    async def test_open_page_opens_no_tab_after_a_loss_during_its_tab_count(self, native_close: bool) -> None:
        from skyvern.exceptions import MissingBrowserState

        context = FakeSearchBrowserContext()
        page = SimpleNamespace(context=context, url="https://example.test/listing")
        browser_state, browser_context = run_bound_browser_state("wr_open_page")

        async def count_tabs(**_: object) -> list[object]:
            if native_close:
                browser_state._on_browser_context_closed(browser_context)
            return []

        browser_state.list_valid_pages = count_tabs  # type: ignore[method-assign]
        open_page = _bind_code_block_open_page(page, [], browser_state)  # type: ignore[arg-type]

        with pytest.raises(MissingBrowserState) if native_close else nullcontext():
            await open_page(page, "https://example.test/detail/1")
        assert len(context.opened) == (0 if native_close else 1)

    @pytest.mark.asyncio
    async def test_open_page_treats_a_non_positive_cap_as_unlimited(self, monkeypatch: pytest.MonkeyPatch) -> None:
        monkeypatch.setattr(settings, "BROWSER_MAX_PAGES_NUMBER", 0)
        context = FakeSearchBrowserContext()
        page = SimpleNamespace(context=context, url="https://example.test/listing")
        for _ in range(3):
            await context.new_page()

        await _bind_code_block_open_page(page, [])(page, "https://example.test/detail/1")  # type: ignore[arg-type]
        assert len(context.opened) == 4

    @pytest.mark.asyncio
    async def test_disposal_hands_the_selection_back_to_the_root_when_it_closed_the_selected_page(self) -> None:
        context = FakeSearchBrowserContext()
        root = SimpleNamespace(context=context, url="https://example.test/listing", is_closed=lambda: False)
        recording_root = RecordingPage(root)
        opened: list[Page | RecordingPage] = []
        open_page = _bind_code_block_open_page(recording_root, opened)
        detail = await open_page(recording_root, "https://example.test/detail/1")
        selected = detail._underlying_page if isinstance(detail, RecordingPage) else detail
        browser_state = SimpleNamespace(
            get_working_page=AsyncMock(return_value=selected),
            set_active_page=AsyncMock(),
        )

        await dispose_opened_code_block_pages(
            opened,
            browser_state=browser_state,  # type: ignore[arg-type]
            root_page=recording_root,
            engine="inline",
            workflow_run_id="wr-1",
            workflow_run_block_id="wrb-1",
            organization_id="org-1",
        )

        assert selected.closed is True
        browser_state.get_working_page.assert_awaited_once_with(prune_excess_pages=False)
        browser_state.set_active_page.assert_awaited_once_with(root, prune_excess_pages=False)

    @pytest.mark.asyncio
    @pytest.mark.parametrize("root_closed", [False, True])
    async def test_disposal_re_pins_nothing_unless_the_selected_page_was_closed(self, root_closed: bool) -> None:
        context = FakeSearchBrowserContext()
        root = SimpleNamespace(context=context, url="https://example.test/listing", is_closed=lambda: root_closed)
        opened: list[Page | RecordingPage] = []
        detail = await _bind_code_block_open_page(root, opened)(root, "https://example.test/detail/1")  # type: ignore[arg-type]
        popup = SimpleNamespace(url="https://example.test/popup", is_closed=lambda: False)
        browser_state = SimpleNamespace(
            get_working_page=AsyncMock(return_value=detail if root_closed else root),
            set_active_page=AsyncMock(),
            list_valid_pages=AsyncMock(return_value=[popup, detail]),
        )

        await dispose_opened_code_block_pages(
            opened,
            browser_state=browser_state,  # type: ignore[arg-type]
            root_page=root,  # type: ignore[arg-type]
            engine="inline",
            workflow_run_id="wr-1",
            workflow_run_block_id="wrb-1",
            organization_id="org-1",
        )

        assert context.opened[0].closed is True
        browser_state.set_active_page.assert_not_awaited()

    @pytest.mark.asyncio
    async def test_a_failure_on_an_owned_page_is_reported_from_that_page_before_it_is_closed(
        self, monkeypatch: pytest.MonkeyPatch
    ) -> None:
        context = FakeSearchBrowserContext()
        timeout = PlaywrightTimeoutError("locator timed out")

        class DetailLocator:
            def __init__(self, page: FakeSearchPage) -> None:
                self.page = page

            async def click(self, **_kwargs: Any) -> None:
                raise timeout

            async def evaluate(self, _script: str, **_kwargs: Any) -> str:
                return '<div id="cover">'

        class DetailPage(FakeSearchPage):
            def locator(self, _selector: str, **_kwargs: Any) -> DetailLocator:
                return DetailLocator(self)

            @property
            def main_frame(self) -> SimpleNamespace:
                return SimpleNamespace(page=self, goto=self.goto, child_frames=[], parent_frame=None)

        async def new_page() -> DetailPage:
            page = DetailPage(context=context)
            context.opened.append(page)
            return page

        context.new_page = new_page  # type: ignore[method-assign]
        root = SimpleNamespace(context=context, url="https://example.test/listing", is_closed=lambda: False)
        root.title = AsyncMock(return_value="Listing")
        browser_state = SimpleNamespace(
            browser_artifacts=BrowserArtifacts(),
            get_working_page=AsyncMock(return_value=root),
            list_valid_pages=AsyncMock(return_value=[root]),
            set_active_page=AsyncMock(),
        )

        monkeypatch.setattr(
            "skyvern.forge.sdk.workflow.models.block.app.AGENT_FUNCTION.validate_code_block", AsyncMock()
        )
        monkeypatch.setattr(CodeBlock, "get_or_create_browser_state", AsyncMock(return_value=browser_state))
        monkeypatch.setattr(CodeBlock, "get_workflow_run_context", lambda *args: FakeWorkflowRunContext(values={}))
        monkeypatch.setattr(CodeBlock, "record_output_parameter_value", AsyncMock())
        block = self._block()
        block.code = (
            "detail = await open_page(page, 'https://example.test/detail/1')\nawait detail.locator('#berth').click()\n"
        )

        result = await block.execute(workflow_run_id="wrid_test", workflow_run_block_id="")

        assert result.success is False
        # The block's own page stays the block's final URL; the opened page is named beside it.
        assert "Final URL: https://example.test/listing" in result.failure_reason
        assert "Page title: Listing" in result.failure_reason
        assert "Failed on opened page: https://example.test/detail/1" in result.failure_reason
        assert 'Covering element: <div id="cover">' in result.failure_reason
        # Closed after the evidence was read, not before.
        assert [page.closed for page in context.opened] == [True]

        # A page-level call on the opened page names it the same way, with no locator to ask.
        context.opened.clear()
        block.code = "detail = await open_page(page, 'https://example.test/detail/2')\nawait detail.goto('/refused')\n"
        monkeypatch.setattr(
            "skyvern.forge.sdk.workflow.models.block.app.AGENT_FUNCTION.validate_code_block", AsyncMock()
        )

        async def timing_out_goto(self: FakeSearchPage, url: str, **_kwargs: object) -> None:
            if url.endswith("/refused"):
                raise timeout
            self.url = url

        monkeypatch.setattr(DetailPage, "goto", timing_out_goto)
        result = await block.execute(workflow_run_id="wrid_test", workflow_run_block_id="")

        assert result.success is False
        assert "Final URL: https://example.test/listing" in result.failure_reason
        assert "Failed on opened page: https://example.test/detail/2" in result.failure_reason
        assert "Covering element" not in result.failure_reason

        # A navigation through the opened page's frame names that page too.
        context.opened.clear()
        block.code = "detail = await open_page(page, 'https://example.test/detail/3')\nawait detail.main_frame.goto('/refused')\n"
        result = await block.execute(workflow_run_id="wrid_test", workflow_run_block_id="")

        assert result.success is False
        assert "Failed on opened page: https://example.test/detail/3" in result.failure_reason

    @pytest.mark.asyncio
    async def test_a_failure_on_a_claimed_popup_names_that_popup_and_leaves_it_open(
        self, monkeypatch: pytest.MonkeyPatch
    ) -> None:
        context = FakeSearchBrowserContext()
        timeout = PlaywrightTimeoutError("locator timed out")

        class PopupLocator:
            def __init__(self, page: FakeSearchPage) -> None:
                self.page = page

            async def click(self, **_kwargs: Any) -> None:
                raise timeout

            async def evaluate(self, _script: str, **_kwargs: Any) -> str:
                return '<div id="cover">'

        class PopupPage(FakeSearchPage):
            def locator(self, _selector: str, **_kwargs: Any) -> PopupLocator:
                return PopupLocator(self)

        popup = PopupPage(context=context)
        popup.url = "https://example.test/sign-in"

        @asynccontextmanager
        async def expect_popup(**_kwargs: Any) -> AsyncIterator[SimpleNamespace]:
            value: asyncio.Future[PopupPage] = asyncio.get_running_loop().create_future()
            value.set_result(popup)
            yield SimpleNamespace(value=value)

        root = SimpleNamespace(
            context=context, url="https://example.test/listing", is_closed=lambda: False, expect_popup=expect_popup
        )
        root.title = AsyncMock(return_value="Listing")
        browser_state = SimpleNamespace(
            browser_artifacts=BrowserArtifacts(),
            get_working_page=AsyncMock(return_value=root),
            list_valid_pages=AsyncMock(return_value=[root]),
            set_active_page=AsyncMock(),
        )
        monkeypatch.setattr(
            "skyvern.forge.sdk.workflow.models.block.app.AGENT_FUNCTION.validate_code_block", AsyncMock()
        )
        monkeypatch.setattr(CodeBlock, "get_or_create_browser_state", AsyncMock(return_value=browser_state))
        monkeypatch.setattr(CodeBlock, "get_workflow_run_context", lambda *args: FakeWorkflowRunContext(values={}))
        monkeypatch.setattr(CodeBlock, "record_output_parameter_value", AsyncMock())
        block = self._block()
        block.code = (
            "async with page.expect_popup() as popup_info:\n"
            "    pass\n"
            "popup = await popup_info.value\n"
            "await popup.locator('#user').click()\n"
        )

        result = await block.execute(workflow_run_id="wrid_test", workflow_run_block_id="")

        assert result.success is False
        assert "Final URL: https://example.test/listing" in result.failure_reason
        assert "Failed on opened page: https://example.test/sign-in" in result.failure_reason
        assert popup.closed is False

    @pytest.mark.asyncio
    async def test_open_page_fails_closed_without_a_run_browser(self) -> None:
        with pytest.raises(RuntimeError, match="only supported while the run browser is open"):
            await CodeBlock.build_safe_vars()["open_page"](None, "https://example.test/")

    @pytest.mark.asyncio
    async def test_open_page_names_a_relative_url_it_cannot_resolve(self) -> None:
        context = FakeSearchBrowserContext()
        page = SimpleNamespace(context=context, url="about:blank")
        open_page = _bind_code_block_open_page(page, [])  # type: ignore[arg-type]

        with pytest.raises(ValueError, match="not 'detail/1'"):
            await open_page(page, "detail/1")
        with pytest.raises(ValueError, match="not 'file:///etc/passwd'"):
            await open_page(page, "file:///etc/passwd")
        assert context.opened == []


class TestClearBrowserDataHelperBinding:
    @staticmethod
    def _block() -> CodeBlock:
        now = datetime.now(timezone.utc)
        return CodeBlock(
            label="clear_block",
            code="",
            output_parameter=OutputParameter(
                parameter_type=ParameterType.OUTPUT,
                key="clear_output",
                description="test output",
                output_parameter_id="op_clear",
                workflow_id="w_test",
                created_at=now,
                modified_at=now,
            ),
        )

    def test_clear_browser_data_is_reserved_in_safe_vars(self) -> None:
        assert "clear_browser_data" in CodeBlock.build_safe_vars()

    def test_the_helper_authored_code_gets_takes_only_the_page(self) -> None:
        # The secure runner's wrapper accepts the page and nothing else, so a block that named
        # origins here would run in one executor and be rejected by the other. Worker-side callers
        # that do have origins to name use the private function instead.
        bound = CodeBlock.build_safe_vars()["clear_browser_data"]

        assert list(inspect.signature(bound).parameters) == ["page"]

    @pytest.mark.asyncio
    async def test_a_persisted_parameter_named_clear_browser_data_keeps_its_value(self) -> None:
        # The name was a valid parameter name before it became a helper; a block that declared it
        # must keep receiving its value rather than the function.
        user_function = self._block().generate_async_user_function(
            'return {"value": clear_browser_data}\n',
            SimpleNamespace(url="about:blank", context=FakeClearingBrowserContext()),  # type: ignore[arg-type]
            {"clear_browser_data": "keep"},
        )

        assert await user_function() == {"value": "keep"}

    @pytest.mark.asyncio
    async def test_a_persisted_parameter_named_open_page_keeps_its_value(self) -> None:
        user_function = self._block().generate_async_user_function(
            'return {"value": open_page}\n',
            SimpleNamespace(url="about:blank", context=FakeClearingBrowserContext()),  # type: ignore[arg-type]
            {"open_page": "keep"},
        )

        assert await user_function() == {"value": "keep"}

    @pytest.mark.asyncio
    async def test_publish_file_in_process_refuses_with_its_reason(self) -> None:
        user_function = self._block().generate_async_user_function(
            'await publish_file("report.txt", text="hi")\nreturn {}\n',
            SimpleNamespace(url="about:blank", context=FakeClearingBrowserContext()),  # type: ignore[arg-type]
        )

        with pytest.raises(RuntimeError, match="secure code runner"):
            await user_function()

    @pytest.mark.asyncio
    async def test_a_persisted_parameter_named_publish_file_keeps_its_value(self) -> None:
        user_function = self._block().generate_async_user_function(
            'return {"value": publish_file}\n',
            SimpleNamespace(url="about:blank", context=FakeClearingBrowserContext()),  # type: ignore[arg-type]
            {"publish_file": "keep"},
        )

        assert await user_function() == {"value": "keep"}

    @pytest.mark.asyncio
    @pytest.mark.parametrize(
        ("opener_is_open", "inherits", "expected_session_clears"),
        [
            (
                True,
                True,
                [
                    {"securityOrigin": "https://portal.example", "isLocalStorage": False},
                    {"storageKey": "https://portal.example/", "isLocalStorage": False},
                ],
            ),
            (True, False, [{"securityOrigin": "https://portal.example", "isLocalStorage": False}]),
            (False, True, [{"storageKey": "https://portal.example/", "isLocalStorage": False}]),
        ],
        ids=["window_opened_on_about_blank", "tab_on_an_opaque_origin", "only_the_opened_window_is_left"],
    )
    async def test_a_tab_whose_url_names_no_origin_is_cleared_by_the_origin_it_inherited(
        self, opener_is_open: bool, inherits: bool, expected_session_clears: list[dict]
    ) -> None:
        # A window opened on about:blank keeps a copy of its opener's sessionStorage in a tab area of
        # its own, which clearing by origin does not reach and the opener's own clear does not
        # address. A tab that inherited nothing has no storage area at all and must not fail the run,
        # and one left without its opener is the only document still naming that origin.
        context = FakeClearingBrowserContext()
        popup = SimpleNamespace(url="about:blank", context=context, frames=[SimpleNamespace(url="about:blank")])
        opener = SimpleNamespace(
            url="https://portal.example/login",
            context=context,
            frames=[SimpleNamespace(url="https://portal.example/login")],
        )
        page = opener if opener_is_open else SimpleNamespace(url="about:blank", context=context, frames=[])
        context.pages = [page, popup]
        # A tab that inherited nothing is on an opaque origin, which reaches no storage either.
        context.frames_without_storage = [] if opener_is_open else [page]
        if inherits:
            context.inherited_storage_keys = [(popup, "https://portal.example/")]
        else:
            context.frames_without_storage.append(popup)

        user_function = self._block().generate_async_user_function(
            'await clear_browser_data(page)\nreturn {"cleared": True}\n',
            page,  # type: ignore[arg-type]
        )

        assert await user_function() == {"cleared": True}
        sent = [call for _, session in context.cdp_sessions for call in session.sent]
        assert [params["storageId"] for method, params in sent if method == "DOMStorage.clear"] == (
            expected_session_clears
        )
        # The origin an inherited-origin tab names still has to reach the origin-keyed pass, which is
        # all that clears its persistent storage once no other document spells it.
        assert [params["origin"] for method, params in sent if method == "Storage.clearDataForOrigin"] == [
            "https://portal.example"
        ]

    @pytest.mark.asyncio
    async def test_a_tab_that_reaches_storage_but_cannot_be_keyed_fails_the_clear(self) -> None:
        # Only a tab with no storage area at all has nothing to clear. A lookup that fails on a tab
        # which CAN reach storage -- a timeout, say -- must not pass for one, or the helper reports a
        # clear while that tab keeps its session.
        context = FakeClearingBrowserContext()
        popup = SimpleNamespace(url="about:blank", context=context, frames=[SimpleNamespace(url="about:blank")])
        page = SimpleNamespace(
            url="https://portal.example/login",
            context=context,
            frames=[SimpleNamespace(url="https://portal.example/login")],
        )
        context.pages = [page, popup]

        user_function = self._block().generate_async_user_function(
            'await clear_browser_data(page)\nreturn {"cleared": True}\n',
            page,  # type: ignore[arg-type]
        )

        with pytest.raises(PlaywrightError):
            await user_function()
        assert context.clear_cookies_calls == 0

    @pytest.mark.asyncio
    async def test_an_origin_that_closes_during_the_clear_still_has_its_stored_data_cleared(self) -> None:
        # The session-storage pass awaits, and a page can drop a frame while it does. The origin was
        # open when the clear began, so its stored data is still the run's to clear.
        context = FakeClearingBrowserContext()
        embedded = SimpleNamespace(url="https://embedded.example/widget")
        page = SimpleNamespace(
            url="https://portal.example/login",
            context=context,
            frames=[SimpleNamespace(url="https://portal.example/login"), embedded],
        )
        context.pages = [page]
        original_new_cdp_session = context.new_cdp_session

        async def dropping_frame_session(target: object) -> object:
            session = await original_new_cdp_session(target)
            page.frames = [page.frames[0]]
            return session

        context.new_cdp_session = dropping_frame_session  # type: ignore[assignment]

        user_function = self._block().generate_async_user_function(
            'await clear_browser_data(page)\nreturn {"cleared": True}\n',
            page,  # type: ignore[arg-type]
        )

        assert await user_function() == {"cleared": True}
        sent = [call for _, session in context.cdp_sessions for call in session.sent]
        assert [params["origin"] for method, params in sent if method == "Storage.clearDataForOrigin"] == [
            "https://portal.example",
            "https://embedded.example",
        ]

    @pytest.mark.asyncio
    async def test_a_frame_without_a_session_of_its_own_is_cleared_through_the_page(self) -> None:
        # A frame sharing its parent's renderer is refused a session of its own, and is the ordinary
        # case: same-site iframes, and any cross-origin one when site isolation is off. Its clear has
        # to fall back to the page's session, which can reach it, or nothing clears it at all.
        context = FakeClearingBrowserContext()
        embedded = SimpleNamespace(url="https://embedded.example/widget")
        page = SimpleNamespace(
            url="https://portal.example/login",
            context=context,
            frames=[SimpleNamespace(url="https://portal.example/login"), embedded],
        )
        context.pages = [page]
        context.frames_without_own_session = [embedded]

        user_function = self._block().generate_async_user_function(
            'await clear_browser_data(page)\nreturn {"cleared": True}\n',
            page,  # type: ignore[arg-type]
        )

        assert await user_function() == {"cleared": True}
        cleared = [call for _, session in context.cdp_sessions for call in session.sent]
        assert (
            "DOMStorage.clear",
            {"storageId": {"securityOrigin": "https://embedded.example", "isLocalStorage": False}},
        ) in cleared

    @pytest.mark.asyncio
    @pytest.mark.parametrize(
        ("has_own_session", "reaches_storage", "on_page_target", "skipped"),
        [
            (True, False, False, True),
            (True, True, False, False),
            (False, False, False, False),
            (True, False, True, True),
        ],
        ids=[
            "sandboxed_frame",
            "real_defect",
            "same_process_frame_cannot_be_classified",
            "sandboxed_frame_whose_session_is_on_the_page_target",
        ],
    )
    @pytest.mark.parametrize("refusal_error", [PlaywrightError, CdpError], ids=["playwright", "raw_cdp"])
    async def test_a_frame_whose_clear_fails_is_skipped_only_when_it_reports_no_storage_of_its_own(
        self,
        has_own_session: bool,
        reaches_storage: bool,
        on_page_target: bool,
        skipped: bool,
        refusal_error: type[BaseException],
    ) -> None:
        # A sandboxed iframe has no storage area to address, so a clear that cannot find one found
        # nothing to clear and the block carries on. A frame that CAN reach storage is an addressing
        # defect. And a frame sharing its parent's renderer cannot be asked at all -- the probe would
        # answer for the page -- so its failure surfaces rather than being explained away.
        context = FakeClearingBrowserContext()
        embedded = SimpleNamespace(url="https://embedded.example/widget")
        page = SimpleNamespace(
            url="https://portal.example/login",
            context=context,
            frames=[SimpleNamespace(url="https://portal.example/login"), embedded],
        )
        context.pages = [page]
        context.origins_refusing_clear = ["https://embedded.example"]
        # Both engines a run can select refuse the same way; only the error family differs.
        context.refusal_error = refusal_error
        if not has_own_session:
            context.frames_without_own_session = [embedded]
            # Even with the page itself reporting no storage, the frame's failure still surfaces.
            context.frames_without_storage = [page]
        elif not reaches_storage:
            context.frames_without_storage = [embedded]
        if on_page_target:
            context.frames_attached_to_page_target = [(embedded, page)]

        user_function = self._block().generate_async_user_function(
            'await clear_browser_data(page)\nreturn {"cleared": True}\n',
            page,  # type: ignore[arg-type]
        )

        if not skipped:
            with pytest.raises(refusal_error):
                await user_function()
            # The cookie drop comes after this pass, so the run is still recoverable.
            assert context.clear_cookies_calls == 0
            return
        assert await user_function() == {"cleared": True}
        assert context.clear_cookies_calls == 1

    @pytest.mark.asyncio
    async def test_an_origin_is_cleared_through_a_frame_that_can_reach_its_storage(self) -> None:
        # A sandboxed frame spells its origin exactly as an ordinary frame does, so an origin can be
        # represented by one frame that reaches no storage and another that holds it. Passing over the
        # first has to leave the second its turn, or that storage survives a clear reporting success.
        context = FakeClearingBrowserContext()
        sandboxed = SimpleNamespace(url="https://shared.example/ad")
        ordinary = SimpleNamespace(url="https://shared.example/widget")
        page = SimpleNamespace(
            url="https://portal.example/login",
            context=context,
            frames=[SimpleNamespace(url="https://portal.example/login"), sandboxed, ordinary],
        )
        context.pages = [page]
        context.frames_refusing_clear = [sandboxed]
        context.frames_without_storage = [sandboxed]

        user_function = self._block().generate_async_user_function(
            'await clear_browser_data(page)\nreturn {"cleared": True}\n',
            page,  # type: ignore[arg-type]
        )

        assert await user_function() == {"cleared": True}
        cleared_through = [
            attached_to
            for attached_to, session in context.cdp_sessions
            for method, _ in session.sent
            if method == "DOMStorage.clear"
        ]
        assert ordinary in cleared_through

    @pytest.mark.asyncio
    async def test_a_clear_failing_with_something_other_than_a_driver_error_is_not_excused(self) -> None:
        # Only a browser driver's own refusal is a candidate for "there was nothing to clear"; anything
        # else is a fault, and excusing it would report a clear that never happened.
        context = FakeClearingBrowserContext()
        embedded = SimpleNamespace(url="https://embedded.example/widget")
        page = SimpleNamespace(
            url="https://portal.example/login",
            context=context,
            frames=[SimpleNamespace(url="https://portal.example/login"), embedded],
        )
        context.pages = [page]
        context.origins_failing_unexpectedly = ["https://embedded.example"]
        context.frames_without_storage = [embedded]

        user_function = self._block().generate_async_user_function(
            'await clear_browser_data(page)\nreturn {"cleared": True}\n',
            page,  # type: ignore[arg-type]
        )

        with pytest.raises(ValueError):
            await user_function()
        assert context.clear_cookies_calls == 0

    @pytest.mark.asyncio
    @pytest.mark.parametrize(
        ("page_url", "expected_origin"),
        [
            ("https://portal.example/login?next=%2Fhome", "https://portal.example"),
            ("https://svc:[email protected]:8443/login", "https://portal.example:8443"),
            # A blob document reaches the storage of the origin that minted it, and carries that
            # origin inside its own URL rather than as a host of its own.
            ("blob:https://portal.example/9f1c2f5e-7a44-4d3e", "https://portal.example"),
            ("about:blank", ""),
            # An opaque blob has no origin to reach, so there is nothing of its own to clear.
            ("blob:null/9f1c2f5e-7a44-4d3e", ""),
        ],
        ids=["web_origin", "userinfo_in_url", "blob_of_a_web_origin", "no_origin", "opaque_blob"],
    )
    async def test_authored_code_clears_cookies_and_every_origin_in_the_context(
        self, page_url: str, expected_origin: str
    ) -> None:
        # Cookies always go; stored data goes for the origins the context holds a document for, and a
        # page whose URL names none is asked which origin it inherited instead.
        context = FakeClearingBrowserContext()
        page = SimpleNamespace(url=page_url, context=context, frames=[SimpleNamespace(url=page_url)])
        context.pages = [page]
        if not expected_origin:
            context.frames_without_storage = [page]

        user_function = self._block().generate_async_user_function(
            'await clear_browser_data(page)\nreturn {"cleared": True}\n',
            page,  # type: ignore[arg-type]
        )
        result = await user_function()

        assert result == {"cleared": True}
        assert context.clear_cookies_calls == 1
        if not expected_origin:
            # This tab inherited no origin either, so the lookup is all that happens and nothing is
            # cleared for it.
            assert [(attached_to, session.sent) for attached_to, session in context.cdp_sessions] == [
                (
                    page,
                    [
                        ("Page.getFrameTree", None),
                        ("Storage.getStorageKeyForFrame", {"frameId": "frame-of-this-session"}),
                    ],
                ),
            ]
            assert all(session.detached for _, session in context.cdp_sessions)
            return
        assert [(attached_to, session.sent) for attached_to, session in context.cdp_sessions] == [
            (
                page.frames[0],
                [("DOMStorage.clear", {"storageId": {"securityOrigin": expected_origin, "isLocalStorage": False}})],
            ),
            (page, [("Storage.clearDataForOrigin", {"origin": expected_origin, "storageTypes": "all"})]),
        ]
        assert all(session.detached for _, session in context.cdp_sessions)


class TestInertSlotRenderIsTheSpliceReference:
    """The gate compares the real render against the same template rendered with inert value slots, so a slot may
    hold a literal and nothing else; no template shape or value can cancel that out."""

    def _block(self, code: str) -> CodeBlock:
        now = datetime.now(UTC)
        return CodeBlock(
            label="read",
            code=code,
            output_parameter=OutputParameter(
                parameter_type=ParameterType.OUTPUT,
                key="read_output",
                description="",
                output_parameter_id="op_read",
                workflow_id="w_test",
                created_at=now,
                modified_at=now,
            ),
        )

    _EXFIL = "await page.context.cookies()"

    @pytest.mark.parametrize(
        ("template", "values", "expect_refused"),
        [
            pytest.param("jar = {{ exfil }}\nresult = jar\n", {"exfil": _EXFIL}, True, id="single-line-slot"),
            pytest.param("jar = {{\nexfil\n}}\nresult = jar\n", {"exfil": _EXFIL}, True, id="multiline-slot"),
            pytest.param("jar = {{- exfil }}\nresult = jar\n", {"exfil": _EXFIL}, True, id="whitespace-control-slot"),
            pytest.param(
                "total = {{- count -}}\n + 1\n", {"count": 41}, False, id="trim-markers-apply-to-both-renders"
            ),
            pytest.param("result = {% print exfil %}\n", {"exfil": _EXFIL}, True, id="print-statement-slot"),
            pytest.param("result = {%- print exfil -%}\n", {"exfil": _EXFIL}, True, id="print-statement-trim-slot"),
            pytest.param("result = {% print count %}\n", {"count": 7}, False, id="print-statement-literal"),
            pytest.param(
                '{% filter replace("SLOT", payload) %}result = SLOT{% endfilter %}\n',
                {"payload": _EXFIL},
                True,
                id="filter-block-with-value-argument",
            ),
            pytest.param(
                '{% filter replace(old="SLOT", new=payload) %}result = SLOT{% endfilter %}\n',
                {"payload": _EXFIL},
                True,
                id="filter-block-with-value-keyword",
            ),
            pytest.param(
                '{% filter replace("SLOT", "7") | upper %}RESULT = SLOT\n{% endfilter %}',
                {},
                False,
                id="filter-block-with-constant-arguments",
            ),
            pytest.param(
                "{% macro emit(v) %}result = {{ v }}{% endmacro %}{{ emit(payload) }}\n",
                {"payload": _EXFIL},
                True,
                id="macro-emitting-a-value",
            ),
            pytest.param(
                "{% macro wrap() %}result = {{ caller() }}{% endmacro %}{% call wrap() %}{{ payload }}{% endcall %}\n",
                {"payload": _EXFIL},
                True,
                id="call-block-emitting-a-value",
            ),
            pytest.param(
                "{% set n %}{{ count }}{% endset %}result = {{ n }}\n",
                {"count": 7},
                False,
                id="set-block-then-literal-slot",
            ),
            pytest.param(
                "{% if flag %}page.context.set_default_timeout(1)\n{% endif %}jar = {{ exfil }}\n",
                {"flag": False, "exfil": _EXFIL},
                True,
                id="false-branch-decoy",
            ),
            pytest.param(
                "ctx = page.context\nresult = {{ payload }}\n",
                {"payload": "await ctx.cookies()"},
                True,
                id="authored-alias-consumed-by-value",
            ),
            pytest.param(
                "__skyvern_slot__0 = page.context\nresult = await {{ alias }}.cookies()\n",
                {"alias": "__skyvern_slot__0"},
                True,
                id="value-equal-to-the-inert-marker",
            ),
            pytest.param(
                "x = {{ payload }}; page.context.set_default_timeout(1)\n",
                {"payload": f"{_EXFIL} or None #"},
                True,
                id="value-comments-out-authored-statement",
            ),
            pytest.param("total = {{ count }} + 1\n", {"count": 41}, False, id="number-slot"),
            pytest.param('url = "{{ base }}/x"\n', {"base": "https://a.example"}, False, id="slot-inside-string"),
            pytest.param("flag = {{ on }}\n", {"on": True}, False, id="bool-slot"),
            pytest.param(
                "rows = []\n{% for r in items %}rows.append({{ r }})\n{% endfor %}",
                {"items": [{"a": 1}, {"a": "b"}]},
                False,
                id="json-literal-per-loop-iteration",
            ),
            pytest.param(
                "# note {{ exfil }}\nresult = 1\n", {"exfil": _EXFIL}, False, id="slot-inside-comment-is-masked"
            ),
            pytest.param("result = await page.context.cookies()\n", {}, False, id="no-slots-authored-read"),
        ],
    )
    def test_slots_may_hold_literals_only(self, template: str, values: dict[str, object], expect_refused: bool) -> None:
        block = self._block(template)

        authored = block.render_code_with_reference(FakeWorkflowRunContext(values=values))

        errors = rendering_introduced_security_errors(label="read", authored_code=authored, rendered_code=block.code)
        assert bool(errors) is expect_refused, (authored, block.code)

    @pytest.mark.parametrize(
        ("template", "values", "named_expression", "names_json"),
        [
            pytest.param(
                "data = json.loads({{ sheet | tojson }})\n",
                {"sheet": {"rows": None, "cells": [{"bold": True}]}},
                "{{ sheet | tojson }}",
                True,
                id="tojson-output-pasted-as-python",
            ),
            pytest.param(
                "x = {{ a }}\ny = {{ b }}\n",
                {"a": 1, "b": _EXFIL},
                "{{ b }}",
                False,
                id="second-slot-carries-code",
            ),
        ],
    )
    def test_refusal_names_the_slot_that_carried_code(
        self, template: str, values: dict[str, object], named_expression: str, names_json: bool
    ) -> None:
        block = self._block(template)

        authored = block.render_code_with_reference(FakeWorkflowRunContext(values=values))

        [error] = rendering_introduced_security_errors(label="read", authored_code=authored, rendered_code=block.code)
        assert f"`{named_expression}`" in error
        assert ("rendered JSON" in error) is names_json

    def test_reference_render_does_not_consume_what_the_real_render_consumed(self) -> None:
        block = self._block("{% set item = items.pop() %}result = {{ item }}\n")
        context = FakeWorkflowRunContext(values={"items": [7]})

        authored = block.render_code_with_reference(context)

        assert block.code.strip() == "result = 7"
        assert authored is not None
        assert (
            rendering_introduced_security_errors(label="read", authored_code=authored, rendered_code=block.code) == []
        )
        assert context.values["items"] == []

    def test_template_that_pulls_in_another_template_fails_before_the_gate(self) -> None:
        block = self._block('{% include "other" %}result = 1\n')

        with pytest.raises(FailedToFormatJinjaStyleParameter):
            block.format_potential_template_parameters(FakeWorkflowRunContext(values={}))

    def test_slot_form_the_rewrite_misses_is_rejected_by_the_parse(self, monkeypatch: pytest.MonkeyPatch) -> None:
        monkeypatch.setattr(code_block_security, "_PRINT_SLOT_RE", re.compile(r"(?!x)(x)(y)"))
        block = self._block("result = {% print exfil %}\n")

        assert block.render_code_with_inert_slots(block.code, FakeWorkflowRunContext(values={"exfil": "1"})) is None

    def test_prompt_and_error_mapping_still_render_after_code(self) -> None:
        block = self._block("result = 1\n")
        block.prompt = "hello {{ name }}"

        block.format_potential_template_parameters(FakeWorkflowRunContext(values={"name": "world"}))

        assert block.prompt == "hello world"