Skip to content

Automate browser based workflows with AI

Resolve the login-block credential the same way at run setup and mid-run (#8002)

Celal Zamanoğlu4416027817e7
diff --git a/skyvern/forge/sdk/workflow/service.py b/skyvern/forge/sdk/workflow/service.py
index 90915a4db..143b1582c 100644
--- a/skyvern/forge/sdk/workflow/service.py
+++ b/skyvern/forge/sdk/workflow/service.py
@@ -680,29 +680,6 @@ def _build_managed_browser_profile_name(workflow_title: str | None, rendered_key
return f"{title}{suffix}"
-def _credential_id_from_setup_parameter(parameter: Any, parameter_values: dict[str, Any]) -> str | None:
- """Resolve the credential id a credential-typed block parameter points at from in-memory run
- parameters. The per-run rotation selection (keyed by parameter key) wins over the static value.
- Only credential-typed parameters are consulted, so an unrelated string input can never be mistaken
- for a credential id."""
- if isinstance(parameter, CredentialParameter):
- selected = parameter_values.get(parameter.key)
- if isinstance(selected, str) and selected:
- return selected
- return parameter.credential_id
- if (
- isinstance(parameter, WorkflowParameter)
- and parameter.workflow_parameter_type == WorkflowParameterType.CREDENTIAL_ID
- ):
- selected = parameter_values.get(parameter.key)
- if isinstance(selected, str) and selected:
- return selected
- default_value = parameter.default_value
- if isinstance(default_value, str) and default_value:
- return default_value
- return None
-
-
class WorkflowService:
# Prevent GC of fire-and-forget asyncio tasks (e.g. task_run sync).
_background_tasks: set[asyncio.Task] = set() # noqa: RUF012
@@ -2255,9 +2232,10 @@ class WorkflowService:
# stamp keeps today's path.
credential_browser_profile_id = await self._resolve_setup_credential_seed(
workflow=workflow,
- parameter_values=parameter_values,
+ workflow_run_id=workflow_run.workflow_run_id,
organization_id=organization_id,
engine_enabled=engine_enabled,
+ parameter_values=parameter_values,
)
if credential_browser_profile_id:
return credential_browser_profile_id, BrowserSeedSource.credential, own_browser_profile_id
@@ -2277,9 +2255,10 @@ class WorkflowService:
# behavior change, so flag-off falls to fresh and the preserved mid-run stamp keeps today's path.
credential_browser_profile_id = await self._resolve_setup_credential_seed(
workflow=workflow,
- parameter_values=parameter_values,
+ workflow_run_id=workflow_run.workflow_run_id,
organization_id=organization_id,
engine_enabled=engine_enabled,
+ parameter_values=parameter_values,
)
if credential_browser_profile_id:
return credential_browser_profile_id, BrowserSeedSource.credential, None
@@ -2290,9 +2269,10 @@ class WorkflowService:
self,
*,
workflow: Workflow,
- parameter_values: dict[str, Any],
+ workflow_run_id: str,
organization_id: str,
engine_enabled: bool,
+ parameter_values: dict[str, Any],
) -> str | None:
"""Setup-time credential-profile seed, gated on the browser-memory engine. Flag-off returns None
so the fleet keeps today's behavior (fresh until the login block's mid-run stamp); the engine era
@@ -2300,7 +2280,10 @@ class WorkflowService:
if not engine_enabled:
return None
return await self._resolve_credential_browser_profile_id_for_setup(
- workflow=workflow, parameter_values=parameter_values, organization_id=organization_id
+ workflow=workflow,
+ workflow_run_id=workflow_run_id,
+ organization_id=organization_id,
+ parameter_values=parameter_values,
)
async def _resolve_picked_profile_role(
@@ -2334,23 +2317,56 @@ class WorkflowService:
)
return "error", None
+ async def _resolve_single_login_credential_ids_for_setup(
+ self, *, workflow: Workflow, workflow_run_id: str, organization_id: str, parameter_values: dict[str, Any]
+ ) -> list[str]:
+ """Credential ids for the run's single unambiguous login block, resolved via the SAME rich path
+ as the mid-run login-block stamp (workflow_run_context / rotation pool / DB fallback selection /
+ static credential id). Resolving pool- and DB-selected credentials here is what lets setup seed
+ and pin the same account the block signs into — the only loader that can win when a cached-script
+ or pre-navigating block opens the browser before the login block runs, which locks the mid-run
+ loader out. parameter_values carries the in-memory render values so a request-supplied
+ WorkflowParameter/CREDENTIAL_ID resolves at setup, before run parameters are persisted.
+
+ Returns an empty list when resolution must defer to the mid-run stamp: any conditional branching
+ (the executing branch is unknown at setup) or not exactly one non-skip login block. Best-effort:
+ a resolution failure defers rather than aborting setup."""
+ all_blocks = get_all_blocks(workflow.workflow_definition.blocks)
+ if any(isinstance(block, ConditionalBlock) for block in all_blocks):
+ return []
+ login_blocks = [b for b in all_blocks if isinstance(b, LoginBlock) and not b.skip_saved_profile]
+ if len(login_blocks) != 1:
+ return []
+ try:
+ return await self._resolve_login_block_credential_ids(
+ login_blocks[0],
+ workflow_run_id,
+ organization_id,
+ workflow.workflow_permanent_id,
+ run_parameter_values=parameter_values,
+ )
+ except Exception:
+ LOG.warning(
+ "Failed to resolve setup login credential ids",
+ workflow_permanent_id=workflow.workflow_permanent_id,
+ exc_info=True,
+ )
+ return []
+
async def _resolve_active_credential_pin_for_setup(
- self, *, workflow: Workflow, parameter_values: dict[str, Any], organization_id: str
+ self, *, workflow: Workflow, workflow_run_id: str, organization_id: str, parameter_values: dict[str, Any]
) -> tuple[str, str] | None:
"""The run's active single-login credential's dedicated-IP pin at setup — (credential_id,
proxy_session_id) if that credential pins its IP, else None. Same single-unambiguous-login guard
as the credential-profile seed (branches / multiple logins defer). B4: this pin wins over the
seed profile's own pin (the site tracks IP per account)."""
- all_blocks = get_all_blocks(workflow.workflow_definition.blocks)
- if any(isinstance(block, ConditionalBlock) for block in all_blocks):
- return None
- login_blocks = [b for b in all_blocks if isinstance(b, LoginBlock) and not b.skip_saved_profile]
- if len(login_blocks) != 1:
- return None
- for param in login_blocks[0].parameters:
- credential_id = _credential_id_from_setup_parameter(param, parameter_values)
- if not credential_id:
- continue
+ credential_ids = await self._resolve_single_login_credential_ids_for_setup(
+ workflow=workflow,
+ workflow_run_id=workflow_run_id,
+ organization_id=organization_id,
+ parameter_values=parameter_values,
+ )
+ for credential_id in credential_ids:
try:
db_cred = await app.DATABASE.credentials.get_credential(
credential_id=credential_id, organization_id=organization_id
@@ -2387,7 +2403,10 @@ class WorkflowService:
proxy_session_id: str | None = None
pinned_credential_id: str | None = None
active = await self._resolve_active_credential_pin_for_setup(
- workflow=workflow, parameter_values=parameter_values, organization_id=organization_id
+ workflow=workflow,
+ workflow_run_id=workflow_run.workflow_run_id,
+ organization_id=organization_id,
+ parameter_values=parameter_values,
)
if active:
pinned_credential_id, proxy_session_id = active
@@ -2516,35 +2535,25 @@ class WorkflowService:
self,
*,
workflow: Workflow,
- parameter_values: dict[str, Any],
+ workflow_run_id: str,
organization_id: str,
+ parameter_values: dict[str, Any],
) -> str | None:
- """Setup-time (pre-persist) variant of the login-block credential-profile resolution: reads the
- run's selected credential from the in-memory parameter values (rotation selections keyed by
- parameter key) rather than the DB, so the credential's saved profile can seed the run before
- any browser is created.
+ """Setup-time (pre-persist) variant of the login-block credential-profile resolution: resolves
+ the run's login credential via the same rich path as the mid-run stamp (see
+ _resolve_single_login_credential_ids_for_setup) so the credential's saved profile can seed the
+ run before any browser is created.
Only resolves when a single login block makes the credential unambiguous. With multiple login
blocks (e.g. one per conditional branch) the executing block is unknown at setup, so resolution
is deferred to the mid-run login-block stamp rather than risk seeding the wrong account."""
- # Setup can't know which branch a run takes, so only seed the credential when execution of a
- # single login is guaranteed: no conditional branching anywhere, and exactly one top-level
- # login block. Anything else (branches, nested/multiple logins) defers to the mid-run stamp,
- # which resolves the login that actually runs. This keeps the common linear case working while
- # never booting the wrong account on a branch that skips the login.
- all_blocks = get_all_blocks(workflow.workflow_definition.blocks)
- if any(isinstance(block, ConditionalBlock) for block in all_blocks):
- return None
- # Count over ALL blocks (not just top level): a login nested in a non-conditional container
- # (e.g. a for-loop) still executes, so it must count toward the single-login guarantee or a
- # top-level + nested pair would seed the wrong account instead of deferring to the mid-run stamp.
- login_blocks = [block for block in all_blocks if isinstance(block, LoginBlock) and not block.skip_saved_profile]
- if len(login_blocks) != 1:
- return None
- for param in login_blocks[0].parameters:
- credential_id = _credential_id_from_setup_parameter(param, parameter_values)
- if not credential_id:
- continue
+ credential_ids = await self._resolve_single_login_credential_ids_for_setup(
+ workflow=workflow,
+ workflow_run_id=workflow_run_id,
+ organization_id=organization_id,
+ parameter_values=parameter_values,
+ )
+ for credential_id in credential_ids:
try:
db_cred = await app.DATABASE.credentials.get_credential(
credential_id=credential_id,
@@ -5646,8 +5655,14 @@ class WorkflowService:
workflow_run_id: str | None,
organization_id: str | None = None,
workflow_permanent_id: str | None = None,
+ run_parameter_values: dict[str, Any] | None = None,
) -> list[str]:
- """Return credential ids bound to this block, preserving parameter order."""
+ """Return credential ids bound to this block, preserving parameter order.
+
+ run_parameter_values, when provided, supplies the in-memory render values (rotation selection,
+ dereferenced indirection, or request value) and wins for both credential styles. Setup resolves
+ the seed BEFORE the run context and run parameters are persisted, so the persisted reads below are
+ empty then; mid-run (block execution) passes None and the persisted state is authoritative."""
params = block.parameters
# Pre-fetch run parameters once (used by WorkflowParameter/CREDENTIAL_ID style).
@@ -5659,40 +5674,55 @@ class WorkflowService:
# Style 1: CredentialParameter (has credential_id directly)
if isinstance(param, CredentialParameter):
- credential_id = await self._resolve_credential_parameter_id(
- parameter=param,
- workflow_run_id=workflow_run_id,
- organization_id=organization_id,
- workflow_permanent_id=workflow_permanent_id,
- )
+ # In-memory render values win when provided (setup): they already carry the rotation
+ # selection and any dereferenced indirection, which the persisted-state resolver below
+ # can't see until the run context / selections are persisted.
+ if run_parameter_values is not None:
+ selected = run_parameter_values.get(param.key)
+ if isinstance(selected, str) and selected:
+ credential_id = selected
+ if not credential_id:
+ credential_id = await self._resolve_credential_parameter_id(
+ parameter=param,
+ workflow_run_id=workflow_run_id,
+ organization_id=organization_id,
+ workflow_permanent_id=workflow_permanent_id,
+ )
# Style 2: WorkflowParameter with type CREDENTIAL_ID
elif (
isinstance(param, WorkflowParameter)
and getattr(param, "workflow_parameter_type", None) == WorkflowParameterType.CREDENTIAL_ID
):
- # The credential_id is stored as the run-parameter value (or
- # falls back to default_value on the workflow parameter).
- if workflow_run_id is None:
- run_param_tuples = []
- elif run_param_tuples is None:
- try:
- run_param_tuples = await app.DATABASE.workflow_runs.get_workflow_run_parameters(
- workflow_run_id=workflow_run_id,
- )
- except Exception:
- LOG.warning(
- "Failed to fetch workflow run parameters for credential resolution",
- workflow_run_id=workflow_run_id,
- exc_info=True,
- )
+ # In-memory render values win when provided: setup resolves the seed before run
+ # parameters are persisted, so the DB read below is empty at that point.
+ if run_parameter_values is not None:
+ selected = run_parameter_values.get(param.key)
+ if isinstance(selected, str) and selected:
+ credential_id = selected
+
+ # Otherwise the credential_id is the persisted run-parameter value.
+ if not credential_id:
+ if workflow_run_id is None:
run_param_tuples = []
+ elif run_param_tuples is None:
+ try:
+ run_param_tuples = await app.DATABASE.workflow_runs.get_workflow_run_parameters(
+ workflow_run_id=workflow_run_id,
+ )
+ except Exception:
+ LOG.warning(
+ "Failed to fetch workflow run parameters for credential resolution",
+ workflow_run_id=workflow_run_id,
+ exc_info=True,
+ )
+ run_param_tuples = []
- for wf_param, run_param in run_param_tuples:
- if wf_param.key == param.key:
- if isinstance(run_param.value, str) and run_param.value:
- credential_id = run_param.value
- break
+ for wf_param, run_param in run_param_tuples:
+ if wf_param.key == param.key:
+ if isinstance(run_param.value, str) and run_param.value:
+ credential_id = run_param.value
+ break
# Fallback to default_value
if not credential_id:
diff --git a/tests/unit/test_seed_precedence_engine.py b/tests/unit/test_seed_precedence_engine.py
index fc6a5b3ac..27fe40448 100644
--- a/tests/unit/test_seed_precedence_engine.py
+++ b/tests/unit/test_seed_precedence_engine.py
@@ -9,8 +9,13 @@ import pytest
from skyvern.forge import app
from skyvern.forge.sdk.db.enums import BrowserSeedSource
from skyvern.forge.sdk.workflow.models.block import LoginBlock
-from skyvern.forge.sdk.workflow.models.parameter import CredentialParameter, OutputParameter
-from skyvern.forge.sdk.workflow.service import WorkflowService, _credential_id_from_setup_parameter
+from skyvern.forge.sdk.workflow.models.parameter import (
+ CredentialParameter,
+ OutputParameter,
+ WorkflowParameter,
+ WorkflowParameterType,
+)
+from skyvern.forge.sdk.workflow.service import WorkflowService
from skyvern.schemas.runs import ProxyLocation
from skyvern.services.workflow_service import workflow_request_body_from_existing_run
@@ -705,10 +710,15 @@ async def test_maybe_pin_active_credential_applies_without_seed(monkeypatch: pyt
assert update_run.await_args.kwargs["extra_http_headers"] == {"x-sky-proxy": "ps_active"}
-# --- credential id extraction (rotation-aware) -------------------------------
+# --- credential parameter builder (shared) -----------------------------------
-def _credential_parameter(key: str, credential_id: str, credential_ids: list[str] | None = None) -> CredentialParameter:
+def _credential_parameter(
+ key: str,
+ credential_id: str,
+ credential_ids: list[str] | None = None,
+ fallback_credential_ids: list[str] | None = None,
+) -> CredentialParameter:
now = datetime.now(timezone.utc)
return CredentialParameter(
key=key,
@@ -716,26 +726,12 @@ def _credential_parameter(key: str, credential_id: str, credential_ids: list[str
workflow_id="wf_test",
credential_id=credential_id,
credential_ids=credential_ids or [],
+ fallback_credential_ids=fallback_credential_ids or [],
created_at=now,
modified_at=now,
)
-def test_credential_id_prefers_run_selection_over_static() -> None:
- # Rotation: the per-run selected credential (in parameter_values, keyed by parameter key) wins.
- param = _credential_parameter("login", credential_id="cred_static", credential_ids=["cred_a", "cred_b"])
- assert _credential_id_from_setup_parameter(param, {"login": "cred_b"}) == "cred_b"
-
-
-def test_credential_id_falls_back_to_static_credential_id() -> None:
- param = _credential_parameter("login", credential_id="cred_static")
- assert _credential_id_from_setup_parameter(param, {}) == "cred_static"
-
-
-def test_credential_id_none_for_unrelated_parameter() -> None:
- assert _credential_id_from_setup_parameter(SimpleNamespace(key="not_a_cred"), {}) is None
-
-
# --- retry re-resolves instead of pinning ------------------------------------
@@ -812,7 +808,7 @@ def _output_parameter(key: str) -> OutputParameter:
return OutputParameter(output_parameter_id=f"{key}_id", key=key, workflow_id="wf", created_at=now, modified_at=now)
-def _login_block(label: str, credential_param: CredentialParameter) -> LoginBlock:
+def _login_block(label: str, credential_param: CredentialParameter | WorkflowParameter) -> LoginBlock:
return LoginBlock(
url="https://example.com/login",
label=label,
@@ -823,6 +819,19 @@ def _login_block(label: str, credential_param: CredentialParameter) -> LoginBloc
)
+def _workflow_credential_parameter(key: str, default_value: str | None) -> WorkflowParameter:
+ now = datetime.now(timezone.utc)
+ return WorkflowParameter(
+ key=key,
+ workflow_parameter_id=f"wp_{key}",
+ workflow_parameter_type=WorkflowParameterType.CREDENTIAL_ID,
+ workflow_id="wf_test",
+ default_value=default_value,
+ created_at=now,
+ modified_at=now,
+ )
+
+
def _workflow_with_blocks(*blocks: LoginBlock) -> SimpleNamespace:
return SimpleNamespace(
workflow_permanent_id="wpid_test",
@@ -832,6 +841,7 @@ def _workflow_with_blocks(*blocks: LoginBlock) -> SimpleNamespace:
@pytest.mark.asyncio
async def test_setup_credential_resolves_single_login_block(monkeypatch: pytest.MonkeyPatch) -> None:
+ monkeypatch.setattr(app.WORKFLOW_CONTEXT_MANAGER, "workflow_run_contexts", {}) # no run context at setup
monkeypatch.setattr(
app.DATABASE.credentials,
"get_credential",
@@ -846,8 +856,9 @@ async def test_setup_credential_resolves_single_login_block(monkeypatch: pytest.
result = await WorkflowService()._resolve_credential_browser_profile_id_for_setup(
workflow=workflow, # type: ignore[arg-type]
- parameter_values={},
+ workflow_run_id="wr_test",
organization_id="o_test",
+ parameter_values={},
)
assert result == "bp_cred"
@@ -864,8 +875,9 @@ async def test_setup_credential_defers_when_multiple_login_blocks(monkeypatch: p
result = await WorkflowService()._resolve_credential_browser_profile_id_for_setup(
workflow=workflow, # type: ignore[arg-type]
- parameter_values={},
+ workflow_run_id="wr_test",
organization_id="o_test",
+ parameter_values={},
)
# Ambiguous which login block executes -> defer to the mid-run stamp, don't even look one up.
@@ -875,6 +887,7 @@ async def test_setup_credential_defers_when_multiple_login_blocks(monkeypatch: p
@pytest.mark.asyncio
async def test_setup_credential_best_effort_on_profile_lookup_failure(monkeypatch: pytest.MonkeyPatch) -> None:
+ monkeypatch.setattr(app.WORKFLOW_CONTEXT_MANAGER, "workflow_run_contexts", {}) # no run context at setup
monkeypatch.setattr(
app.DATABASE.credentials,
"get_credential",
@@ -890,13 +903,232 @@ async def test_setup_credential_best_effort_on_profile_lookup_failure(monkeypatc
# A transient failure degrades to a fresh seed instead of failing setup.
result = await WorkflowService()._resolve_credential_browser_profile_id_for_setup(
workflow=workflow, # type: ignore[arg-type]
- parameter_values={},
+ workflow_run_id="wr_test",
organization_id="o_test",
+ parameter_values={},
)
assert result is None
+async def test_setup_credential_resolves_pool_credential(monkeypatch: pytest.MonkeyPatch) -> None:
+ # The defect surface: a pool credential (credential_ids) is invisible to the old in-memory setup
+ # extractor, so setup seeded fresh while the block resolved the pool selection. That split brain only
+ # bites when a cached-script / pre-navigating block opens the browser before the login block, locking
+ # the mid-run loader out (get_or_create returns the already-open context) — leaving setup as the only
+ # loader that can seed. Setup must resolve the pool selection via the SAME rich path as the block.
+ monkeypatch.setattr(app.WORKFLOW_CONTEXT_MANAGER, "workflow_run_contexts", {})
+ # The run's rotation selection is already persisted (select_credential_for_run is idempotent per run).
+ monkeypatch.setattr(
+ app.DATABASE.workflow_run_credential_selections,
+ "get_selection",
+ AsyncMock(return_value="cred_pool"),
+ )
+ get_credential = AsyncMock(return_value=SimpleNamespace(browser_profile_id="bp_pool"))
+ monkeypatch.setattr(app.DATABASE.credentials, "get_credential", get_credential)
+ monkeypatch.setattr(
+ app.DATABASE.browser_sessions,
+ "get_browser_profile",
+ AsyncMock(return_value=SimpleNamespace(browser_profile_id="bp_pool")),
+ )
+ workflow = _workflow_with_blocks(
+ _login_block("login", _credential_parameter("login", "cred_primary", credential_ids=["cred_a", "cred_pool"]))
+ )
+
+ result = await WorkflowService()._resolve_credential_browser_profile_id_for_setup(
+ workflow=workflow, # type: ignore[arg-type]
+ workflow_run_id="wr_test",
+ organization_id="o_test",
+ parameter_values={},
+ )
+
+ assert result == "bp_pool"
+ assert get_credential.await_args.kwargs["credential_id"] == "cred_pool" # pool selection, not the static id
+
+
+async def test_setup_credential_resolves_fallback_db_selection(monkeypatch: pytest.MonkeyPatch) -> None:
+ # A fallback retry persists a DB credential selection that differs from the parameter's static id.
+ # The old in-memory extractor returned the static (primary) id, seeding the wrong account's profile;
+ # the block resolved the DB selection. Setup must read the same DB selection.
+ monkeypatch.setattr(app.WORKFLOW_CONTEXT_MANAGER, "workflow_run_contexts", {})
+ monkeypatch.setattr(
+ app.DATABASE.workflow_run_credential_selections,
+ "get_selection",
+ AsyncMock(return_value="cred_fallback"),
+ )
+ get_credential = AsyncMock(return_value=SimpleNamespace(browser_profile_id="bp_fallback"))
+ monkeypatch.setattr(app.DATABASE.credentials, "get_credential", get_credential)
+ monkeypatch.setattr(
+ app.DATABASE.browser_sessions,
+ "get_browser_profile",
+ AsyncMock(return_value=SimpleNamespace(browser_profile_id="bp_fallback")),
+ )
+ workflow = _workflow_with_blocks(
+ _login_block("login", _credential_parameter("login", "cred_primary", fallback_credential_ids=["cred_fallback"]))
+ )
+
+ result = await WorkflowService()._resolve_credential_browser_profile_id_for_setup(
+ workflow=workflow, # type: ignore[arg-type]
+ workflow_run_id="wr_test",
+ organization_id="o_test",
+ parameter_values={},
+ )
+
+ assert result == "bp_fallback"
+ assert get_credential.await_args.kwargs["credential_id"] == "cred_fallback" # DB selection wins over static
+
+
+async def test_setup_credential_uses_same_rich_resolver_as_block(monkeypatch: pytest.MonkeyPatch) -> None:
+ # Regression pin: setup resolves the single login block's credential through the identical resolver
+ # the mid-run stamp uses (_resolve_login_block_credential_ids), so the two can never disagree.
+ svc = WorkflowService()
+ rich = AsyncMock(return_value=["cred_rich"])
+ monkeypatch.setattr(svc, "_resolve_login_block_credential_ids", rich)
+ monkeypatch.setattr(
+ app.DATABASE.credentials,
+ "get_credential",
+ AsyncMock(return_value=SimpleNamespace(browser_profile_id="bp_rich")),
+ )
+ monkeypatch.setattr(
+ app.DATABASE.browser_sessions,
+ "get_browser_profile",
+ AsyncMock(return_value=SimpleNamespace(browser_profile_id="bp_rich")),
+ )
+ workflow = _workflow_with_blocks(_login_block("login", _credential_parameter("login", "cred_static")))
+
+ result = await svc._resolve_credential_browser_profile_id_for_setup(
+ workflow=workflow, # type: ignore[arg-type]
+ workflow_run_id="wr_test",
+ organization_id="o_test",
+ parameter_values={},
+ )
+
+ assert result == "bp_rich"
+ rich.assert_awaited_once()
+ assert rich.await_args.args[1:] == ("wr_test", "o_test", "wpid_test")
+
+
+async def test_setup_pin_resolves_pool_credential(monkeypatch: pytest.MonkeyPatch) -> None:
+ # The sibling pin resolver had the identical gap: a pool/DB-selected credential was invisible, so a
+ # pinning credential's dedicated IP was not applied at setup. It now resolves via the same rich path.
+ monkeypatch.setattr(app.WORKFLOW_CONTEXT_MANAGER, "workflow_run_contexts", {})
+ monkeypatch.setattr(
+ app.DATABASE.workflow_run_credential_selections,
+ "get_selection",
+ AsyncMock(return_value="cred_pool"),
+ )
+ monkeypatch.setattr(
+ app.DATABASE.credentials,
+ "get_credential",
+ AsyncMock(
+ return_value=SimpleNamespace(
+ credential_id="cred_pool", pin_saved_session_ip=True, proxy_session_id="ps_pool"
+ )
+ ),
+ )
+ workflow = _workflow_with_blocks(
+ _login_block("login", _credential_parameter("login", "cred_primary", credential_ids=["cred_a", "cred_pool"]))
+ )
+
+ result = await WorkflowService()._resolve_active_credential_pin_for_setup(
+ workflow=workflow, # type: ignore[arg-type]
+ workflow_run_id="wr_test",
+ organization_id="o_test",
+ parameter_values={},
+ )
+
+ assert result == ("cred_pool", "ps_pool")
+
+
+async def test_setup_credential_prefers_request_value_over_default_for_workflow_parameter(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ # Setup runs before run parameters are persisted, so a request-supplied WorkflowParameter/CREDENTIAL_ID
+ # value lives only in the in-memory render params. Seeding must use it, not the parameter default, or a
+ # cached-script run seeds the wrong account.
+ monkeypatch.setattr(app.WORKFLOW_CONTEXT_MANAGER, "workflow_run_contexts", {})
+ get_credential = AsyncMock(return_value=SimpleNamespace(browser_profile_id="bp_request"))
+ monkeypatch.setattr(app.DATABASE.credentials, "get_credential", get_credential)
+ monkeypatch.setattr(
+ app.DATABASE.browser_sessions,
+ "get_browser_profile",
+ AsyncMock(return_value=SimpleNamespace(browser_profile_id="bp_request")),
+ )
+ workflow = _workflow_with_blocks(
+ _login_block("login", _workflow_credential_parameter("login_wp", "cred_default_a"))
+ )
+
+ result = await WorkflowService()._resolve_credential_browser_profile_id_for_setup(
+ workflow=workflow, # type: ignore[arg-type]
+ workflow_run_id="wr_test",
+ organization_id="o_test",
+ parameter_values={"login_wp": "cred_request_b"},
+ )
+
+ assert result == "bp_request"
+ assert get_credential.await_args.kwargs["credential_id"] == "cred_request_b" # request value, not the default
+
+
+async def test_setup_credential_resolves_dereferenced_binding_for_fallback_parameter(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ # A fallback-configured CredentialParameter whose credential_id indirectly references another parameter
+ # is dereferenced by the render pipeline into the in-memory params. With no persisted fallback selection
+ # yet, the rich resolver would return the raw reference; setup must use the dereferenced value.
+ monkeypatch.setattr(app.WORKFLOW_CONTEXT_MANAGER, "workflow_run_contexts", {})
+ monkeypatch.setattr(app.DATABASE.workflow_run_credential_selections, "get_selection", AsyncMock(return_value=None))
+ get_credential = AsyncMock(return_value=SimpleNamespace(browser_profile_id="bp_deref"))
+ monkeypatch.setattr(app.DATABASE.credentials, "get_credential", get_credential)
+ monkeypatch.setattr(
+ app.DATABASE.browser_sessions,
+ "get_browser_profile",
+ AsyncMock(return_value=SimpleNamespace(browser_profile_id="bp_deref")),
+ )
+ workflow = _workflow_with_blocks(
+ _login_block("login", _credential_parameter("login_cred", "raw_reference", fallback_credential_ids=["fb"]))
+ )
+
+ result = await WorkflowService()._resolve_credential_browser_profile_id_for_setup(
+ workflow=workflow, # type: ignore[arg-type]
+ workflow_run_id="wr_test",
+ organization_id="o_test",
+ parameter_values={"login_cred": "cred_deref_b"},
+ )
+
+ assert result == "bp_deref"
+ assert get_credential.await_args.kwargs["credential_id"] == "cred_deref_b" # dereferenced, not "raw_reference"
+
+
+async def test_setup_pin_prefers_request_value_over_default(monkeypatch: pytest.MonkeyPatch) -> None:
+ # The pin resolver shares the setup credential resolution, so a request-supplied credential's pin must
+ # win over the parameter default's pin.
+ monkeypatch.setattr(app.WORKFLOW_CONTEXT_MANAGER, "workflow_run_contexts", {})
+ get_credential = AsyncMock(
+ return_value=SimpleNamespace(credential_id="cred_b", pin_saved_session_ip=True, proxy_session_id="ps_b")
+ )
+ monkeypatch.setattr(app.DATABASE.credentials, "get_credential", get_credential)
+ workflow = _workflow_with_blocks(
+ _login_block("login", _workflow_credential_parameter("login_wp", "cred_default_a"))
+ )
+
+ result = await WorkflowService()._resolve_active_credential_pin_for_setup(
+ workflow=workflow, # type: ignore[arg-type]
+ workflow_run_id="wr_test",
+ organization_id="o_test",
+ parameter_values={"login_wp": "cred_b"},
+ )
+
+ assert result == ("cred_b", "ps_b")
+ assert get_credential.await_args.kwargs["credential_id"] == "cred_b" # request value, not the default
+
+
@pytest.mark.asyncio
async def test_login_block_cached_browser_degrades_instead_of_credential_seeding(
monkeypatch: pytest.MonkeyPatch,