Skip to content

The agent that grows with you

fix(gateway): build a managed worker's agent on its resolved route

unsupportedpastels49d996faea07
diff --git a/agent/managed_worker.py b/agent/managed_worker.py
index b49a97acd0c..0643ec1b503 100644
--- a/agent/managed_worker.py
+++ b/agent/managed_worker.py
@@ -431,10 +431,8 @@ def _construct_agent(frame, factory, policy, **kwargs):
"""Agent construction resolves the provider credentials; a Kanban attempt that fails here
still books the one-shot exit mapping (quota wall 75, re-login 78) before the crash."""
try:
- if kwargs['api_key'] is None:
- from gateway.session_worker_construct import worker_api_key
- kwargs['api_key'] = worker_api_key(policy, kwargs)
- return factory(**kwargs)
+ from gateway.session_worker_construct import worker_route
+ return factory(**{**kwargs, **worker_route(policy, kwargs)})
except Exception as exc:
from gateway.session_kanban import record_start_failure
record_start_failure(frame, exc)
diff --git a/gateway/session_worker_construct.py b/gateway/session_worker_construct.py
index efff49a7d7a..7dae30b1c0b 100644
--- a/gateway/session_worker_construct.py
+++ b/gateway/session_worker_construct.py
@@ -151,7 +151,7 @@ def route_runtime(policy, frozen, model, key, *, fallback=True):
"""``(runtime, fallback_entry)`` for a launch-policy route: the session's FROZEN config and launch
credential, walking its own fallback chain on an AuthError unless ``fallback=False`` (a session
control asking which route the session itself runs). The one resolver an in-process turn
- (``_resolve_policy_agent_runtime``) and a key-less managed worker (``worker_api_key``) share."""
+ (``_resolve_policy_agent_runtime``) and a managed worker (``worker_route``) share."""
from hermes_cli.runtime_provider import (
frozen_runtime_config, resolve_runtime_provider, resolve_runtime_with_fallback,
)
@@ -189,11 +189,14 @@ def route_runtime(policy, frozen, model, key, *, fallback=True):
explicit_api_key=key, explicit_base_url=launch_url, target_model=model)
-def worker_api_key(policy, construct):
- """Child side, for a worker launched without a key (every Kanban run): the route's own credential,
- resolved as the in-process turn resolves it, from this process's profile home (its HERMES_HOME and
- ``.env``). A fallback entry's key belongs to another provider and model, so the primary never
- carries it; the agent's own fallback chain switches all three together (#139155).
+def worker_route(policy, construct):
+ """Child side: the ``api_key`` / ``base_url`` / ``api_mode`` overrides for the worker's agent, resolved
+ as the in-process turn resolves its route (``route_runtime`` with the launch key, if one was bound),
+ from this process's profile home (its HERMES_HOME and ``.env``). The frozen ``model.base_url`` alone
+ is not the route: a per-model api_mode (opencode-go Responses models) and a resolved endpoint
+ (Bedrock's regional URL) come only from resolution. A fallback entry's route belongs to another
+ provider and model, so the primary never carries it; the agent's own fallback chain switches them
+ together (#139155).
A primary AuthError walks the chain the agent will actually get: the owner's per-turn
``construct_v1`` chain (an ordinary session's refreshed profile fallbacks) when it was sent,
@@ -201,8 +204,11 @@ def worker_api_key(policy, construct):
frozen, model = policy.effective()
if 'fallback_model' in construct:
frozen = {**frozen, 'fallback_providers': construct['fallback_model'] or [], 'fallback_model': None}
- runtime, fallback_entry = route_runtime(policy, frozen, model, None)
- return runtime.get('api_key') if fallback_entry is None else None
+ runtime, fallback_entry = route_runtime(policy, frozen, model, construct['api_key'])
+ if fallback_entry is not None:
+ return {}
+ return {'api_key': construct['api_key'] or runtime.get('api_key'), 'base_url': runtime.get('base_url'),
+ 'api_mode': runtime.get('api_mode')}
def checkpoint_agent_kwargs(config):
diff --git a/tests/gateway/test_managed_worker_route_key.py b/tests/gateway/test_managed_worker_route_key.py
index ee7c47f1df8..d864505e0d3 100644
--- a/tests/gateway/test_managed_worker_route_key.py
+++ b/tests/gateway/test_managed_worker_route_key.py
@@ -108,6 +108,28 @@ def test_bound_launch_key_wins_over_the_route_key(tmp_path, monkeypatch):
assert built['api_key'] == launch
[email protected]('model, block, env, expected', [
+ # opencode-go serves this model only on its Responses API (#141861).
+ ('muse-spark-1.3-contributor', {'provider': 'opencode-go', 'base_url': 'https://opencode.ai/zen/go/v1'},
+ {'OPENCODE_GO_API_KEY': 'fake-opencode-go-key'},
+ {'api_mode': 'codex_responses', 'base_url': 'https://opencode.ai/zen/go/v1'}),
+ # Bedrock stores no base_url: the runtime builds the regional endpoint (#143073).
+ ('eu.amazon.nova-pro-v1:0', {'provider': 'bedrock'}, {'AWS_REGION': 'eu-west-1'},
+ {'api_mode': 'bedrock_converse', 'base_url': 'https://bedrock-runtime.eu-west-1.amazonaws.com'}),
+])
+def test_worker_builds_on_the_routes_resolved_api_mode_and_endpoint(tmp_path, monkeypatch, model, block, env, expected):
+ """KB-11: the worker's agent gets the api_mode and endpoint the in-process turn's resolution
+ gives it, not the frozen ``model.base_url`` with no api_mode (chat_completions, us-east-1)."""
+ frame, _ = assignment(tmp_path, monkeypatch)
+ for name, value in env.items():
+ monkeypatch.setenv(name, value)
+ config = {**json.loads(frame['policy']['config_json']), 'model': {'default': model, **block}}
+ request = {k: v for k, v in json.loads(frame['policy']['request_json']).items() if k not in ('provider', 'base_url')}
+ policy = {**frame['policy'], 'model': model, 'config_json': json.dumps(config), 'request_json': json.dumps(request)}
+ built = _execute({**frame, 'policy': policy, 'api_key': None}, monkeypatch)
+ assert {key: built.get(key) for key in expected} == expected
+
+
def test_primary_auth_failure_never_borrows_a_fallback_providers_key(tmp_path, monkeypatch):
"""A fallback entry's credential belongs to another provider; the worker's primary route never
carries it (the agent's own fallback chain switches provider, model and key together)."""