Skip to content

The agent that grows with you

fix(gateway): keep a following session's key_env on a pick at its current endpoint

Teknium5116a0355a8c
diff --git a/gateway/session_mutation_model.py b/gateway/session_mutation_model.py
index 6a1a0bd0934..20c7c94aa37 100644
--- a/gateway/session_mutation_model.py
+++ b/gateway/session_mutation_model.py
@@ -7,6 +7,9 @@ from typing import NamedTuple
from hermes_state_runtime import RuntimeStoreError
+_ROUTE_FIELDS = ('provider', 'base_url', 'key_env', 'api_key_env')
+
+
class _Route(NamedTuple):
provider: str
model: str
@@ -63,6 +66,12 @@ async def prepare_model(authority, live, payload, prepared):
if refusal is not None:
return refusal
frozen = old.config()
+ if old.follows_profile:
+ # A following session switches FROM the profile's current route: measured from its creation
+ # endpoint, a pick on the current one read as a route change and dropped its key_env pointer.
+ followed = config.get('model') or {}
+ frozen['model'] = {k: v for k, v in (frozen.get('model') or {}).items() if k not in _ROUTE_FIELDS}
+ frozen['model'].update({k: followed[k] for k in _ROUTE_FIELDS if k in followed})
# The one selection shape config.yaml gets (#25106): api_mode follows the target, a context pin
# and the endpoint-bound credential fields (inline key, key_env/api_key_env) drop on a route change.
# Off-loop: the context-pin check can do cold-start disk I/O.
diff --git a/tests/gateway/test_session_mutation_model_route.py b/tests/gateway/test_session_mutation_model_route.py
index af4051173b0..edec602fbe4 100644
--- a/tests/gateway/test_session_mutation_model_route.py
+++ b/tests/gateway/test_session_mutation_model_route.py
@@ -213,3 +213,35 @@ async def test_a_route_that_cannot_authenticate_does_not_block_switching_away(ho
finally:
await owner.close()
store.close_all_db_handles()
+
+
+async def test_a_following_pick_keeps_the_current_endpoints_key_env(home, endpoints):
+ """The profile names its key by ``key_env`` and moved from A to B before the pick. The pick lands
+ on B, so B's own key keeps authenticating the next turn: the pointer must not be dropped as a
+ route change measured from A, the endpoint the session was created on."""
+ (a, url_a), (b, url_b) = endpoints
+ (home / '.env').write_text('KEY_A=key-a\nKEY_B=key-b\n', encoding='utf-8')
+
+ def profile(url, var):
+ (home / 'config.yaml').write_text(json.dumps({'model': {
+ 'provider': 'custom', 'default': 'original', 'base_url': url, 'key_env': var}}), encoding='utf-8')
+ profile(url_a, 'KEY_A')
+ store, runner, authority, owner = _owner(home)
+ _bind_resolver(runner)
+ try:
+ live = await _create(owner, authority, home, 'follow-key-env')
+ sid = next(sid for sid, value in authority.sessions.items() if value is live)
+ profile(url_b, 'KEY_B')
+ assert _route(runner, live) == ('original', url_b, 'key-b')
+ a.received.clear()
+ b.received.clear()
+
+ response = await _pick(owner, authority, sid, {'model': 'picked', 'provider': 'custom'})
+
+ assert 'result' in response, response
+ assert a.received == [] and set(b.received) == {'Bearer key-b'}
+ assert _route(runner, live) == ('picked', url_b, 'key-b')
+ finally:
+ await owner.close()
+ store.close_all_db_handles()