Skip to content

Automate browser based workflows with AI

Let Copilot use credentials saved without a login URL (SKY-12959) (#7711)

Andrew Neilson808ef71c9872
diff --git a/skyvern/forge/sdk/copilot/request_policy.py b/skyvern/forge/sdk/copilot/request_policy.py
index dd44f85e4..5233f6ee1 100644
--- a/skyvern/forge/sdk/copilot/request_policy.py
+++ b/skyvern/forge/sdk/copilot/request_policy.py
@@ -4179,7 +4179,10 @@ async def _apply_exact_credential_name_scope(
if (
policy.credential_input_kind == "website_stored_credential"
and policy.login_page_urls
- and _match_by_url(credentials, policy.login_page_urls)
+ and _match_by_url(
+ [credential for credential in credentials if credential.credential_type == CredentialType.PASSWORD],
+ policy.login_page_urls,
+ )
):
return
matched_names = _clean_list(
@@ -4604,10 +4607,32 @@ async def _resolve_credentials(
return
return
- matches = _match_by_url(credentials, policy.login_page_urls)
+ login_credentials = [
+ credential for credential in credentials if credential.credential_type == CredentialType.PASSWORD
+ ]
+ matches = _match_by_url(login_credentials, policy.login_page_urls)
+ if not matches:
+ # A credential saved without a tested_url is still a valid login credential,
+ # so it stays a candidate once both URL tiers come up empty.
+ urlless = [credential for credential in login_credentials if not credential.tested_url]
+ if len(urlless) == 1:
+ policy.resolved_credentials = urlless
+ return
+ if urlless:
+ # Found-but-unbound keeps the unresolved-login override from
+ # relabeling this disambiguation ask as a missing-credential turn.
+ policy.discovered_credentials = urlless
+ _block(
+ policy,
+ _AMBIGUOUS_URL_CREDENTIAL_QUESTION,
+ urlless,
+ reason="credential_name_unresolved",
+ )
+ return
if len(matches) == 1:
policy.resolved_credentials = matches
elif matches:
+ policy.discovered_credentials = matches
_block(
policy,
_AMBIGUOUS_URL_CREDENTIAL_QUESTION,
diff --git a/tests/unit/test_copilot_agent_helpers.py b/tests/unit/test_copilot_agent_helpers.py
index d2cafb12a..af053f742 100644
--- a/tests/unit/test_copilot_agent_helpers.py
+++ b/tests/unit/test_copilot_agent_helpers.py
@@ -124,6 +124,7 @@ from skyvern.forge.sdk.copilot.turn_origin import TurnOrigin
from skyvern.forge.sdk.copilot.verification_evidence import WorkflowVerificationEvidence
from skyvern.forge.sdk.routes.workflow_copilot import CHAT_HISTORY_CONTEXT_MESSAGES
from skyvern.forge.sdk.schemas.copilot_turn_outcome import ResponseKind, TurnOutcome
+from skyvern.forge.sdk.schemas.credentials import CredentialType
from skyvern.forge.sdk.schemas.workflow_copilot import (
WorkflowCopilotChatHistoryMessage,
WorkflowCopilotChatSender,
@@ -4802,7 +4803,12 @@ workflow_definition:
from skyvern.forge.sdk.copilot import request_policy as policy_module
from skyvern.forge.sdk.copilot.request_policy import build_request_policy
- credential = SimpleNamespace(credential_id="cred_bank", name="Bank", tested_url="https://bank.example/login")
+ credential = SimpleNamespace(
+ credential_id="cred_bank",
+ name="Bank",
+ tested_url="https://bank.example/login",
+ credential_type=CredentialType.PASSWORD,
+ )
credentials = SimpleNamespace(get_credentials=AsyncMock(return_value=[credential]))
monkeypatch.setattr(policy_module.app, "DATABASE", SimpleNamespace(credentials=credentials))
@@ -5453,7 +5459,12 @@ workflow_definition:
from skyvern.forge.sdk.copilot import request_policy as policy_module
from skyvern.forge.sdk.copilot.request_policy import build_request_policy
- bank = SimpleNamespace(credential_id="cred_bank", name="Bank", tested_url="https://bank.example/login")
+ bank = SimpleNamespace(
+ credential_id="cred_bank",
+ name="Bank",
+ tested_url="https://bank.example/login",
+ credential_type=CredentialType.PASSWORD,
+ )
monkeypatch.setattr(
policy_module.app,
"DATABASE",
diff --git a/tests/unit/test_copilot_request_policy_workflow_credentials.py b/tests/unit/test_copilot_request_policy_workflow_credentials.py
index d484ccc91..575dbda8a 100644
--- a/tests/unit/test_copilot_request_policy_workflow_credentials.py
+++ b/tests/unit/test_copilot_request_policy_workflow_credentials.py
@@ -13,6 +13,7 @@ from skyvern.forge.sdk.copilot.context import (
record_approved_credentials_in_global_llm_context,
)
from skyvern.forge.sdk.copilot.request_policy import (
+ _AMBIGUOUS_URL_CREDENTIAL_QUESTION,
_LOGIN_CREDENTIAL_QUESTION,
CREDENTIAL_DEFERRED_DRAFT_REASONS,
CREDENTIAL_PROMPT_CLARIFICATION_REASONS,
@@ -545,6 +546,161 @@ async def test_success_website_kind_with_matching_url_keeps_url_credential() ->
assert [c.credential_id for c in policy.resolved_credentials] == ["cred_url"]
+async def test_website_kind_resolves_sole_url_less_credential() -> None:
+ policy = await _build_with_forced_classifier(
+ user_message="Log into the portal at https://portal.example.com/login.",
+ classifier_policy=RequestPolicy(
+ credential_input_kind="website_stored_credential",
+ login_page_urls=["https://portal.example.com/login"],
+ login_intent=True,
+ classifier_status="success",
+ ),
+ org_credentials=[_cred("saved-login", "cred_urlless")],
+ )
+
+ assert [c.credential_id for c in policy.resolved_credentials] == ["cred_urlless"]
+ assert policy.clarification_question is None
+ assert policy.clarification_reason != "credential_name_unresolved"
+
+
+async def test_website_kind_url_match_wins_over_url_less_credential() -> None:
+ policy = await _build_with_forced_classifier(
+ user_message="Log into the portal at https://portal.example.com/login.",
+ classifier_policy=RequestPolicy(
+ credential_input_kind="website_stored_credential",
+ login_page_urls=["https://portal.example.com/login"],
+ login_intent=True,
+ classifier_status="success",
+ ),
+ org_credentials=[
+ _cred("portal-cred", "cred_url", tested_url="https://portal.example.com/login"),
+ _cred("saved-login", "cred_urlless"),
+ ],
+ )
+
+ assert [c.credential_id for c in policy.resolved_credentials] == ["cred_url"]
+ assert policy.clarification_question is None
+
+
+async def test_website_kind_multiple_url_less_credentials_ask_which_one() -> None:
+ policy = await _build_with_forced_classifier(
+ user_message="Log into the portal at https://portal.example.com/login.",
+ classifier_policy=RequestPolicy(
+ credential_input_kind="website_stored_credential",
+ login_page_urls=["https://portal.example.com/login"],
+ login_intent=True,
+ classifier_status="success",
+ ),
+ org_credentials=[_cred("first-login", "cred_one"), _cred("second-login", "cred_two")],
+ )
+
+ assert policy.requires_user_clarification is True
+ assert policy.clarification_question is not None
+ assert policy.clarification_question.startswith(_AMBIGUOUS_URL_CREDENTIAL_QUESTION)
+ assert "first-login" in policy.clarification_question
+ assert "second-login" in policy.clarification_question
+ assert policy.clarification_reason == "credential_name_unresolved"
+ assert sorted(c.credential_id for c in policy.discovered_credentials) == ["cred_one", "cred_two"]
+ assert not policy.resolved_credentials
+
+
+async def test_website_kind_multiple_url_matches_keep_disambiguation_reason() -> None:
+ policy = await _build_with_forced_classifier(
+ user_message="Log into the portal at https://portal.example.com/login.",
+ classifier_policy=RequestPolicy(
+ credential_input_kind="website_stored_credential",
+ login_page_urls=["https://portal.example.com/login"],
+ login_intent=True,
+ classifier_status="success",
+ ),
+ org_credentials=[
+ _cred("first-login", "cred_one", tested_url="https://portal.example.com/login"),
+ _cred("second-login", "cred_two", tested_url="https://portal.example.com/login"),
+ ],
+ )
+
+ assert policy.clarification_question is not None
+ assert policy.clarification_question.startswith(_AMBIGUOUS_URL_CREDENTIAL_QUESTION)
+ assert policy.clarification_reason == "credential_name_unresolved"
+ assert sorted(c.credential_id for c in policy.discovered_credentials) == ["cred_one", "cred_two"]
+
+
+ "org_credentials",
+ [
+ pytest.param([], id="no_credentials"),
+ pytest.param(
+ [_cred("saved-card", "cred_card", credential_type=CredentialType.CREDIT_CARD)],
+ id="url_less_card_only",
+ ),
+ pytest.param(
+ [
+ _cred(
+ "saved-card",
+ "cred_card",
+ tested_url="https://portal.example.com/login",
+ credential_type=CredentialType.CREDIT_CARD,
+ )
+ ],
+ id="url_matched_card_only",
+ ),
+ ],
+)
+async def test_website_kind_without_login_credentials_still_blocks(
+ org_credentials: list[SimpleNamespace],
+) -> None:
+ policy = await _build_with_forced_classifier(
+ user_message="Log into the portal at https://portal.example.com/login.",
+ classifier_policy=RequestPolicy(
+ credential_input_kind="website_stored_credential",
+ login_page_urls=["https://portal.example.com/login"],
+ login_intent=True,
+ classifier_status="success",
+ ),
+ org_credentials=org_credentials,
+ )
+
+ assert policy.requires_user_clarification is True
+ assert policy.clarification_question is not None
+ assert "could not find a stored credential" in policy.clarification_question
+ assert not policy.resolved_credentials
+
+
+async def test_website_kind_explicit_name_wins_over_url_matched_card() -> None:
+ policy = await _build_with_forced_classifier(
+ user_message=(
+ "Log into the portal at https://portal.example.com/login with the credential named analytics-login."
+ ),
+ classifier_policy=RequestPolicy(
+ credential_input_kind="website_stored_credential",
+ login_page_urls=["https://portal.example.com/login"],
+ login_intent=True,
+ classifier_status="success",
+ ),
+ org_credentials=[
+ _cred(
+ "saved-card",
+ "cred_card",
+ tested_url="https://portal.example.com/login",
+ credential_type=CredentialType.CREDIT_CARD,
+ ),
+ _cred("analytics-login", "cred_named", tested_url="https://other.example.com/login"),
+ ],
+ )
+
+ assert policy.credential_input_kind == "credential_name"
+ assert policy.credential_refs == ["analytics-login"]
+ assert [c.credential_id for c in policy.resolved_credentials] == ["cred_named"]
+ assert policy.requires_user_clarification is False
+ assert policy.clarification_question is None
+
+
@pytest.mark.asyncio
async def test_success_credential_id_intent_is_not_overridden_by_name_scan() -> None:
by_ids = AsyncMock(return_value=[_cred("real", "cred_real")])