The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): bind a launch key to its resolved route, not provider spelling
Tekniumc473d338dc33
diff --git a/gateway/session_mutation_model.py b/gateway/session_mutation_model.pyindex b110f0fdb73..48ba13f14ef 100644--- a/gateway/session_mutation_model.py+++ b/gateway/session_mutation_model.py@@ -45,9 +45,9 @@ def _current_route(authority, live, old):async def prepare_model(authority, live, payload, prepared):- from gateway.session_policy import restore_policy, launch_key, bind_launch_key+ from gateway.session_policy import restore_policy, bind_launch_key, bound_route_holds, route_launch_keyfrom gateway.session_policy_credentials import recover_config_secrets- from hermes_cli.model_switch import apply_model_selection, selection_route_changed, switch_model+ from hermes_cli.model_switch import apply_model_selection, switch_modelfrom hermes_cli.config import get_compatible_custom_providersold = restore_policy(prepared['snapshot']['receipt']['policy'])config, current = _current_route(authority, live, old)@@ -70,10 +70,10 @@ 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 {}- 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')+ held = bound_route_holds(frozen.get('model'), frozen, followed, config)+ bound = () if held 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})@@ -113,8 +113,11 @@ async def prepare_model(authority, live, payload, prepared):# Re-fingerprint frozen config references because policy identity includes model.# An explicit launch key belongs to the endpoint it authenticated, not to a provider name:# custom -> custom on another base_url must not carry it (same route identity as config keys).- key = launch_key(authority, old)- if selection_route_changed({'provider': old.provider, 'base_url': old.base_url or current.base_url}, result):+ # Measured on resolved routes (a respelled provider on one endpoint is one route; a named entry+ # whose base_url moved is another): the key survives only while it still authenticates the route+ # the session runs now and the pick stays on that route.+ key = route_launch_key(authority, old, config)+ if key is None or not bound_route_holds(config.get('model'), config, frozen['model'], config):policy = replace(policy, credential_ref=None)key = Nonepolicy = bind_launch_key(authority, prepared['snapshot']['receipt']['session_id'], policy, key,diff --git a/gateway/session_policy.py b/gateway/session_policy.pyindex 03b329d7a22..abb7dee5f97 100644--- a/gateway/session_policy.py+++ b/gateway/session_policy.py@@ -248,6 +248,19 @@ def model_route(block):return str(block.get('provider') or '').strip().lower(), normalize_route_base_url(block.get('base_url'))+def bound_route_holds(bound, bound_config, block, config):+ """True when *block* (resolved in *config*) is the route *bound* was bound to, *bound* resolved in+ the config it was bound in: a respelled provider on one endpoint is one route, and a named entry+ whose ``base_url`` moved is another. An ``auto``/unset side keeps the literal comparison: a+ credential never follows a route the resolver picks later."""+ from gateway.session_route_identity import resolved_route, same_route+ bound = bound if isinstance(bound, dict) else {}+ identity, url = resolved_route(bound, bound_config)+ if 'auto' in (identity, resolved_route(block, config)[0]):+ return model_route(bound) == model_route(block)+ return same_route({**bound, 'base_url': url} if url else bound, block, config)++def _held_pick(frozen, live):"""A pick that still holds keeps its selection; on the profile's own endpoint the rest of the``model:`` block (a rotated key included) is the profile's. Another endpoint never gets it."""@@ -699,7 +712,8 @@ def route_launch_key(authority, policy, config):"""The launch key a turn on *config* (the route it runs, a followed profile's included) sends.A launch key authenticates the frozen route it was bound with: once the followed profilesupersedes that pick and moves to another endpoint, the key stays behind with its route."""- if model_route(policy.config().get('model')) != model_route(config.get('model')):+ bound = present_sections(json.loads(policy.config_json)) # the route the key was bound with+ if not bound_route_holds(bound.get('model'), bound, config.get('model'), config):return Nonereturn launch_key(authority, policy)diff --git a/tests/gateway/test_session_mutation_model_route.py b/tests/gateway/test_session_mutation_model_route.pyindex 4272c74d22b..f42d53a9088 100644--- a/tests/gateway/test_session_mutation_model_route.py+++ b/tests/gateway/test_session_mutation_model_route.py@@ -280,3 +280,49 @@ async def test_a_superseded_alias_key_never_reaches_the_next_picks_endpoint(homefinally:await owner.close()store.close_all_db_handles()+++async def _launch(owner, home, provider, key):+ created = await owner.dispatch({'id': 'create', 'method': 'session.create', 'params': {+ 'request_id': 'launch', 'source': 'cli', 'cwd': str(home), 'toolsets': [],+ 'provider': provider, 'api_key': key, 'follow_profile_config': True}})+ assert 'result' in created, created+ return created['result']['session_id']++[email protected]('before,after', [('custom', 'vllm'), ('team', 'custom:team')])+async def test_a_respelled_provider_on_the_same_endpoint_keeps_the_launch_key(home, endpoints, before, after):+ """The profile respells its provider (``custom`` -> ``vllm``, ``team`` -> ``custom:team``) without+ moving the endpoint. The route is the same, so the key the session was launched with still+ authenticates it; the profile's own key must not replace it."""+ (a, url), (b, _) = endpoints++ def profile(provider):+ model = {'provider': provider, 'default': 'original'}+ if before == 'custom':+ model.update(base_url=url, api_key='key-profile')+ (home / 'config.yaml').write_text(json.dumps({+ 'model': model, 'providers': {'team': {'name': 'team', 'base_url': url, 'api_key': 'key-profile'}},+ }), encoding='utf-8')+ profile(before)+ store, runner, authority, owner = _owner(home)+ _bind_resolver(runner)+ try:+ sid = await _launch(owner, home, before, 'key-launch')+ live = authority.sessions[sid]+ profile(after)+ assert _route(runner, live) == ('original', url, 'key-launch')+ a.received.clear()++ response = await _pick(owner, authority, sid, {'model': 'picked'})++ assert 'result' in response, response+ # The pick's catalog probe uses a credential of this endpoint; the other server gets nothing.+ assert a.received and set(a.received) <= {'Bearer key-launch', 'Bearer key-profile'}+ assert b.received == []+ assert _route(runner, live) == ('picked', url, 'key-launch')+ finally:+ await owner.close()+ store.close_all_db_handles()+