Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
Apply an agent's configured proxy region to API runs that omit one (SKY-18884) (#9047)
Aaron Perez5dea6418ab06
diff --git a/skyvern/forge/sdk/routes/agent_protocol.py b/skyvern/forge/sdk/routes/agent_protocol.pyindex 0e6adac76..9fb116cda 100644--- a/skyvern/forge/sdk/routes/agent_protocol.py+++ b/skyvern/forge/sdk/routes/agent_protocol.py@@ -203,6 +203,7 @@ from skyvern.schemas.runs import (BrowserTypeOption,BulkCancelRunsRequest,BulkCancelRunsResponse,+ ProxyLocationInput,RunEngine,RunResponse,RunStatus,@@ -651,7 +652,13 @@ async def run_task(def _workflow_run_request_to_legacy_request(workflow_run_request: WorkflowRunRequest) -> WorkflowRequestBody:return WorkflowRequestBody(data=workflow_run_request.parameters,- proxy_location=workflow_run_request.proxy_location,+ # WorkflowRunRequest.proxy_location carries a RESIDENTIAL default, so forwarding it verbatim+ # would hand setup_workflow_run a non-null value for every caller that omitted the field and+ # suppress the workflow-level inheritance the other trigger paths get. Null means "omitted"+ # on the legacy body, so an unsupplied field has to arrive as null.+ proxy_location=(+ workflow_run_request.proxy_location if "proxy_location" in workflow_run_request.model_fields_set else None+ ),webhook_callback_url=workflow_run_request.webhook_url,totp_identifier=workflow_run_request.totp_identifier,totp_verification_url=workflow_run_request.totp_url,@@ -686,9 +693,13 @@ def _hydrate_run_request_for_response(A run that omits browser_type inherits the workflow's engine (resolved onto workflow_run atpersistence), so the returned request must report that engine rather than the request's omitted- null; the title is hydrated from the workflow when one exists.+ null; the title is hydrated from the workflow when one exists. A run that omits proxy_location+ inherits the workflow's region the same way, so the persisted value is echoed over the request's+ RESIDENTIAL default whenever one was resolved."""- updates: dict[str, str | None] = {"browser_type": read_browser_type(workflow_run)}+ updates: dict[str, ProxyLocationInput | str | None] = {"browser_type": read_browser_type(workflow_run)}+ if workflow_run.proxy_location is not None:+ updates["proxy_location"] = workflow_run.proxy_locationif workflow is not None:updates["title"] = workflow.titlereturn run_request.model_copy(update=updates)diff --git a/tests/unit/test_workflow_browser_profile_id.py b/tests/unit/test_workflow_browser_profile_id.pyindex 547208fdd..4a4767119 100644--- a/tests/unit/test_workflow_browser_profile_id.py+++ b/tests/unit/test_workflow_browser_profile_id.py@@ -12,10 +12,13 @@ import pytestfrom skyvern.exceptions import SkyvernHTTPException, WorkflowHasNoBlocksfrom skyvern.forge.sdk.core import skyvern_context+from skyvern.forge.sdk.routes.agent_protocol import _workflow_run_request_to_legacy_requestfrom skyvern.forge.sdk.workflow.models.block import ForLoopBlockfrom skyvern.forge.sdk.workflow.models.parameter import OutputParameterfrom skyvern.forge.sdk.workflow.models.workflow import Workflow, WorkflowDefinition, WorkflowRequestBodyfrom skyvern.forge.sdk.workflow.service import WorkflowService+from skyvern.schemas.proxy_location import ProxyLocation, ProxyLocationInput+from skyvern.schemas.runs import WorkflowRunRequestfrom skyvern.schemas.workflows import WorkflowCreateYAMLRequest, WorkflowDefinitionYAML, WorkflowStatus@@ -509,13 +512,14 @@ def _configure_setup_app_mocks(mock_app: Any) -> None:def _make_workflow_stub(browser_profile_id: str | None,max_elapsed_time_minutes: int | None = None,+ proxy_location: ProxyLocationInput = None,) -> SimpleNamespace:return SimpleNamespace(workflow_id="wf_test",workflow_permanent_id="wpid_test",organization_id="org_test",title="test",- proxy_location=None,+ proxy_location=proxy_location,webhook_callback_url=None,extra_http_headers=None,cdp_connect_headers=None,@@ -625,6 +629,57 @@ async def test_setup_workflow_run_session_present_skips_workflow_default() -> Noassert request.browser_profile_id is None+async def _setup_run_proxy_location(+ workflow_proxy_location: ProxyLocationInput, body: dict[str, Any]+) -> WorkflowRequestBody:+ """Run the public /run/agents body through the route conversion and setup_workflow_run.++ The body is validated the way FastAPI validates it so an omitted proxy_location carries the+ request model's RESIDENTIAL default, which is the state that used to shadow the workflow value.+ """+ workflow_stub = _make_workflow_stub(browser_profile_id=None, proxy_location=workflow_proxy_location)+ service, organization, _ = _make_setup_service(workflow_stub)+ request = _workflow_run_request_to_legacy_request(WorkflowRunRequest.model_validate(body))++ with patch("skyvern.forge.sdk.workflow.service.app") as mock_app:+ _configure_setup_app_mocks(mock_app)++ await service.setup_workflow_run(+ request_id="req_test",+ workflow_request=request,+ workflow_permanent_id="wpid_test",+ organization=organization,+ )++ return request+++async def test_run_without_proxy_location_inherits_the_workflow_region() -> None:+ request = await _setup_run_proxy_location(ProxyLocation.RESIDENTIAL_FR, {"workflow_id": "wpid_test"})++ assert request.proxy_location == ProxyLocation.RESIDENTIAL_FR+++async def test_run_level_proxy_location_overrides_the_workflow_region() -> None:+ request = await _setup_run_proxy_location(+ ProxyLocation.RESIDENTIAL_FR,+ {"workflow_id": "wpid_test", "proxy_location": ProxyLocation.RESIDENTIAL_IT.value},+ )++ assert request.proxy_location == ProxyLocation.RESIDENTIAL_IT+++async def test_run_without_proxy_location_on_a_regionless_workflow_stays_unset() -> None:+ # Nothing configured anywhere still resolves to the residential default at launch, via+ # runtime_proxy_location rather than a value baked onto the run.+ request = await _setup_run_proxy_location(None, {"workflow_id": "wpid_test"})++ assert request.proxy_location is None++@pytest.mark.asyncioasync def test_setup_workflow_run_prefers_request_max_elapsed_time_over_workflow_default() -> None:"""A run-level runtime cap should override the workflow default for that run snapshot."""