Skip to content

The agent that grows with you

fix(worker): resolve a key-less worker's primary failure against this turn's fallback chain

unsupportedpastelsd4a220edaaf0
diff --git a/agent/managed_worker.py b/agent/managed_worker.py
index 01a1d6088c2..3c2511d5091 100644
--- a/agent/managed_worker.py
+++ b/agent/managed_worker.py
@@ -405,7 +405,7 @@ def _construct_agent(frame, factory, policy, **kwargs):
try:
if kwargs['api_key'] is None:
from gateway.session_worker_construct import worker_api_key
- kwargs['api_key'] = worker_api_key(policy)
+ kwargs['api_key'] = worker_api_key(policy, kwargs)
return factory(**kwargs)
except Exception as exc:
from gateway.session_kanban import record_start_failure
diff --git a/gateway/session_worker_construct.py b/gateway/session_worker_construct.py
index 8014bb9bdfe..ff353fdd2f4 100644
--- a/gateway/session_worker_construct.py
+++ b/gateway/session_worker_construct.py
@@ -105,12 +105,18 @@ def route_runtime(policy, frozen, model, key):
explicit_api_key=key, explicit_base_url=launch_url, target_model=model)
-def worker_api_key(policy):
+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)."""
+ carries it; the agent's own fallback chain switches all three 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,
+ the frozen snapshot's otherwise (older owner)."""
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
diff --git a/tests/gateway/test_managed_worker_route_key.py b/tests/gateway/test_managed_worker_route_key.py
index 7914f0022af..333997db04c 100644
--- a/tests/gateway/test_managed_worker_route_key.py
+++ b/tests/gateway/test_managed_worker_route_key.py
@@ -152,6 +152,58 @@ def test_keyless_worker_credential_failure_keeps_the_kanban_start_exit(tmp_path,
assert session_kanban.worker_exit_code(path, context) == expected
+FALLBACK = {'provider': 'openrouter', 'model': 'fallback-model'}
+
+
+def _with_turn_chain(frame, chain):
+ """The owner's per-turn ``construct_v1``: an ordinary session's refreshed profile chain, which the
+ frozen snapshot (no fallbacks here) does not carry."""
+ request = json.loads(frame['policy']['request_json'])
+ request['construct_v1'] = {'fallback_model': chain, 'ephemeral_system_prompt': None, 'prefill_messages': None}
+ return {**frame, 'policy': {**frame['policy'], 'request_json': json.dumps(request)}}
+
+
+def _primary_auth_failure(monkeypatch, fallback_ok):
+ from hermes_cli.auth import AuthError
+ calls = []
+
+ def resolve(requested=None, **kwargs):
+ calls.append(requested)
+ if requested == FALLBACK['provider'] and fallback_ok:
+ return {'provider': 'openrouter', 'api_key': 'fake-fallback', 'base_url': 'https://openrouter.ai/api/v1'}
+ raise AuthError('relogin', provider='nous', relogin_required=True)
+ monkeypatch.setattr('hermes_cli.runtime_provider_custom._resolve_named_custom_runtime', lambda **kw: None)
+ monkeypatch.setattr('hermes_cli.runtime_provider.resolve_runtime_provider', resolve)
+ return calls
+
+
+def test_primary_auth_failure_reaches_the_turns_refreshed_fallback_chain(tmp_path, monkeypatch):
+ """An expired primary with a fallback the user added after session creation: the worker resolves
+ against the chain supplied for this turn, so the agent is built (key-less, its own chain switches
+ provider, model and key together) instead of a start failure."""
+ calls = _primary_auth_failure(monkeypatch, fallback_ok=True)
+ built = _execute(_with_turn_chain(_minimax_frame(tmp_path, monkeypatch), [FALLBACK]), monkeypatch)
+ assert FALLBACK['provider'] in calls
+ assert built['fallback_model'] == [FALLBACK] and built['api_key'] is None
+
+
+def test_primary_auth_failure_with_an_unusable_turn_chain_keeps_the_kanban_start_exit(tmp_path, monkeypatch):
+ from gateway import session_kanban
+ from hermes_cli import kanban_db as kb
+ from hermes_cli.auth import AuthError
+ from tests.gateway.test_kanban_result_exit import _bound_claim
+ path, context = _bound_claim(tmp_path / 'kanban', monkeypatch)
+ frame = _with_turn_chain(_minimax_frame(tmp_path, monkeypatch), [FALLBACK])
+ frame = {**frame, 'policy': {**frame['policy'], 'kanban_json': json.dumps(context)}}
+ _primary_auth_failure(monkeypatch, fallback_ok=False)
+ monkeypatch.setattr(session_kanban, 'bind_worker_context', lambda frame: None)
+ built = {}
+ with pytest.raises(AuthError):
+ _execute(frame, monkeypatch, built=built)
+ assert built == {}
+ assert session_kanban.worker_exit_code(path, context) == kb.KANBAN_TERMINAL_PROVIDER_EXIT_CODE
+
+
def test_keyless_worker_on_a_fallback_never_sends_its_key_to_the_primary_endpoint(tmp_path, monkeypatch):
"""Resolution landing on a configured fallback: the real agent's primary (MiniMax, Anthropic
protocol) client is built without the fallback provider's key. Asserted where the HTTP client