Skip to content

The agent that grows with you

fix(gateway): drop an alias key pointer when the followed route moves

Teknium9e887be0a64e
diff --git a/gateway/session_mutation_model.py b/gateway/session_mutation_model.py
index 20c7c94aa37..b110f0fdb73 100644
--- a/gateway/session_mutation_model.py
+++ b/gateway/session_mutation_model.py
@@ -70,7 +70,12 @@ async def prepare_model(authority, live, payload, prepared):
# 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}
+ from gateway.session_policy import model_route
+ # An inline key (or an alias key's pointer) authenticates the route it was picked on: once the
+ # followed route moved, the same-route cleanup below would keep it for the new endpoint.
+ bound = () if model_route(frozen.get('model')) == model_route(followed) else ('api_key', 'api_key_from')
+ frozen['model'] = {k: v for k, v in (frozen.get('model') or {}).items()
+ if k not in _ROUTE_FIELDS + bound}
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.
diff --git a/tests/gateway/test_session_mutation_model_route.py b/tests/gateway/test_session_mutation_model_route.py
index edec602fbe4..4272c74d22b 100644
--- a/tests/gateway/test_session_mutation_model_route.py
+++ b/tests/gateway/test_session_mutation_model_route.py
@@ -63,10 +63,10 @@ def _configure(home, model, url, key):
'provider': 'custom', 'default': model, 'base_url': url, 'api_key': key}}), encoding='utf-8')
-async def _pick(owner, authority, sid, payload):
+async def _pick(owner, authority, sid, payload, request_id='model-pick'):
row = authority.db.get_session(sid)
return await owner.dispatch({'id': 'pick', 'method': 'session.mutate', 'params': {
- 'session_id': sid, 'request_id': 'model-pick', 'expected_revision': row['runtime_revision'],
+ 'session_id': sid, 'request_id': request_id, 'expected_revision': row['runtime_revision'],
'expected_generation': row['runtime_generation'], 'operation': 'model', 'payload': payload}})
@@ -245,3 +245,38 @@ async def test_a_following_pick_keeps_the_current_endpoints_key_env(home, endpoi
finally:
await owner.close()
store.close_all_db_handles()
+
+
+async def test_a_superseded_alias_key_never_reaches_the_next_picks_endpoint(home, endpoints):
+ """A following session picks a literal-key alias on A; the profile then moves to B and the user
+ picks another model there. The alias's key pointer is bound to A's route: it must not survive
+ the pick and authenticate B's next turn."""
+ (_, url_a), (b, url_b) = endpoints
+
+ def profile(model, url, key):
+ (home / 'config.yaml').write_text(json.dumps({
+ 'model': {'provider': 'custom', 'default': model, 'base_url': url, 'api_key': key},
+ 'model_aliases': {'keyed': {'model': 'current', 'provider': 'custom',
+ 'base_url': url_a, 'api_key': 'key-alias'}},
+ }), encoding='utf-8')
+ profile('original', url_a, 'key-a')
+ store, runner, authority, owner = _owner(home)
+ _bind_resolver(runner)
+ try:
+ live = await _create(owner, authority, home, 'follow-alias')
+ sid = next(sid for sid, value in authority.sessions.items() if value is live)
+ assert 'result' in await _pick(owner, authority, sid, {'model': 'keyed'}, 'alias-pick')
+ assert _route(runner, live) == ('current', url_a, 'key-alias')
+ profile('current', url_b, 'key-b') # a new profile target supersedes the alias pick
+ assert _route(runner, live) == ('current', url_b, 'key-b')
+ b.received.clear()
+
+ response = await _pick(owner, authority, sid, {'model': 'picked', 'provider': 'custom'})
+
+ assert 'result' in response, response
+ assert _route(runner, live) == ('picked', url_b, 'key-b')
+ assert 'Bearer key-alias' not in b.received
+ finally:
+ await owner.close()
+ store.close_all_db_handles()