The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix: mid-run credential rotation never rebinds a session to an entry for another endpoint
teknium1c7d0045a8d45
diff --git a/agent/agent_runtime_helpers.py b/agent/agent_runtime_helpers.pyindex 093c9d53383..a1c2028e840 100644--- a/agent/agent_runtime_helpers.py+++ b/agent/agent_runtime_helpers.py@@ -24,7 +24,8 @@ from agent.tool_dispatch_helpers import _trajectory_normalize_msg, make_tool_resfrom agent.think_scrubber import THINK_TAG_NAMESfrom agent.trajectory import convert_scratchpad_to_thinkfrom agent.credential_pool import (- STATUS_EXHAUSTED, credential_pool_matches_provider, resolve_runtime_pool_key+ STATUS_EXHAUSTED, credential_pool_entry_serves_endpoint, credential_pool_matches_provider,+ resolve_runtime_pool_key,)from agent.error_classifier import FailoverReasonfrom agent.retry_utils import parse_retry_after_seconds, reset_delay_from_message@@ -851,6 +852,14 @@ def recover_with_credential_pool(next_entry = pool.mark_exhausted_and_rotate(**kwargs)if next_entry is None:return False+ if not credential_pool_entry_serves_endpoint(next_entry, getattr(agent, "base_url", None)):+ # Mixed same-provider pool (#68237): the entry serves another endpoint and _swap_credential+ # would rebind this session to it. Treat as no recovery, like a rotation that yields nothing.+ _ra().logger.info(+ "Credential %s (%s) — pool entry %s serves another endpoint; not swapping",+ rotate_status, label, getattr(next_entry, "id", "?"),+ )+ return False_ra().logger.info("Credential %s (%s) — rotated to pool entry %s",rotate_status, label, getattr(next_entry, "id", "?"),diff --git a/agent/credential_pool.py b/agent/credential_pool.pyindex 4eee11c0e39..852ab636614 100644--- a/agent/credential_pool.py+++ b/agent/credential_pool.py@@ -641,6 +641,20 @@ def _legacy_custom_pool_matches(return False+def credential_pool_entry_serves_endpoint(entry: Any, base_url: Any) -> bool:+ """Whether a pooled credential may be bound to a session running at ``base_url``. ``_swap_credential``+ adopts the entry's base_url too, so a same-provider entry for another endpoint (public OpenAI vs. an+ Azure resource) would send the session's requests — and the entry's key — to the wrong host (#68237).+ Entries or sessions without endpoint metadata (legacy adapters, test doubles) cannot rebind and are accepted."""+ if not isinstance(base_url, str) or not base_url:+ return True+ entry_url = getattr(entry, "runtime_base_url", None) or getattr(entry, "base_url", None)+ if not isinstance(entry_url, str) or not entry_url:+ return True+ from hermes_cli.route_identity import normalize_route_base_url+ return normalize_route_base_url(entry_url) == normalize_route_base_url(base_url)++def credential_pool_matches_provider(pool_or_provider: Any,provider: Optional[str],diff --git a/tests/agent/test_pool_rotation_endpoint_veto.py b/tests/agent/test_pool_rotation_endpoint_veto.pynew file mode 100644index 00000000000..ea5d48324c2--- /dev/null+++ b/tests/agent/test_pool_rotation_endpoint_veto.py@@ -0,0 +1,56 @@+# Copyright 2025 Nous Research (Licensed under the Apache License, Version 2.0)+"""Mid-run credential rotation never rebinds a session to a same-provider entry for another endpoint.++A mixed ``openai`` pool (public api.openai.com key + Azure resource key) is legitimately shared with+an Azure-bound child (#68237). ``_swap_credential`` adopts the entry's base_url, so rotating the+Azure session onto the public entry on a 429 would send its traffic — and the public key — to the+wrong host. Rotation must be vetoed exactly like a rotation that yields nothing.+"""++from agent.agent_runtime_helpers import recover_with_credential_pool+from agent.credential_pool import CredentialPool, PooledCredential++_AZURE = "https://res.cognitiveservices.azure.com/openai/v1"+_PUBLIC = "https://api.openai.com/v1"+++def _entry(eid, url):+ return PooledCredential(provider="openai", id=eid, label=eid, auth_type="api_key", priority=0,+ source=f"env:{eid}", access_token=f"key-{eid}", base_url=url)+++class _AzureChild:+ """Session bound to the Azure entry of a mixed pool; real pool + real recovery helper."""++ provider = "openai"+ model = "gpt-5.4"+ base_url = _AZURE+ _fallback_activated = False++ def __init__(self, pool):+ self._credential_pool = pool+ self._credential_pool_entry_id = "az"+ self.api_key = "key-az"++ def _swap_credential(self, entry):+ self.api_key = entry.runtime_api_key+ self.base_url = entry.base_url+ self._credential_pool_entry_id = entry.id+ return True++ def _is_entitlement_failure(self, error_context, status_code):+ return False+++def test_rotation_on_mixed_pool_never_rebinds_to_an_entry_for_another_endpoint():+ pool = CredentialPool("openai", [_entry("az", _AZURE), _entry("pub", _PUBLIC)])+ agent = _AzureChild(pool)++ recovered, _ = recover_with_credential_pool(+ agent, status_code=429, has_retried_429=True, error_context={"message": "Rate limit"},+ )++ assert recovered is False+ assert (agent.api_key, agent.base_url, agent._credential_pool_entry_id) == ("key-az", _AZURE, "az")+ # The failed Azure entry is still benched; only the swap onto the wrong host was refused.+ assert next(e for e in pool.entries() if e.id == "az").last_status == "exhausted"diff --git a/tools/delegate_tool_config.py b/tools/delegate_tool_config.pyindex 1c6b8ae1e92..c7179176a8f 100644--- a/tools/delegate_tool_config.py+++ b/tools/delegate_tool_config.py@@ -214,22 +214,11 @@ def _loaded_pool(key: Any):pool = load_pool(key)return pool if pool is not None and pool.has_credentials() else None-def _entry_serves_endpoint(entry: Any, base_url: Any) -> bool:- """Whether a pooled credential may be bound to a child running at ``base_url``. ``_swap_credential`` adopts the- entry's base_url too, so a same-provider entry for another endpoint (public OpenAI vs. an Azure resource) would- send the child's request — and the entry's key — to the wrong host (#68237). Entries or children without- endpoint metadata (legacy adapters, test doubles) cannot rebind and are accepted."""- if not isinstance(base_url, str) or not base_url:- return True- entry_url = getattr(entry, "runtime_base_url", None) or getattr(entry, "base_url", None)- if not isinstance(entry_url, str) or not entry_url:- return True- from hermes_cli.route_identity import normalize_route_base_url- return normalize_route_base_url(entry_url) == normalize_route_base_url(base_url)-def _pool_serves_endpoint(pool: Any, provider: Optional[str], base_url: Optional[str]) -> bool:"""Provider identity AND at least one entry for the child's endpoint; pools without entry metadata pass."""- from agent.credential_pool import credential_pool_matches_provider+ from agent.credential_pool import (+ credential_pool_entry_serves_endpoint as _entry_serves_endpoint, credential_pool_matches_provider,+ )if not credential_pool_matches_provider(pool, provider, base_url=base_url):return Falseentries_fn = getattr(pool, "entries", None)