Skip to content

The agent that grows with you

fix(delegate): child inherits the parent's live endpoint and key together

teknium152705fba6c89
diff --git a/contributors/emails/[email protected] b/contributors/emails/[email protected]
new file mode 100644
index 00000000000..0e78e2313ef
--- /dev/null
+++ b/contributors/emails/[email protected]
@@ -0,0 +1 @@
+lucvan
diff --git a/tests/tools/test_delegate.py b/tests/tools/test_delegate.py
index 1e583088dab..6fb57bff7ef 100644
--- a/tests/tools/test_delegate.py
+++ b/tests/tools/test_delegate.py
@@ -2285,7 +2285,11 @@ class TestFallbackModelInheritance(unittest.TestCase):
class TestAtomicChildCredentialBundle(unittest.TestCase):
- """provider/base_url reach the child as one bundle: all override or all parent."""
+ """provider/base_url/api_key reach the child as one bundle: all override, or all from the parent's live runtime.
+
+ #90009: a parent that flipped onto a fallback runtime handed the child the live endpoint paired with the
+ surface (stale) key — an instant 401 the child could never retry out of.
+ """
def _build(self, parent, **overrides):
with patch("run_agent.AIAgent") as MockAgent:
@@ -2303,22 +2307,16 @@ class TestAtomicChildCredentialBundle(unittest.TestCase):
self.assertIsNone(kwargs["base_url"])
self.assertNotEqual(kwargs["base_url"], parent.base_url)
- def test_provider_override_uses_its_own_endpoint(self):
- parent = _make_mock_parent(depth=0)
- kwargs = self._build(
- parent, override_provider="minimax", override_base_url="https://api.minimax.example/v1",
- override_api_key="sk-mm-x")
- self.assertEqual(kwargs["provider"], "minimax")
- self.assertEqual(kwargs["base_url"], "https://api.minimax.example/v1")
- self.assertEqual(kwargs["api_key"], "sk-mm-x")
-
- def test_no_override_inherits_whole_parent_bundle(self):
+ def test_no_override_inherits_live_endpoint_and_key_together(self):
parent = _make_mock_parent(depth=0)
- parent._client_kwargs = {}
- parent.client = None
+ parent.base_url = "https://fallback.example/v1"
+ parent.api_key = "FAKE-KEY-STALE-PRIMARY" # surface attribute lagging the live runtime
+ parent._client_kwargs = {"api_key": "FAKE-KEY-FALLBACK", "base_url": "https://fallback.example/v1/"}
+ parent.client = MagicMock(base_url="https://fallback.example/v1/", api_key="FAKE-KEY-FALLBACK")
kwargs = self._build(parent)
self.assertEqual(kwargs["provider"], parent.provider)
- self.assertEqual(kwargs["base_url"], parent.base_url)
+ self.assertEqual(kwargs["base_url"], "https://fallback.example/v1")
+ self.assertEqual(kwargs["api_key"], "FAKE-KEY-FALLBACK")
@patch("hermes_cli.runtime_provider.resolve_runtime_provider")
def test_provider_without_base_url_is_refused(self, mock_resolve):
@@ -2328,13 +2326,6 @@ class TestAtomicChildCredentialBundle(unittest.TestCase):
_resolve_delegation_credentials({"provider": "copilot", "model": "gpt-5"}, parent)
self.assertIn("without a base_url", str(ctx.exception))
- @patch("hermes_cli.runtime_provider.resolve_runtime_provider")
- def test_native_sdk_provider_without_base_url_is_allowed(self, mock_resolve):
- mock_resolve.return_value = {"provider": "bedrock", "base_url": "", "api_key": "aws", "api_mode": None}
- parent = _make_mock_parent(depth=0)
- creds = _resolve_delegation_credentials({"provider": "bedrock", "model": "claude"}, parent)
- self.assertEqual(creds["provider"], "bedrock")
-
if __name__ == "__main__":
unittest.main()
diff --git a/tools/delegate_tool_config.py b/tools/delegate_tool_config.py
index f4ff539ddf5..2306fba24f1 100644
--- a/tools/delegate_tool_config.py
+++ b/tools/delegate_tool_config.py
@@ -191,22 +191,25 @@ def _inherit_parent_capabilities(parent_agent, override_provider, override_base_
return None
return {key: value for key, value in parent_caps.items() if isinstance(key, str) and isinstance(value, bool)}
-def _inherit_parent_base_url(parent_agent, fallback_base_url: Optional[str]) -> Optional[str]:
- """Base URL the parent is actually calling (live client), not a stale attribute: ``parent_agent.base_url`` can lag
- the live client (old OpenRouter URL vs local Ollama) and inheriting the stale one 401s with a dummy/local key."""
- surface_url = _normalized_runtime_url(fallback_base_url)
+def _inherit_parent_endpoint(parent_agent, surface_base_url: Optional[str], surface_api_key: Any) -> tuple:
+ """``(base_url, api_key)`` the parent is actually calling, taken from ONE source. ``parent_agent.base_url`` /
+ ``api_key`` can lag the live client (old OpenRouter URL vs local Ollama; a fallback runtime the surface attributes
+ have not caught up with), and pairing the live endpoint with the surface key hands the child a (base_url, key)
+ pair that was never valid anywhere — an instant, non-retryable 401 (#90009). The live client's key travels with
+ its URL; the surface attributes are used only when there is no live OpenAI-wire client (native Anthropic/Bedrock
+ runtimes keep ``client=None``)."""
client_kwargs = getattr(parent_agent, "_client_kwargs", None)
client = getattr(parent_agent, "client", None)
live_candidates = (
- client_kwargs.get("base_url") if isinstance(client_kwargs, dict) else None,
+ (client_kwargs.get("base_url"), client_kwargs.get("api_key")) if isinstance(client_kwargs, dict) else (None, None),
# OpenAI SDK exposes base_url as httpx.URL — coerce before comparing.
- getattr(client, "base_url", "") if client is not None else None,
+ (getattr(client, "base_url", ""), getattr(client, "api_key", None)) if client is not None else (None, None),
)
- for raw in live_candidates:
- url = _normalized_runtime_url(raw)
- if url and url != surface_url and url.startswith(("http://", "https://")):
- return url
- return fallback_base_url or None
+ for raw_url, live_key in live_candidates:
+ url = _normalized_runtime_url(raw_url)
+ if url and url.startswith(("http://", "https://")):
+ return url, (live_key or surface_api_key)
+ return (surface_base_url or None), surface_api_key
def _loaded_pool(key: Any):
"""``load_pool(key)`` when it holds credentials, else None."""
@@ -486,14 +489,17 @@ def _resolve_child_runtime(
# provider/base_url are one bundle: all from the override, or all from the parent. Per-field fallback built
# children like an override provider pointed at the PARENT's endpoint (e.g. copilot credentials on the
# parent's Codex URL), which 404s on every request and can't be rescued by the fallback chain, whose dedup
- # matches provider+model and so skips the entry as a self-loop. _inherit_parent_base_url recovers the
- # parent's live endpoint, which is meaningless for a different provider.
+ # matches provider+model and so skips the entry as a self-loop. _inherit_parent_endpoint recovers the
+ # parent's live endpoint (with its key), which is meaningless for a different provider.
if override_provider:
effective_provider = override_provider
effective_base_url = override_base_url
+ elif override_base_url:
+ effective_provider = getattr(parent_agent, "provider", None)
+ effective_base_url = override_base_url
else:
effective_provider = getattr(parent_agent, "provider", None)
- effective_base_url = override_base_url or _inherit_parent_base_url(parent_agent, parent_agent.base_url)
+ effective_base_url, parent_api_key = _inherit_parent_endpoint(parent_agent, parent_agent.base_url, parent_api_key)
# api_mode: each provider has its own wire, so a different provider re-derives (None) instead of inheriting (404s
# otherwise). Nous Portal is dual-wire within one provider (anthropic/* → Messages, else chat_completions), so
# same-provider inheritance would pin the child on the wrong wire — re-derive.