From f80de6910f0f48130aea17c0c5a31e7635ac429e Mon Sep 17 00:00:00 2001 From: William Loo Date: Mon, 23 Nov 2026 15:40:45 -0800 Subject: [PATCH] feat(deploy-agent): point prefix-scoped keys at an env-configured endpoint Some gateways issue keys that only a dedicated inference endpoint accepts, not the default LiteLLM proxy. When a run is authed with such a key (recognised by an `ssk` prefix) and the deploy_agent step chose no endpoint of its own, point the agent's LITELLM_BASE_URL at the endpoint the deployment configures via SMIG_OPENAI_BASE_URL, so its model calls authenticate instead of failing the key check on the default proxy. - Per-agent: each deploy_agent step resolves its own endpoint in _model_endpoint, so one agent's choice never overrides another's. - Explicit choices always win: a per-run / step_params override, the step's litellm_base_url field, or a LITELLM_BASE_URL baked into the step env_vars. - The endpoint URL is supplied by the environment, never hard-coded; unset means no fallback (the platform default proxy is used, exactly as before). Co-Authored-By: Claude Opus 4.8 --- .../task_step/task_steps/deploy_agent.py | 36 +++++++++- .../test_deploy_agent_key_scoped_endpoint.py | 66 +++++++++++++++++++ 2 files changed, 100 insertions(+), 2 deletions(-) create mode 100644 tst/unit/task_step/test_deploy_agent_key_scoped_endpoint.py diff --git a/src/agent_env/task_step/task_steps/deploy_agent.py b/src/agent_env/task_step/task_steps/deploy_agent.py index a4d8065cc..150d39748 100644 --- a/src/agent_env/task_step/task_steps/deploy_agent.py +++ b/src/agent_env/task_step/task_steps/deploy_agent.py @@ -3,6 +3,7 @@ import asyncio import logging +import os from collections.abc import Iterable, Mapping from typing import ClassVar, Optional @@ -52,6 +53,26 @@ logger = logging.getLogger(__name__) +# Some gateways issue keys that only a dedicated inference endpoint accepts, not the default +# LiteLLM proxy. When a run is authed with such a key and the step chose no endpoint of its own, +# the agent is pointed at that endpoint so its model calls authenticate. The key is recognised by +# a prefix, and the endpoint URL is supplied by the deployment (``SMIG_OPENAI_BASE_URL``) rather +# than hard-coded; unset means "no fallback" — the platform's default proxy is used, as before. +_FALLBACK_KEY_PREFIX = "ssk" +_FALLBACK_BASE_URL_ENV = "SMIG_OPENAI_BASE_URL" + + +def _key_scoped_base_url(api_key: Optional[str]) -> Optional[str]: + """The LiteLLM base url a key's prefix forces when the step chose no endpoint, else None. + + Returns the deployment-configured endpoint (``_FALLBACK_BASE_URL_ENV``) only for a key whose + prefix marks it as endpoint-scoped; otherwise None, so the caller keeps the default proxy. The + URL is never hard-coded here — it comes from the environment — so this is safe to open-source. + """ + if not (api_key and api_key.strip().startswith(_FALLBACK_KEY_PREFIX)): + return None + return os.environ.get(_FALLBACK_BASE_URL_ENV) or None + def linked_sandbox_problem(sandbox_name: str, sandbox_mode: str, ports: Iterable[int | str]) -> str | None: """Why an agent can't be placed on the sandbox ``sandbox_name``, deployed in ``sandbox_mode`` with ``ports`` @@ -567,9 +588,20 @@ def _model_endpoint(self, context: TaskStepContext) -> dict[str, str]: step's.""" overrides = {**context.metadata.get("user_overrides", {}), **self.step_param_overrides(context)} endpoint = {} + api_key = None if overrides.get("litellm_api_key"): - endpoint["LITELLM_API_KEY"] = get_config().resolve_reference(overrides["litellm_api_key"]) - if base_url := overrides.get("litellm_base_url") or self.litellm_base_url: + api_key = get_config().resolve_reference(overrides["litellm_api_key"]) + endpoint["LITELLM_API_KEY"] = api_key + # Explicit endpoints win — step_params/run override, the step's field, or a base baked into + # the step's env_vars (which this dict is merged over) — then the run key's prefix selects + # one. Per-agent: each deploy_agent step resolves its own, so another agent's choice stands. + base_url = ( + overrides.get("litellm_base_url") + or self.litellm_base_url + or (self.env_vars or {}).get("LITELLM_BASE_URL") + or _key_scoped_base_url(api_key) + ) + if base_url: endpoint["LITELLM_BASE_URL"] = base_url return endpoint diff --git a/tst/unit/task_step/test_deploy_agent_key_scoped_endpoint.py b/tst/unit/task_step/test_deploy_agent_key_scoped_endpoint.py new file mode 100644 index 000000000..a8d09f7c7 --- /dev/null +++ b/tst/unit/task_step/test_deploy_agent_key_scoped_endpoint.py @@ -0,0 +1,66 @@ +"""An `ssk`-prefixed key points the agent at the deployment's inference endpoint when the step +chose no endpoint of its own. Explicit choices (step field, baked env_vars) always win, the URL +comes only from the environment (`SMIG_OPENAI_BASE_URL`), and resolution is per-agent.""" + +from __future__ import annotations + +from unittest.mock import MagicMock, patch + +from agent_env.task_step.context import TaskStepContext +from agent_env.task_step.task_steps import deploy_agent as da +from agent_env.task_step.task_steps.deploy_agent import DeployAgentTaskStep, _key_scoped_base_url + +EP = "https://endpoint.example/openai/v1" + + +def test_key_scoped_base_url(monkeypatch): + monkeypatch.delenv("SMIG_OPENAI_BASE_URL", raising=False) + assert _key_scoped_base_url("ssk_is_abc") is None # unset env -> no fallback, default proxy + monkeypatch.setenv("SMIG_OPENAI_BASE_URL", EP) + assert _key_scoped_base_url("ssk_is_abc") == EP + assert _key_scoped_base_url(" ssk_is_abc ") == EP # stripped before the prefix check + assert _key_scoped_base_url("sk-abc") is None + assert _key_scoped_base_url("") is None + assert _key_scoped_base_url(None) is None + + +def _endpoint(step: DeployAgentTaskStep, key) -> dict: + ctx = TaskStepContext(metadata={"user_overrides": {"litellm_api_key": key} if key else {}}) + cfg = MagicMock() + cfg.resolve_reference.side_effect = lambda v: v # a plain key resolves to itself + with patch.object(da, "get_config", return_value=cfg): + return step._model_endpoint(ctx) + + +def test_ssk_key_falls_back_to_env_endpoint(monkeypatch): + monkeypatch.setenv("SMIG_OPENAI_BASE_URL", EP) + step = DeployAgentTaskStep(id="a", version=None, env_ids=["e"]) + assert _endpoint(step, "ssk_is_abc")["LITELLM_BASE_URL"] == EP + + +def test_explicit_step_base_wins_over_fallback(monkeypatch): + monkeypatch.setenv("SMIG_OPENAI_BASE_URL", EP) + step = DeployAgentTaskStep( + id="a", version=None, env_ids=["e"], litellm_base_url="https://chosen.example/anthropic" + ) + assert _endpoint(step, "ssk_is_abc")["LITELLM_BASE_URL"] == "https://chosen.example/anthropic" + + +def test_env_vars_baked_base_wins_over_fallback(monkeypatch): + monkeypatch.setenv("SMIG_OPENAI_BASE_URL", EP) + step = DeployAgentTaskStep( + id="a", version=None, env_ids=["e"], env_vars={"LITELLM_BASE_URL": "https://baked.example"} + ) + assert _endpoint(step, "ssk_is_abc")["LITELLM_BASE_URL"] == "https://baked.example" + + +def test_non_ssk_key_keeps_default_proxy(monkeypatch): + monkeypatch.setenv("SMIG_OPENAI_BASE_URL", EP) + step = DeployAgentTaskStep(id="a", version=None, env_ids=["e"]) + assert "LITELLM_BASE_URL" not in _endpoint(step, "sk-platform") + + +def test_ssk_key_keeps_default_proxy_when_env_unset(monkeypatch): + monkeypatch.delenv("SMIG_OPENAI_BASE_URL", raising=False) + step = DeployAgentTaskStep(id="a", version=None, env_ids=["e"]) + assert "LITELLM_BASE_URL" not in _endpoint(step, "ssk_is_abc")