Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
Stop re-asking for a credential the user already approved (SKY-15795) (#8506)
Andrew Neilson1b0cbe043360
diff --git a/skyvern/forge/sdk/copilot/tools/credentials.py b/skyvern/forge/sdk/copilot/tools/credentials.pyindex 228adc8ce..6c8c3ed1e 100644--- a/skyvern/forge/sdk/copilot/tools/credentials.py+++ b/skyvern/forge/sdk/copilot/tools/credentials.py@@ -802,19 +802,36 @@ def _serialize_credential(credential: Credential) -> dict[str, Any]:_DENIED_PASS_ROUTES = ["typed_resume", "request_credential_tool", "literal_credential_id"]-def _typed_resume_arm(reference: str, policy: RequestPolicy) -> str | None:- """Which server record already answered which credential this reference names, if any. Both arms- are server-owned records of an identifier, never an interpretation of prose: the user settled the- id this turn, or the server bound it from a login page it admitted.+def _typed_resume_arm(reference: str, policy: RequestPolicy, matches: list[Credential]) -> str | None:+ """Which server record already answered which credential this reference names, if any. Each arm is+ a server-owned record of an identifier, never an interpretation of prose: the user settled the id+ this turn, the server bound it from a login page it admitted, or the user approved it earlier in+ this chat. ``matches`` is what the live inventory resolves the reference to."""++ def keyed(credential: Credential) -> bool:+ # A name on a stored record can have moved to another credential since it was written, so an+ # arm vouches only for the one the live inventory resolved. Passing when the inventory did not+ # resolve exactly one leaves the not-found and ambiguous answers below to report themselves.+ return len(matches) != 1 or matches[0].credential_id == credential.credential_id+if any(credential.credential_id in policy.current_turn_named_credential_idsand reference in {credential.credential_id, credential.name}+ and keyed(credential)for credential in policy.resolved_credentials):return "user_named_this_turn"- if any(reference in {credential.credential_id, credential.name} for credential in policy.auto_bound_credentials):+ if any(+ reference in {credential.credential_id, credential.name} and keyed(credential)+ for credential in policy.auto_bound_credentials+ ):return "server_auto_bound"+ # Durable approval records the user's own answer, not a page's vouch; the recorder skips a+ # page-stamped id except the carried proposal a citation passed, which keeps its admitting origin.+ # It is recorded by id, so it reads the resolved id directly rather than any stored name.+ if len(matches) == 1 and matches[0].credential_id in policy.prior_approved_credential_ids:+ return "user_approved_earlier_turn"return None@@ -839,7 +856,7 @@ async def _resolve_exact_credential(reference: str, ctx: AgentContext) -> dict[s}matches = list(matches_by_id.values())literal_reference = bool(credential_reference_spans(policy.canonical_user_message, reference))- typed_resume_arm = _typed_resume_arm(reference, policy)+ typed_resume_arm = _typed_resume_arm(reference, policy, matches)if len(matches) == 1 and matches[0].credential_id in _saved_workflow_credential_ids(policy):typed_resume_arm = typed_resume_arm or "saved_workflow"# The agent owns natural-language interpretation. This boundary verifies only@@ -879,7 +896,9 @@ async def _resolve_exact_credential(reference: str, ctx: AgentContext) -> dict[s*[item for item in policy.resolved_credentials if item.credential_id != credential.credential_id],credential,]- user_named = typed_resume_arm is None or reference in grounded_references or literal_reference+ # A literal span the grounded check dropped — a shorter saved name sitting inside a longer one the+ # user actually typed — is not the user naming this credential; no arm here already implies grounded.+ user_named = typed_resume_arm is None or reference in grounded_referencesif user_named:policy.current_turn_named_credential_ids.add(credential.credential_id)# A carried origin answers where the secret belongs only while nobody better has. The userdiff --git a/tests/unit/test_copilot_request_policy_workflow_credentials.py b/tests/unit/test_copilot_request_policy_workflow_credentials.pyindex ab3c477f7..12721c14f 100644--- a/tests/unit/test_copilot_request_policy_workflow_credentials.py+++ b/tests/unit/test_copilot_request_policy_workflow_credentials.py@@ -758,7 +758,7 @@ async def test_the_recorded_proposal_carries_through_the_bootstrap_into_the_credcited_other = await _resolve_exact_credential(other.credential_id, ctx)assert cited_proposal["data"]["status"] == "resolved"- assert _typed_resume_arm(proposed.credential_id, policy) == "server_auto_bound"+ assert _typed_resume_arm(proposed.credential_id, policy, [proposed]) == "server_auto_bound"assert policy.current_turn_named_credential_ids == set()assert policy.live_page_admitted_urls[proposed.credential_id] == _ADMITTED_URLassert cited_other["data"]["status"] == "denied"@@ -855,8 +855,8 @@ async def test_a_card_answered_after_the_seed_still_wins_the_fill_seam(assert policy.current_turn_named_credential_ids == {picked.credential_id}assert _request_settled_credential(policy, picked.credential_id)assert not _request_settled_credential(policy, proposed.credential_id)- assert _typed_resume_arm(picked.credential_id, policy) == "user_named_this_turn"- assert _typed_resume_arm(proposed.credential_id, policy) == "server_auto_bound"+ assert _typed_resume_arm(picked.credential_id, policy, [picked]) == "user_named_this_turn"+ assert _typed_resume_arm(proposed.credential_id, policy, [proposed]) == "server_auto_bound"@pytest.mark.asynciodiff --git a/tests/unit/test_copilot_tools_list_credentials.py b/tests/unit/test_copilot_tools_list_credentials.pyindex db5f7deb0..c9fb4aa11 100644--- a/tests/unit/test_copilot_tools_list_credentials.py+++ b/tests/unit/test_copilot_tools_list_credentials.py@@ -146,6 +146,72 @@ async def test_the_user_naming_another_credential_settles_the_fill_on_that_one()assert _request_settled_credential(policy, "cred_other")+async def test_citing_a_shorter_saved_name_inside_the_one_the_user_typed_is_not_naming_it() -> None:+ # The user typed "portal login", so the grounded check drops "portal" as a substring of it. Citing+ # the shorter credential must not claim the user settled it this turn, which would unpin its origin.+ approved = _password_credential(credential_id="cred_portal", name="portal")+ longer = _password_credential(credential_id="cred_portal_login", name="portal login")+ policy = RequestPolicy(canonical_user_message="use portal login to finish the build")+ policy.prior_approved_credential_ids = {approved.credential_id}+ policy.resolved_credentials = [approved]+ policy.seeded_proposal_credential_ids = {approved.credential_id}+ policy.live_page_admitted_urls = {approved.credential_id: "https://a.example/login"}++ data = await _resolve("portal", policy, [approved, longer])++ assert data["status"] == "resolved"+ assert policy.current_turn_named_credential_ids == set()+ assert policy.live_page_admitted_urls == {approved.credential_id: "https://a.example/login"}++[email protected]("reference_is_approved", [True, False])+async def test_an_approval_from_an_earlier_turn_resolves_without_the_user_naming_it_again(+ reference_is_approved: bool,+) -> None:+ approved = _password_credential(credential_id="cred_approved", name="portal-login")+ other = _password_credential(credential_id="cred_other", name="billing-login")+ # What a turn looks like after _seed_prior_approved_credentials rehydrates an approval the user+ # gave earlier in this chat, on a turn whose message names no credential.+ policy = RequestPolicy(canonical_user_message="now finish the workflow and test it")+ policy.prior_approved_credential_ids = {approved.credential_id}+ policy.resolved_credentials = [approved]++ reference = approved.credential_id if reference_is_approved else other.credential_id+ data = await _resolve(reference, policy, [approved, other])++ assert data["status"] == ("resolved" if reference_is_approved else "denied")+ # The approval answers which credential; it never claims the user named one this turn.+ assert policy.current_turn_named_credential_ids == set()++[email protected]("arm", ["user_named_this_turn", "server_auto_bound", "user_approved_earlier_turn"])+async def test_no_resume_arm_transfers_authority_to_whichever_credential_now_carries_that_name(arm: str) -> None:+ # Every arm's record is written against an id. If that credential is renamed and a different one+ # takes the old name, the name on the stored record must not vouch for the new holder.+ recorded = _password_credential(credential_id="cred_recorded", name="Portal login")+ recorded_now = _password_credential(credential_id="cred_recorded", name="Old portal")+ renamed_onto_the_old_name = _password_credential(credential_id="cred_other", name="Portal login")+ policy = RequestPolicy(canonical_user_message="carry on and finish the build")+ if arm == "user_named_this_turn":+ policy.resolved_credentials = [recorded]+ policy.current_turn_named_credential_ids = {recorded.credential_id}+ elif arm == "server_auto_bound":+ policy.auto_bound_credentials = [recorded]+ else:+ policy.prior_approved_credential_ids = {recorded.credential_id}+ policy.resolved_credentials = [recorded]++ data = await _resolve("Portal login", policy, [recorded_now, renamed_onto_the_old_name])++ assert data["status"] == "denied"+ assert renamed_onto_the_old_name.credential_id not in {+ credential.credential_id for credential in policy.resolved_credentials+ }++@pytest.mark.asyncio@pytest.mark.parametrize("saved", [True, False])async def test_saved_workflow_selection_resolves_but_a_canvas_proposal_does_not(saved: bool) -> None: