Skip to content

Automate browser based workflows with AI

Bind the URL-matched saved credential when the agent observes a login page (SKY-13293) (#7966)

Andrew Neilson0b9190efdaa5
diff --git a/skyvern/forge/sdk/copilot/agent.py b/skyvern/forge/sdk/copilot/agent.py
index f53215797..a7fa24b93 100644
--- a/skyvern/forge/sdk/copilot/agent.py
+++ b/skyvern/forge/sdk/copilot/agent.py
@@ -4505,6 +4505,25 @@ def _output_policy_diagnostics_from_guardrail_exception(exc: BaseException) -> d
return {key: data[key] for key in keys if key in data}
+def _unapproved_credential_reference_reply(request_policy: RequestPolicy | None) -> str:
+ resolution = request_policy.live_page_resolution if request_policy is not None else None
+ if resolution is not None and resolution.verdict == "ambiguous" and resolution.candidates:
+ listed = "; ".join(f"{candidate.name} (`{candidate.credential_id}`)" for candidate in resolution.candidates)
+ # The Credentials UI marker has to survive on this branch too: credential_prompt_reason keys
+ # the FE credential card off it, so dropping it silently removes the card for ambiguous asks.
+ return (
+ f"More than one saved credential is set up for the sign-in page I reached: {listed}. "
+ "Reply with the credential ID you want me to use, manage them in the Credentials UI, "
+ "or adjust the workflow to avoid using credentials."
+ )
+ # Only the observation seam records a verdict, so the absence of one says nothing about whether a
+ # credential matched — claiming "no match" here would deny a real match found at the fill seam.
+ return (
+ "I need an approved credential to continue. Reply with the credential ID to use, "
+ "add one in the Credentials UI, or adjust the workflow to avoid using credentials."
+ )
+
+
def _build_output_policy_blocked_result(
ctx: CopilotContext,
verdict: OutputPolicyVerdict,
@@ -4545,11 +4564,7 @@ def _build_output_policy_blocked_result(
user_response = _RAW_SECRET_LEAK_REFUSAL
add_saved_draft_copy = True
elif OutputPolicyReason.UNAPPROVED_CREDENTIAL_REFERENCE in verdict.reason_codes:
- user_response = (
- "I need you to confirm which saved credential should be used before I can continue. "
- "Please reply with the credential name from the Credentials UI, or adjust the workflow to avoid "
- "using credentials."
- )
+ user_response = _unapproved_credential_reference_reply(request_policy)
add_saved_draft_copy = True
elif OutputPolicyReason.CREDENTIAL_SCOPE_BROADENED in verdict.reason_codes:
user_response = (
diff --git a/skyvern/forge/sdk/copilot/request_policy.py b/skyvern/forge/sdk/copilot/request_policy.py
index 28372cc46..92e9fd159 100644
--- a/skyvern/forge/sdk/copilot/request_policy.py
+++ b/skyvern/forge/sdk/copilot/request_policy.py
@@ -6,7 +6,7 @@ import hashlib
import json
import re
import time
-from collections.abc import Awaitable, Callable
+from collections.abc import Awaitable, Callable, Sequence
from dataclasses import dataclass, field, replace
from typing import Any, Literal, cast, get_args
@@ -23,6 +23,10 @@ from skyvern.forge.sdk.copilot.context import (
prior_signin_email_from_context,
sanitize_global_llm_context_for_prompt,
)
+from skyvern.forge.sdk.copilot.credential_resolution import (
+ CredentialResolution,
+ CredentialResolutionTier,
+)
from skyvern.forge.sdk.copilot.credential_resolution import deduplicate_credentials as _deduplicate_credentials
from skyvern.forge.sdk.copilot.credential_resolution import load_credentials as _load_credentials
from skyvern.forge.sdk.copilot.credential_resolution import (
@@ -552,6 +556,20 @@ class RequestSlotAnchorCorrectionDecisionV1:
accepted_payload: dict[str, Any] | None = None
+LiveCredentialSeam = Literal["fill", "page_observation"]
+LivePageResolutionVerdict = Literal["resolved", "ambiguous", "no_match", "declined"]
+
+
+@dataclass(frozen=True)
+class LivePageResolutionRecord:
+ verdict: LivePageResolutionVerdict
+ tier: CredentialResolutionTier | None = None
+ candidates: tuple[Credential, ...] = ()
+ # The page the verdict is about. Without it a later observation's record is rendered as an answer
+ # about whichever page the reply happens to mention.
+ page_url: str = ""
+
+
@dataclass
class RequestPolicy:
testing_intent: str = "unspecified"
@@ -581,6 +599,8 @@ class RequestPolicy:
# credential_id -> the login page URL that granted it, so each later fill can confirm the
# browser has not left that site and a redirect cannot inherit the grant.
live_page_admitted_urls: dict[str, str] = field(default_factory=dict)
+ live_page_resolution: LivePageResolutionRecord | None = None
+ live_page_logged_urls: set[str] = field(default_factory=set)
# Approves persisting a bound credential, not running it: run authority stays
# scoped to resolved_credentials (ADR 0002).
discovered_credentials: list[Credential] = field(default_factory=list)
@@ -5023,36 +5043,51 @@ class LiveCredentialAdmission:
page_url: str | None = None
-async def admit_credential_for_live_page(
+def _live_page_log_allowed(policy: RequestPolicy, page_url: str, seam: LiveCredentialSeam, outcome: str = "") -> bool:
+ """Whether this seam may emit the admission fingerprint for this page.
+
+ The observation seam runs on every page the scout reaches, so without a per-URL guard a
+ single turn's no-match spam would drown the admission signal it shares a name with. The key
+ carries the outcome so a declining observation cannot eat the token before the grant that
+ follows it on the same URL — the grant is the signal the guard exists to protect.
+ """
+ if seam != "page_observation":
+ return True
+ key = f"{page_url}::{outcome}"
+ if key in policy.live_page_logged_urls:
+ return False
+ policy.live_page_logged_urls.add(key)
+ return True
+
+
+async def _resolve_live_page_credentials(
policy: RequestPolicy,
*,
organization_id: str,
- credential_id: str,
page_url: str,
- load_org_credentials: Callable[[], Awaitable[list[Credential]]] | None = None,
-) -> LiveCredentialAdmission:
- """Grant run authority for the credential whose saved login URL matches the page the scout reached.
-
- The discriminator is that server-computed match, never the model's claim: a credential the page
- does not vouch for is refused, and so is one saved without a login URL, which is evidence about
- no page at all.
- """
- if policy.raw_secret_detected or not policy.allow_run_blocks or not credential_id or not page_url:
- return LiveCredentialAdmission(False)
+ load_org_credentials: Callable[[], Awaitable[list[Credential]]] | None,
+ seam: LiveCredentialSeam,
+ credential_id: str | None = None,
+) -> CredentialResolution | None:
+ """Ask the live page which saved credentials it vouches for, or None when a precondition declines."""
+ if policy.raw_secret_detected or not policy.allow_run_blocks or not page_url:
+ return None
if _is_passwordless_email_signin(policy):
# Turn-start resolution excludes this shape from every URL tier because a site that signs in
# by emailed link has no password question to put to the user, so admitting one here would
# fill a password the user said the account does not have.
- LOG.info(
- "copilot credential live-page admission",
- outcome="passwordless_declined",
- organization_id=organization_id,
- credential_id=credential_id,
- page_url=loggable_origin(page_url),
- )
- return LiveCredentialAdmission(False)
+ if _live_page_log_allowed(policy, page_url, seam, "passwordless_declined"):
+ LOG.info(
+ "copilot credential live-page admission",
+ outcome="passwordless_declined",
+ seam=seam,
+ organization_id=organization_id,
+ credential_id=credential_id,
+ page_url=loggable_origin(page_url),
+ )
+ return None
- resolution = resolve_by_url(
+ return resolve_by_url(
await (load_org_credentials() if load_org_credentials else _load_credentials(organization_id)),
[page_url],
# The scout reached this page, so its own path is the evidence; a bare host match would let
@@ -5060,6 +5095,105 @@ async def admit_credential_for_live_page(
tiers=("url_exact", "url_path"),
password_only=True,
)
+
+
+def _record_live_page_admission(policy: RequestPolicy, candidates: Sequence[Credential], page_url: str) -> None:
+ # Only a credential this page is what granted gets the page-scoped stamp. One the turn already
+ # resolved (the user named it) keeps its unstamped standing: record_approved_credentials_in_global_llm_context
+ # skips stamped ids, so stamping it here would strip its durable cross-turn approval.
+ already_resolved = {credential.credential_id for credential in policy.resolved_credentials}
+ policy.resolved_credentials = _deduplicate_credentials(list(policy.resolved_credentials) + list(candidates))
+ for candidate in candidates:
+ if candidate.credential_id not in already_resolved:
+ policy.live_page_admitted_urls[candidate.credential_id] = page_url
+
+
+async def resolve_credential_for_live_page(
+ policy: RequestPolicy,
+ *,
+ organization_id: str,
+ page_url: str,
+ load_org_credentials: Callable[[], Awaitable[list[Credential]]] | None = None,
+) -> LivePageResolutionRecord:
+ """Grant run authority for the sole saved credential the observed page vouches for.
+
+ Same discriminator and same records as the fill seam; the difference is only that no
+ credential was requested, so a sole match is the answer rather than something to check against.
+ """
+ resolution = await _resolve_live_page_credentials(
+ policy,
+ organization_id=organization_id,
+ page_url=page_url,
+ load_org_credentials=load_org_credentials,
+ seam="page_observation",
+ )
+ if resolution is None:
+ record = LivePageResolutionRecord(verdict="declined", page_url=page_url)
+ else:
+ record = LivePageResolutionRecord(
+ verdict="no_match" if resolution.verdict == "unresolved" else resolution.verdict,
+ tier=resolution.tier,
+ candidates=tuple(resolution.candidates),
+ page_url=page_url,
+ )
+ if record.verdict == "resolved":
+ _record_live_page_admission(policy, record.candidates, page_url)
+ if record.verdict == "ambiguous":
+ policy.discovered_credentials = _deduplicate_credentials(
+ list(policy.discovered_credentials) + list(record.candidates)
+ )
+ # A later page that matches nothing must not erase the login page's answer, which the
+ # blocked-output renderer still needs at the end of the turn. An ambiguous verdict also outranks a
+ # later resolved one from a different page: the ambiguity is the thing the user has to settle, and
+ # a same-page verdict is a genuine update rather than an unrelated overwrite.
+ stored = policy.live_page_resolution
+ if record.verdict in {"resolved", "ambiguous"} and (
+ stored is None
+ or stored.verdict != "ambiguous"
+ or stored.page_url == record.page_url
+ or record.verdict == "ambiguous"
+ ):
+ policy.live_page_resolution = record
+ observation_outcome = "admitted" if record.verdict == "resolved" else record.verdict
+ if _live_page_log_allowed(policy, page_url, "page_observation", observation_outcome):
+ LOG.info(
+ "copilot credential live-page admission",
+ outcome=observation_outcome,
+ seam="page_observation",
+ organization_id=organization_id,
+ page_url=loggable_origin(page_url),
+ tier=record.tier,
+ candidate_credential_ids=[candidate.credential_id for candidate in record.candidates],
+ )
+ return record
+
+
+async def admit_credential_for_live_page(
+ policy: RequestPolicy,
+ *,
+ organization_id: str,
+ credential_id: str,
+ page_url: str,
+ load_org_credentials: Callable[[], Awaitable[list[Credential]]] | None = None,
+) -> LiveCredentialAdmission:
+ """Grant run authority for the credential whose saved login URL matches the page the scout reached.
+
+ The discriminator is that server-computed match, never the model's claim: a credential the page
+ does not vouch for is refused, and so is one saved without a login URL, which is evidence about
+ no page at all.
+ """
+ if not credential_id:
+ return LiveCredentialAdmission(False)
+ resolution = await _resolve_live_page_credentials(
+ policy,
+ organization_id=organization_id,
+ page_url=page_url,
+ load_org_credentials=load_org_credentials,
+ seam="fill",
+ credential_id=credential_id,
+ )
+ if resolution is None:
+ return LiveCredentialAdmission(False)
# Being *one of* several page matches is not authority; only a sole match the page
# vouches for is, so an ambiguous set still asks even when it contains the requested id.
admitted = resolution.verdict == "resolved" and resolution.contains(credential_id)
@@ -5068,6 +5202,7 @@ async def admit_credential_for_live_page(
outcome="admitted"
if admitted
else ("wrong_credential" if resolution.verdict == "resolved" else resolution.verdict),
+ seam="fill",
organization_id=organization_id,
credential_id=credential_id,
page_url=loggable_origin(page_url),
@@ -5075,11 +5210,7 @@ async def admit_credential_for_live_page(
candidate_credential_ids=[candidate.credential_id for candidate in resolution.candidates],
)
if admitted:
- policy.resolved_credentials = _deduplicate_credentials(
- list(policy.resolved_credentials) + list(resolution.candidates)
- )
- for candidate in resolution.candidates:
- policy.live_page_admitted_urls[candidate.credential_id] = page_url
+ _record_live_page_admission(policy, resolution.candidates, page_url)
return LiveCredentialAdmission(True, page_url=page_url)
if resolution.verdict == "ambiguous":
diff --git a/skyvern/forge/sdk/copilot/tools/__init__.py b/skyvern/forge/sdk/copilot/tools/__init__.py
index 675638e5b..399e61e30 100644
--- a/skyvern/forge/sdk/copilot/tools/__init__.py
+++ b/skyvern/forge/sdk/copilot/tools/__init__.py
@@ -1032,8 +1032,11 @@ async def fill_credential_field_tool(
`selector` must be a CSS selector for the exact input field (no comma-union
fallbacks — inspect the page first and target the proven field).
- `credential_id` must be a credential from the request policy's
- `resolved_credentials`. `field` is one of `username`, `password`, `totp`.
+ `credential_id`: when a page observation returns
+ `resolved_login_credential_id`, the server has already authorized that
+ credential for this login page — pass that id. When it returns
+ `candidate_login_credentials`, ask the user which one to use and pass the
+ `credential_id` they choose. `field` is one of `username`, `password`, `totp`.
This tool only fills; it never clicks or submits. Each successful fill is
recorded as a scouted interaction, so the SYNTHESIZED CODE BLOCK will bind
diff --git a/skyvern/forge/sdk/copilot/tools/composition_capture.py b/skyvern/forge/sdk/copilot/tools/composition_capture.py
index a2d0b3820..84707ae98 100644
--- a/skyvern/forge/sdk/copilot/tools/composition_capture.py
+++ b/skyvern/forge/sdk/copilot/tools/composition_capture.py
@@ -47,6 +47,7 @@ from .completion import (
)
from .discovery import _resolve_discovery_entry_url
from .guardrails import _authority_tool_error
+from .mcp_hooks import _bind_login_credential_for_observed_url
from .scouting import (
_clear_pending_browser_interaction_observation,
_consume_pending_browser_interaction_observation,
@@ -727,6 +728,7 @@ async def _inspect_page_for_composition_impl(
"reached_via": reached_via,
"data": evidence,
}
+ await _bind_login_credential_for_observed_url(copilot_ctx, str(current_url), result)
if observation_step is not None:
result["observation_step"] = observation_step
record_tool_step_result_for_ctx(copilot_ctx, "inspect_page_for_composition", arguments, result)
diff --git a/skyvern/forge/sdk/copilot/tools/mcp_hooks.py b/skyvern/forge/sdk/copilot/tools/mcp_hooks.py
index 5011a8f16..09076a7b1 100644
--- a/skyvern/forge/sdk/copilot/tools/mcp_hooks.py
+++ b/skyvern/forge/sdk/copilot/tools/mcp_hooks.py
@@ -22,12 +22,14 @@ from skyvern.forge.sdk.copilot.config import (
download_scout_act_required_for_policy,
)
from skyvern.forge.sdk.copilot.context import CopilotContext
+from skyvern.forge.sdk.copilot.credential_resolution import load_credentials
from skyvern.forge.sdk.copilot.mcp_adapter import SchemaOverlay
-from skyvern.forge.sdk.copilot.request_policy import RequestPolicy
+from skyvern.forge.sdk.copilot.request_policy import RequestPolicy, resolve_credential_for_live_page
from skyvern.forge.sdk.copilot.request_slots import is_canonical_request_slot_path
from skyvern.forge.sdk.copilot.runtime import AgentContext, ScoutedInteraction
from skyvern.forge.sdk.copilot.secret_scrub import registered_scrub_values
from skyvern.forge.sdk.copilot.typed_value_policy import safe_typed_default_value, should_reject_type_text_value
+from skyvern.forge.sdk.schemas.credentials import Credential
from ._shared import _DISCOVERY_PER_CALL_TIMEOUT_SECONDS, _composition_get_structured_evidence
from .banned_blocks import (
@@ -435,6 +437,52 @@ async def _press_key_pre_hook(
return _strip_intent_for_code_only_selector_action(params, ctx, tool_name="press_key")
+async def _bind_login_credential_for_observed_url(ctx: AgentContext, url: str, result: dict[str, Any]) -> None:
+ """Surface the saved credential the observed page vouches for, if the server can bind one.
+
+ A resolver or loader failure leaves the page observation exactly as it was, but the context
+ reads stay outside that guard: swallowing them would turn a mis-wired context into a silent
+ no-op that no test can catch.
+ """
+ if not url:
+ return
+
+ policy = ctx.request_policy
+ if not isinstance(policy, RequestPolicy):
+ return
+ organization_id = ctx.organization_id
+
+ async def load_once() -> list[Credential]:
+ if ctx.org_credentials_for_turn is None:
+ ctx.org_credentials_for_turn = await load_credentials(organization_id)
+ return ctx.org_credentials_for_turn
+
+ try:
+ record = await resolve_credential_for_live_page(
+ policy,
+ organization_id=organization_id,
+ page_url=url,
+ load_org_credentials=load_once,
+ )
+ except Exception:
+ LOG.warning(
+ "copilot credential live-page admission",
+ outcome="resolver_error",
+ seam="page_observation",
+ exc_info=True,
+ )
+ return
+
+ if record.verdict == "resolved" and record.candidates:
+ credential = record.candidates[0]
+ result["resolved_login_credential_id"] = credential.credential_id
+ result["resolved_login_credential_name"] = credential.name
+ elif record.verdict == "ambiguous":
+ result["candidate_login_credentials"] = [
+ {"credential_id": candidate.credential_id, "name": candidate.name} for candidate in record.candidates
+ ]
+
+
async def _navigate_post_hook(
result: dict[str, Any],
raw: dict[str, Any],
@@ -444,6 +492,7 @@ async def _navigate_post_hook(
if result.get("ok"):
data = result.pop("data", {})
result["url"] = data.get("url", "")
+ await _bind_login_credential_for_observed_url(ctx, result["url"], result)
result["next_step"] = (
"Page loaded, and a screenshot of it is attached. Use evaluate or "
"inspect_page_for_composition when you need the page's structure or selectors "
@@ -508,6 +557,7 @@ async def _click_post_hook(
"url": url,
"title": title,
}
+ await _bind_login_credential_for_observed_url(ctx, url, result)
navigated = bool(source_url) and bool(url) and source_url != url
role, accessible_name = await _resolve_scout_role_name(ctx, selector, allow_browser_read=not navigated)
if navigated and not (role and accessible_name):
@@ -1077,6 +1127,7 @@ async def _press_key_post_hook(
"selector": selector,
"url": url,
}
+ await _bind_login_credential_for_observed_url(ctx, url, result)
_record_scouted_interaction(
ctx,
tool_name="press_key",
diff --git a/skyvern/forge/sdk/protected_reference.py b/skyvern/forge/sdk/protected_reference.py
new file mode 100644
index 000000000..2546bf76f
--- /dev/null
+++ b/skyvern/forge/sdk/protected_reference.py
@@ -0,0 +1,13 @@
+"""Consumer-bound resolution contract for browser-firewall protected references."""
+
+from __future__ import annotations
+
+from typing import Protocol
+
+from skyvern.forge.sdk.browser_action_policy import ProtectedReference
+
+
+class ProtectedReferenceResolver(Protocol):
+ """Capability exposed only to final trusted sinks."""
+
+ async def resolve(self, ref: ProtectedReference, run_id: str, consumer_id: str) -> str: ...
diff --git a/skyvern/webeye/cdp_retry.py b/skyvern/webeye/cdp_retry.py
index 4c8d28593..450382ad7 100644
--- a/skyvern/webeye/cdp_retry.py
+++ b/skyvern/webeye/cdp_retry.py
@@ -34,6 +34,9 @@ _CDP_CONNECTION_ERROR_SUBSTR_FALLBACK = (
# selected: they are raised beneath every driver, so no engine's native error families own them. Kept
# as the shared floor under both the selection-aware and stock paths so migrating classification to a
# selected engine never drops the cross-engine transport signal.
+# These three subclasses, never `OSError` itself: this predicate also gates cloud quarantine, and
+# `OSError` covers permanent local failures (missing or unexecutable browser binary, EACCES, EMFILE)
+# where retrying cannot succeed and pulling the address from the pool is wrong.
_CDP_CONNECTION_TRANSPORT_ERRORS: tuple[type[BaseException], ...] = (
ConnectionRefusedError,
ConnectionResetError,
diff --git a/tests/unit/test_cdp_error_detection.py b/tests/unit/test_cdp_error_detection.py
index c196b6d4c..145569097 100644
--- a/tests/unit/test_cdp_error_detection.py
+++ b/tests/unit/test_cdp_error_detection.py
@@ -1,5 +1,8 @@
from __future__ import annotations
+import errno
+import socket
+import ssl
from typing import Any, cast
import pytest
@@ -136,3 +139,50 @@ class TestSelectionAwareDetection:
selection = _rich_fake_selection()
for exc in (ConnectionRefusedError("ECONNREFUSED"), ConnectionResetError("ECONNRESET"), TimeoutError("late")):
assert is_cdp_connection_error(exc, selection), f"Expected transport floor for {exc!r}"
+
+
+class TestTransportFloorExcludesBlanketOSError:
+ """The floor is the three narrow ``OSError`` subclasses, never ``OSError`` itself.
+
+ ``is_cdp_connection_error`` gates the runner retry loop *and* cloud quarantine, so a ``True``
+ both spends the retry budget and pulls the address out of the schedulable pool. Widening to
+ ``OSError`` would capture ~24 other classes, including permanent local-launch failures — a
+ missing or unexecutable browser binary, EACCES, EMFILE — where retrying cannot succeed and
+ quarantining is wrong. ECONNREFUSED / ECONNRESET / ETIMEDOUT, the socket conditions the retry
+ loop exists for, are already covered by their own subclasses below.
+ """
+
+ @pytest.mark.parametrize(
+ "exc",
+ [
+ OSError(errno.EHOSTUNREACH, "No route to host"),
+ socket.gaierror(socket.EAI_NONAME, "Name or service not known"),
+ ssl.SSLCertVerificationError("certificate verify failed"),
+ BrokenPipeError(errno.EPIPE, "Broken pipe"),
+ ConnectionAbortedError(errno.ECONNABORTED, "Software caused connection abort"),
+ FileNotFoundError(errno.ENOENT, "No such file or directory"),
+ PermissionError(errno.EACCES, "Permission denied"),
+ OSError(errno.EMFILE, "Too many open files"),
+ OSError(errno.ENOSPC, "No space left on device"),
+ IsADirectoryError(errno.EISDIR, "Is a directory"),
+ ],
+ ids=lambda e: type(e).__name__ + ":" + str(getattr(e, "errno", "")),
+ )
+ def test_non_transport_oserrors_are_not_retryable(self, exc: Exception) -> None:
+ assert not is_cdp_connection_error(exc)
+ assert not is_cdp_connection_error(exc, _stock_selection())
+ assert not is_cdp_connection_error(exc, _rich_fake_selection())
+
+ @pytest.mark.parametrize(
+ "exc",
+ [
+ ConnectionRefusedError(errno.ECONNREFUSED, "Connection refused"),
+ ConnectionResetError(errno.ECONNRESET, "Connection reset by peer"),
+ TimeoutError(errno.ETIMEDOUT, "Connection timed out"),
+ ],
+ ids=lambda e: type(e).__name__,
+ )
+ def test_socket_transport_subclasses_stay_retryable(self, exc: Exception) -> None:
+ assert is_cdp_connection_error(exc)
+ assert is_cdp_connection_error(exc, _stock_selection())
+ assert is_cdp_connection_error(exc, _rich_fake_selection())
diff --git a/tests/unit/test_copilot_agent_blocker_renderer.py b/tests/unit/test_copilot_agent_blocker_renderer.py
index 93945c1eb..c4674899b 100644
--- a/tests/unit/test_copilot_agent_blocker_renderer.py
+++ b/tests/unit/test_copilot_agent_blocker_renderer.py
@@ -36,7 +36,7 @@ from skyvern.forge.sdk.copilot.output_policy import (
OutputPolicyReason,
OutputPolicyVerdict,
)
-from skyvern.forge.sdk.copilot.request_policy import CompletionCriterion, RequestPolicy
+from skyvern.forge.sdk.copilot.request_policy import CompletionCriterion, LivePageResolutionRecord, RequestPolicy
from skyvern.forge.sdk.copilot.run_outcome import TERMINAL_CHALLENGE_BLOCKER_REASON_CODE, RecordedRunOutcome
from skyvern.forge.sdk.copilot.tools.discovery import _build_discovery_exhausted_escape_signal
from skyvern.forge.sdk.copilot.turn_halt import TurnHalt, TurnHaltKind
@@ -1020,6 +1020,49 @@ def test_unapproved_credential_reference_asks_without_naming_the_credential_inve
result = _blocked_result(ctx, OutputPolicyReason.UNAPPROVED_CREDENTIAL_REFERENCE)
- assert "confirm which saved credential" in result.user_response
+ assert "I need an approved credential to continue" in result.user_response
+ # Only the observation seam records a verdict, so no-verdict must not be rendered as "no match":
+ # a fill-seam ambiguity reaches this branch with a real match behind it.
+ assert "could not match" not in result.user_response
+ assert "Credentials UI" in result.user_response
assert "cred_unrelated" not in result.user_response
assert "hr portal" not in result.user_response
+
+
+def test_unapproved_credential_reference_lists_both_ambiguous_candidate_ids() -> None:
+ ctx = _ctx()
+ ctx.request_policy = RequestPolicy(
+ live_page_resolution=LivePageResolutionRecord(
+ verdict="ambiguous",
+ tier="url_path",
+ candidates=(
+ SimpleNamespace(credential_id="cred_first", name="first login"),
+ SimpleNamespace(credential_id="cred_second", name="second login"),
+ ),
+ )
+ )
+
+ result = _blocked_result(ctx, OutputPolicyReason.UNAPPROVED_CREDENTIAL_REFERENCE)
+
+ assert "cred_first" in result.user_response
+ assert "cred_second" in result.user_response
+ assert "credential ID" in result.user_response
+
+
+def test_unapproved_credential_reference_points_at_credentials_ui_when_nothing_matched() -> None:
+ """A no-match verdict must not borrow the ambiguous reply's candidate list."""
+ ctx = _ctx()
+ ctx.request_policy = RequestPolicy(
+ live_page_resolution=LivePageResolutionRecord(
+ verdict="no_match",
+ page_url="https://analytics.example.com/login",
+ candidates=(SimpleNamespace(credential_id="cred_unmatched", name="unmatched login"),),
+ )
+ )
+
+ result = _blocked_result(ctx, OutputPolicyReason.UNAPPROVED_CREDENTIAL_REFERENCE)
+
+ assert "Credentials UI" in result.user_response
+ # Candidates ride on the record whatever the verdict; only an ambiguous one may name them.
+ assert "cred_unmatched" not in result.user_response
+ assert "More than one saved credential" not in result.user_response
diff --git a/tests/unit/test_copilot_context.py b/tests/unit/test_copilot_context.py
index a649fb669..4cf182bc8 100644
--- a/tests/unit/test_copilot_context.py
+++ b/tests/unit/test_copilot_context.py
@@ -785,6 +785,16 @@ def test_record_approved_credentials_survive_prompt_sanitization() -> None:
assert ids == ["cred_portal"]
+def test_a_live_page_grant_does_not_carry_into_a_later_turn() -> None:
+ """The evidence is a page a later turn has not seen, whichever seam admitted it."""
+ ctx = _policy_ctx([SimpleNamespace(credential_id="cred_portal", name="mock-portal-login")])
+ ctx.request_policy.live_page_admitted_urls = {"cred_portal": "https://portal.example.com/login"}
+
+ raw = record_approved_credentials_in_global_llm_context(ctx, None)
+
+ assert StructuredContext.from_json_str(raw).approved_credentials == []
+
+
def test_record_approved_credentials_no_ops_without_resolved() -> None:
assert record_approved_credentials_in_global_llm_context(_policy_ctx([]), None) is None
assert record_approved_credentials_in_global_llm_context(SimpleNamespace(request_policy=None), "prior") == "prior"
diff --git a/tests/unit/test_copilot_discovery.py b/tests/unit/test_copilot_discovery.py
index 550f0bf98..77ccbc678 100644
--- a/tests/unit/test_copilot_discovery.py
+++ b/tests/unit/test_copilot_discovery.py
@@ -33,6 +33,8 @@ class _Ctx:
self.pending_browser_interaction_observation = None
self.workflow_verification_evidence = WorkflowVerificationEvidence()
self.browser_session_id = None
+ self.request_policy = None
+ self.org_credentials_for_turn = None
class _FailingNavigateServer:
diff --git a/tests/unit/test_copilot_evaluate_page_evidence.py b/tests/unit/test_copilot_evaluate_page_evidence.py
index 54326832e..d8f3ab971 100644
--- a/tests/unit/test_copilot_evaluate_page_evidence.py
+++ b/tests/unit/test_copilot_evaluate_page_evidence.py
@@ -1,6 +1,8 @@
from __future__ import annotations
-from unittest.mock import MagicMock
+import json
+from types import SimpleNamespace
+from unittest.mock import AsyncMock, MagicMock, patch
import pytest
@@ -18,6 +20,7 @@ from skyvern.forge.sdk.copilot.tools import (
from skyvern.forge.sdk.copilot.tools import run_execution as run_execution_module
from skyvern.forge.sdk.copilot.tools.blockers import _tool_loop_error
from skyvern.forge.sdk.copilot.turn_intent import TurnIntent, TurnIntentAuthority, TurnIntentMode
+from skyvern.forge.sdk.schemas.credentials import CredentialType
def _ctx() -> CopilotContext:
@@ -408,3 +411,59 @@ async def test_failed_evaluate_does_not_leak_expression_into_next_read() -> None
await _evaluate_post_hook({"ok": True, "data": {"result": "still here"}}, raw={}, ctx=ctx)
assert [i for i in ctx.scout_trajectory if i.get("tool_name") == "read_value"] == []
+
+
+async def test_inspecting_a_login_page_binds_the_credential_that_page_vouches_for(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ login_url = "https://analytics.example.test/login?next=%2Fweb"
+ ctx = _ctx()
+
+ async def fallback_page_info(_ctx: CopilotContext) -> tuple[str, str]:
+ return login_url, "Sign in"
+
+ async def capture_evidence(
+ _ctx: CopilotContext,
+ *,
+ inspected_url: str,
+ current_url: str,
+ ) -> tuple[dict[str, object], None]:
+ return (
+ {
+ "inspected_url": inspected_url,
+ "current_url": current_url,
+ "page_title": "Sign in",
+ "source_tool": "inspect_page_for_composition",
+ "forms": [],
+ "result_containers": [],
+ "navigation_targets": [],
+ "challenge_controls": [],
+ },
+ None,
+ )
+
+ monkeypatch.setattr(
+ "skyvern.forge.sdk.copilot.tools.composition_capture._fallback_page_info",
+ fallback_page_info,
+ )
+ monkeypatch.setattr(
+ "skyvern.forge.sdk.copilot.tools.composition_capture._capture_composition_evidence",
+ capture_evidence,
+ )
+ org_credentials = [
+ SimpleNamespace(
+ credential_id="cred_analytics",
+ name="analytics",
+ tested_url="https://analytics.example.test/login",
+ credential_type=CredentialType.PASSWORD,
+ )
+ ]
+
+ with patch("skyvern.forge.app.DATABASE.credentials.get_credentials", new=AsyncMock(return_value=org_credentials)):
+ result = await _inspect_page_for_composition_impl(ctx, "current_page")
+
+ assert result["resolved_login_credential_id"] == "cred_analytics"
+ assert result["resolved_login_credential_name"] == "analytics"
+ assert ctx.request_policy.live_page_admitted_urls == {"cred_analytics": login_url}
+ assert "tested_url" not in json.dumps(result)
diff --git a/tests/unit/test_copilot_fill_credential_field.py b/tests/unit/test_copilot_fill_credential_field.py
index b82c88f92..e39015bf4 100644
--- a/tests/unit/test_copilot_fill_credential_field.py
+++ b/tests/unit/test_copilot_fill_credential_field.py
@@ -53,6 +53,7 @@ def _ctx(**overrides: Any) -> SimpleNamespace:
fill_carry_rebound_done=False,
observed_browser_urls=[],
pending_scout_source_url=None,
+ pending_scout_download_snapshot=None,
pending_browser_interaction_observation=None,
discovery_mcp_server=None,
secret_scrub_values=[],
@@ -689,3 +690,214 @@ class TestCredentialFillLivePageAdmission:
assert first["ok"] is True
assert second["ok"] is True
assert len(page.fill_calls) == 2
+
+
+_DECOY_LOGIN_URL = "https://billing.example.com/login"
+
+
+class TestObservationSeamCredentialBinding:
+ """Page observation binds the sole URL-matched credential before any fill is attempted.
+
+ Every case starts from an empty resolved set, which is what a prompt with no login wording
+ leaves behind when the scout walks into a sign-in wall.
+ """
+
+ async def _observe_navigate(
+ self,
+ ctx: SimpleNamespace,
+ url: str,
+ org_credentials: list[SimpleNamespace],
+ ) -> tuple[dict[str, Any], AsyncMock]:
+ load_mock = AsyncMock(return_value=org_credentials)
+ with patch("skyvern.forge.app.DATABASE.credentials.get_credentials", new=load_mock):
+ result = await tools_module._navigate_post_hook({"ok": True, "data": {"url": url}}, {}, ctx)
+ return result, load_mock
+
+ @pytest.mark.asyncio
+ async def test_navigating_to_a_matched_login_page_binds_and_surfaces_the_id(self) -> None:
+ policy = RequestPolicy()
+ ctx = _ctx(request_policy=policy)
+
+ result, _ = await self._observe_navigate(
+ ctx,
+ _FIXTURE_LOGIN_URL,
+ [
+ _org_credential("cred_analytics", "analytics", _FIXTURE_LOGIN_URL),
+ _org_credential("cred_billing", "billing", _DECOY_LOGIN_URL),
+ _org_credential("cred_urlless", "urlless", None),
+ ],
+ )
+
+ assert result["resolved_login_credential_id"] == "cred_analytics"
+ assert result["resolved_login_credential_name"] == "analytics"
+ assert "candidate_login_credentials" not in result
+
+ @pytest.mark.asyncio
+ async def test_the_surfaced_id_passes_the_fill_gate_without_user_confirmation(self) -> None:
+ policy = RequestPolicy()
+ ctx = _ctx(request_policy=policy)
+ result, _ = await self._observe_navigate(
+ ctx, _FIXTURE_LOGIN_URL, [_org_credential("cred_analytics", "analytics", _FIXTURE_LOGIN_URL)]
+ )
+
+ with patch.object(credential_fill_module, "_live_working_page_url", AsyncMock(return_value=_FIXTURE_LOGIN_URL)):
+ error, admitted_url = await credential_fill_module._credential_fill_gate_error(
+ ctx, result["resolved_login_credential_id"]
+ )
+
+ assert error is None
+ assert admitted_url == _FIXTURE_LOGIN_URL
+
+ @pytest.mark.asyncio
+ async def test_leaving_the_admitted_origin_after_the_seam_bind_refuses_the_fill(
+ self, monkeypatch: pytest.MonkeyPatch
+ ) -> None:
+ page = _FakePage()
+ _wire_impl(monkeypatch, page)
+ ctx = _ctx(request_policy=RequestPolicy())
+ await self._observe_navigate(
+ ctx, _FIXTURE_LOGIN_URL, [_org_credential("cred_analytics", "analytics", _FIXTURE_LOGIN_URL)]
+ )
+ page.url = "https://elsewhere.example.com/collect"
+
+ result = await tools_module._fill_credential_field_impl(ctx, "#passwordInput", "cred_analytics", "password")
+
+ assert result["ok"] is False
+ assert page.fill_calls == []
+
+ @pytest.mark.asyncio
+ async def test_two_matches_surface_both_ids_and_bind_nothing(self) -> None:
+ policy = RequestPolicy()
+ ctx = _ctx(request_policy=policy)
+
+ result, _ = await self._observe_navigate(
+ ctx,
+ _FIXTURE_LOGIN_URL,
+ [
+ _org_credential("cred_one", "analytics one", _FIXTURE_LOGIN_URL),
+ _org_credential("cred_two", "analytics two", _FIXTURE_LOGIN_URL),
+ ],
+ )
+
+ assert result["candidate_login_credentials"] == [
+ {"credential_id": "cred_one", "name": "analytics one"},
+ {"credential_id": "cred_two", "name": "analytics two"},
+ ]
+ assert "resolved_login_credential_id" not in result
+ assert policy.resolved_credentials == []
+ assert policy.live_page_admitted_urls == {}
+
+ @pytest.mark.asyncio
+ async def test_no_match_leaves_the_observation_exactly_as_it_was(self) -> None:
+ policy = RequestPolicy()
+ ctx = _ctx(request_policy=policy)
+
+ result, _ = await self._observe_navigate(
+ ctx,
+ _FIXTURE_LOGIN_URL,
+ [_org_credential("cred_billing", "billing", _DECOY_LOGIN_URL), _org_credential("cred_urlless", "u", None)],
+ )
+
+ assert result["ok"] is True
+ assert "resolved_login_credential_id" not in result
+ assert "resolved_login_credential_name" not in result
+ assert "candidate_login_credentials" not in result
+ assert policy.resolved_credentials == []
+
+ @pytest.mark.asyncio
+ @pytest.mark.parametrize(
+ "org_credentials",
+ [
+ # Each arm carries a non-matching credential whose own tested_url is the decoy, so the
+ # decoy assertion below can actually fail: a serializer that leaked saved URLs would
+ # carry this one even though the page never matched it.
+ [
+ _org_credential("cred_analytics", "analytics", _FIXTURE_LOGIN_URL),
+ _org_credential("cred_decoy", "decoy", _DECOY_LOGIN_URL),
+ ],
+ [
+ _org_credential("cred_one", "analytics one", _FIXTURE_LOGIN_URL),
+ _org_credential("cred_two", "analytics two", _FIXTURE_LOGIN_URL),
+ _org_credential("cred_decoy", "decoy", _DECOY_LOGIN_URL),
+ ],
+ ],
+ )
+ async def test_no_saved_login_url_reaches_the_model(self, org_credentials: list[SimpleNamespace]) -> None:
+ ctx = _ctx(request_policy=RequestPolicy())
+
+ result, _ = await self._observe_navigate(ctx, _FIXTURE_LOGIN_URL, org_credentials)
+
+ serialized = json.dumps(result)
+ assert "tested_url" not in serialized
+ assert _DECOY_LOGIN_URL not in serialized
+
+ @pytest.mark.asyncio
+ async def test_a_click_that_lands_on_the_login_wall_binds(self) -> None:
+ policy = RequestPolicy()
+ ctx = _ctx(
+ request_policy=policy,
+ pending_scout_role_name=None,
+ pending_scout_click_selector=None,
+ pending_scout_ambiguous=None,
+ pending_scout_reanchor=None,
+ pending_scout_dynamic_row=None,
+ last_scout_act_observe_outcome=None,
+ last_scout_act_observe_packet=None,
+ )
+
+ with patch(
+ "skyvern.forge.app.DATABASE.credentials.get_credentials",
+ new=AsyncMock(return_value=[_org_credential("cred_analytics", "analytics", _FIXTURE_LOGIN_URL)]),
+ ):
+ result = await tools_module._click_post_hook(
+ {"ok": True, "data": {"selector": "#sign-in"}},
+ {"browser_context": {"url": _FIXTURE_LOGIN_URL, "title": "Sign in"}},
+ ctx,
+ )
+
+ assert result["resolved_login_credential_id"] == "cred_analytics"
+ assert [c.credential_id for c in policy.resolved_credentials] == ["cred_analytics"]
+
+ @pytest.mark.asyncio
+ async def test_an_enter_press_that_lands_on_the_login_wall_binds(self) -> None:
+ policy = RequestPolicy()
+ ctx = _ctx(request_policy=policy)
+
+ with patch(
+ "skyvern.forge.app.DATABASE.credentials.get_credentials",
+ new=AsyncMock(return_value=[_org_credential("cred_analytics", "analytics", _FIXTURE_LOGIN_URL)]),
+ ):
+ result = await tools_module._press_key_post_hook(
+ {"ok": True, "data": {"key": "Enter", "selector": "#search"}},
+ {"browser_context": {"url": _FIXTURE_LOGIN_URL, "title": "Sign in"}},
+ ctx,
+ )
+
+ assert result["resolved_login_credential_id"] == "cred_analytics"
+ assert [c.credential_id for c in policy.resolved_credentials] == ["cred_analytics"]
+
+ @pytest.mark.asyncio
+ async def test_a_credential_loader_failure_leaves_the_tool_result_untouched(self) -> None:
+ policy = RequestPolicy()
+ ctx = _ctx(request_policy=policy)
+
+ with patch(
+ "skyvern.forge.app.DATABASE.credentials.get_credentials",
+ new=AsyncMock(side_effect=RuntimeError("credential table unavailable")),
+ ):
+ result = await tools_module._navigate_post_hook({"ok": True, "data": {"url": _FIXTURE_LOGIN_URL}}, {}, ctx)
+
+ assert result["ok"] is True
+ assert "resolved_login_credential_id" not in result
+ assert policy.resolved_credentials == []
+ assert policy.live_page_admitted_urls == {}
+
+ @pytest.mark.asyncio
+ async def test_the_lite_lane_never_reads_credentials(self) -> None:
+ ctx = _ctx(request_policy=None)
+ result, load_mock = await self._observe_navigate(
+ ctx, _FIXTURE_LOGIN_URL, [_org_credential("cred_analytics", "analytics", _FIXTURE_LOGIN_URL)]
+ )
+
+ assert "resolved_login_credential_id" not in result
+ load_mock.assert_not_awaited()
diff --git a/tests/unit/test_copilot_hooks.py b/tests/unit/test_copilot_hooks.py
index 135a98ad1..4bbbc529d 100644
--- a/tests/unit/test_copilot_hooks.py
+++ b/tests/unit/test_copilot_hooks.py
@@ -1008,6 +1008,9 @@ class TestBrowserInteractionObservationHooks:
scouted_interactions=[],
scout_trajectory=[],
pending_scout_source_url=None,
+ request_policy=None,
+ org_credentials_for_turn=None,
+ organization_id="o_1",
)
result = await _click_post_hook(
{"ok": True, "data": {"selector": "#add-to-cart"}},
@@ -1111,6 +1114,9 @@ class TestScoutedInteractionCapture:
pending_scout_source_url=source_url,
prior_fill_carry=[],
fill_carry_rebound_done=False,
+ request_policy=None,
+ org_credentials_for_turn=None,
+ organization_id="o_1",
)
if policy is not None:
ns.block_authoring_policy = policy
diff --git a/tests/unit/test_copilot_output_policy.py b/tests/unit/test_copilot_output_policy.py
index 76eef61af..940aabd4b 100644
--- a/tests/unit/test_copilot_output_policy.py
+++ b/tests/unit/test_copilot_output_policy.py
@@ -1785,7 +1785,7 @@ def test_sdk_output_guardrail_allows_unknown_click_with_authority_denied_blocker
@pytest.mark.parametrize(
("reason", "expected_terms"),
[
- (OutputPolicyReason.UNAPPROVED_CREDENTIAL_REFERENCE, ("credential", "confirm")),
+ (OutputPolicyReason.UNAPPROVED_CREDENTIAL_REFERENCE, ("credential", "credential id")),
(OutputPolicyReason.CREDENTIAL_SCOPE_BROADENED, ("credential", "url", "re-select")),
],
)
@@ -1901,7 +1901,7 @@ workflow_definition:
assert result.response_type == "ASK_QUESTION"
assert "credential" in result.user_response.lower()
- assert "confirm" in result.user_response.lower()
+ assert "credential id" in result.user_response.lower()
assert "I could not safely return" not in result.user_response
diff --git a/tests/unit/test_copilot_request_policy_workflow_credentials.py b/tests/unit/test_copilot_request_policy_workflow_credentials.py
index 01b1f0741..4b0ed7276 100644
--- a/tests/unit/test_copilot_request_policy_workflow_credentials.py
+++ b/tests/unit/test_copilot_request_policy_workflow_credentials.py
@@ -26,6 +26,7 @@ from skyvern.forge.sdk.copilot.request_policy import (
CREDENTIAL_DEFERRED_DRAFT_REASONS,
CREDENTIAL_PROMPT_CLARIFICATION_REASONS,
LiveCredentialAdmission,
+ LivePageResolutionRecord,
RequestPolicy,
_can_defer_unresolved_credential_name_for_draft,
_classification_from_raw,
@@ -38,6 +39,7 @@ from skyvern.forge.sdk.copilot.request_policy import (
admit_credential_for_live_page,
build_request_policy,
credential_prompt_reason,
+ resolve_credential_for_live_page,
)
from skyvern.forge.sdk.copilot.tools.credentials import _credential_run_approval_error
from skyvern.forge.sdk.schemas.credentials import CredentialType
@@ -2908,6 +2910,175 @@ class TestLivePageCredentialAdmission:
assert admission.admitted is False
load_mock.assert_not_awaited()
+ @pytest.mark.asyncio
+ async def test_the_admission_fingerprint_names_the_fill_seam(self) -> None:
+ with capture_logs() as logs:
+ await _admit(
+ RequestPolicy(),
+ credential_id="cred_analytics",
+ page_url=_LIVE_LOGIN_URL,
+ org_credentials=[_cred("analytics", "cred_analytics", _SAVED_LOGIN_URL)],
+ )
+
+ emitted = [entry for entry in logs if entry["event"] == "copilot credential live-page admission"]
+ assert [(entry["seam"], entry["outcome"]) for entry in emitted] == [("fill", "admitted")]
+
+
+async def _resolve_live_page(
+ policy: RequestPolicy,
+ *,
+ page_url: str,
+ org_credentials: list[SimpleNamespace],
+) -> tuple[LivePageResolutionRecord, AsyncMock]:
+ load_mock = AsyncMock(return_value=org_credentials)
+ with patch("skyvern.forge.app.DATABASE.credentials.get_credentials", new=load_mock):
+ record = await resolve_credential_for_live_page(
+ policy,
+ organization_id="o_test",
+ page_url=page_url,
+ )
+ return record, load_mock
+
+
+class TestLivePageCredentialObservation:
+ """The observation seam asks the same question with no credential requested: whose page is this?"""
+
+ @pytest.mark.asyncio
+ async def test_a_sole_page_match_binds_and_writes_the_fill_seam_records(self) -> None:
+ policy = RequestPolicy()
+ record, _ = await _resolve_live_page(
+ policy,
+ page_url=_LIVE_LOGIN_URL,
+ org_credentials=[
+ _cred("analytics", "cred_analytics", _SAVED_LOGIN_URL),
+ _cred("billing", "cred_billing", "https://billing.example.com/login"),
+ _cred("urlless", "cred_urlless"),
+ ],
+ )
+
+ assert record.verdict == "resolved"
+ assert record.tier == "url_path"
+ assert [c.credential_id for c in record.candidates] == ["cred_analytics"]
+ assert [c.credential_id for c in policy.resolved_credentials] == ["cred_analytics"]
+ assert policy.live_page_admitted_urls == {"cred_analytics": _LIVE_LOGIN_URL}
+
+ @pytest.mark.asyncio
+ async def test_two_page_matches_bind_nothing_and_record_both_candidates(self) -> None:
+ policy = RequestPolicy()
+ record, _ = await _resolve_live_page(
+ policy,
+ page_url=_LIVE_LOGIN_URL,
+ org_credentials=[
+ _cred("analytics one", "cred_one", _SAVED_LOGIN_URL),
+ _cred("analytics two", "cred_two", _SAVED_LOGIN_URL),
+ ],
+ )
+
+ assert record.verdict == "ambiguous"
+ assert [c.credential_id for c in record.candidates] == ["cred_one", "cred_two"]
+ assert policy.resolved_credentials == []
+ assert policy.live_page_admitted_urls == {}
+ assert policy.live_page_resolution is record
+
+ @pytest.mark.asyncio
+ async def test_decoys_only_bind_nothing(self) -> None:
+ policy = RequestPolicy()
+ record, _ = await _resolve_live_page(
+ policy,
+ page_url=_LIVE_LOGIN_URL,
+ org_credentials=[
+ _cred("billing", "cred_billing", "https://billing.example.com/login"),
+ _cred("urlless", "cred_urlless"),
+ ],
+ )
+
+ assert record.verdict == "no_match"
+ assert policy.resolved_credentials == []
+ assert policy.live_page_admitted_urls == {}
+
+ @pytest.mark.asyncio
+ async def test_a_card_credential_never_binds_as_a_login(self) -> None:
+ policy = RequestPolicy()
+ record, _ = await _resolve_live_page(
+ policy,
+ page_url=_LIVE_LOGIN_URL,
+ org_credentials=[_cred("card", "cred_card", _SAVED_LOGIN_URL, CredentialType.CREDIT_CARD)],
+ )
+
+ assert record.verdict == "no_match"
+ assert policy.resolved_credentials == []
+
+ @pytest.mark.asyncio
+ @pytest.mark.parametrize(
+ "policy",
+ [
+ RequestPolicy(raw_secret_detected=True),
+ RequestPolicy(allow_run_blocks=False),
+ RequestPolicy(email_signin_intent=True, login_intent=True),
+ ],
+ )
+ async def test_a_turn_without_run_authority_never_reads_credentials(self, policy: RequestPolicy) -> None:
+ record, load_mock = await _resolve_live_page(
+ policy,
+ page_url=_LIVE_LOGIN_URL,
+ org_credentials=[_cred("analytics", "cred_analytics", _SAVED_LOGIN_URL)],
+ )
+
+ assert record.verdict == "declined"
+ assert policy.resolved_credentials == []
+ assert policy.live_page_admitted_urls == {}
+ load_mock.assert_not_awaited()
+
+ @pytest.mark.asyncio
+ async def test_the_admission_fingerprint_fires_once_per_page(self) -> None:
+ policy = RequestPolicy()
+ org_credentials = [_cred("analytics", "cred_analytics", _SAVED_LOGIN_URL)]
+ with capture_logs() as logs:
+ await _resolve_live_page(policy, page_url=_LIVE_LOGIN_URL, org_credentials=org_credentials)
+ await _resolve_live_page(policy, page_url=_LIVE_LOGIN_URL, org_credentials=org_credentials)
+ await _resolve_live_page(
+ policy, page_url="https://other.example.com/login", org_credentials=org_credentials
+ )
+
+ emitted = [entry for entry in logs if entry["event"] == "copilot credential live-page admission"]
+ assert [entry["seam"] for entry in emitted] == ["page_observation", "page_observation"]
+ assert emitted[0]["outcome"] == "admitted"
+ assert emitted[0]["tier"] == "url_path"
+
+ @pytest.mark.asyncio
+ async def test_a_passwordless_email_turn_declines_under_the_observation_seam(self) -> None:
+ policy = RequestPolicy(email_signin_intent=True, login_intent=True)
+ with capture_logs() as logs:
+ record, _ = await _resolve_live_page(
+ policy,
+ page_url=_LIVE_LOGIN_URL,
+ org_credentials=[_cred("analytics", "cred_analytics", _SAVED_LOGIN_URL)],
+ )
+
+ emitted = [entry for entry in logs if entry["event"] == "copilot credential live-page admission"]
+ assert record.verdict == "declined"
+ # Both lines emit: the guard keys on outcome, so a decline cannot consume the token a later
+ # grant on the same URL needs.
+ assert [(entry["seam"], entry["outcome"]) for entry in emitted] == [
+ ("page_observation", "passwordless_declined"),
+ ("page_observation", "declined"),
+ ]
+
+ @pytest.mark.asyncio
+ async def test_a_later_unmatched_page_does_not_erase_the_login_pages_answer(self) -> None:
+ policy = RequestPolicy()
+ ambiguous_credentials = [
+ _cred("analytics one", "cred_one", _SAVED_LOGIN_URL),
+ _cred("analytics two", "cred_two", _SAVED_LOGIN_URL),
+ ]
+ await _resolve_live_page(policy, page_url=_LIVE_LOGIN_URL, org_credentials=ambiguous_credentials)
+ await _resolve_live_page(
+ policy, page_url="https://help.example.com/docs", org_credentials=ambiguous_credentials
+ )
+
+ assert policy.live_page_resolution is not None
+ assert policy.live_page_resolution.verdict == "ambiguous"
+
@pytest.mark.asyncio
@pytest.mark.parametrize("kind", ["website_stored_credential", "credential_name"])
diff --git a/tests/unit/test_copilot_scout_act_observe.py b/tests/unit/test_copilot_scout_act_observe.py
index dffef86e6..6d98ca4cb 100644
--- a/tests/unit/test_copilot_scout_act_observe.py
+++ b/tests/unit/test_copilot_scout_act_observe.py
@@ -151,6 +151,8 @@ def _ctx(*, server: Any = None, source_url: str | None = _SOURCE_URL) -> SimpleN
scout_observation_contract=None,
scouted_output_covered_paths=set(),
scout_observed_terminal_criterion_ids=set(),
+ request_policy=None,
+ org_credentials_for_turn=None,
)
diff --git a/tests/unit/test_protected_reference.py b/tests/unit/test_protected_reference.py
new file mode 100644
index 000000000..f38c8bd95
--- /dev/null
+++ b/tests/unit/test_protected_reference.py
@@ -0,0 +1,14 @@
+import inspect
+
+from skyvern.forge.sdk.protected_reference import ProtectedReferenceResolver
+
+
+def test_resolver_contract_is_consumer_bound() -> None:
+ signature = inspect.signature(ProtectedReferenceResolver.resolve)
+
+ assert inspect.iscoroutinefunction(ProtectedReferenceResolver.resolve)
+ assert tuple(signature.parameters) == ("self", "ref", "run_id", "consumer_id")
+ assert signature.parameters["ref"].annotation == "ProtectedReference"
+ assert signature.parameters["run_id"].annotation == "str"
+ assert signature.parameters["consumer_id"].annotation == "str"
+ assert signature.return_annotation == "str"