Skip to content

The agent that grows with you

fix(gateway): unchanged legacy route keeps the profile's credential for that endpoint

Tekniume61c0e5ef992
diff --git a/gateway/session_local_migration.py b/gateway/session_local_migration.py
index c171787fa38..9cd82e3c6c6 100644
--- a/gateway/session_local_migration.py
+++ b/gateway/session_local_migration.py
@@ -147,18 +147,22 @@ def adopt_legacy_session(authority, actor, row):
config = _load_gateway_config()
model_cfg = config.get('model')
if isinstance(model_cfg, dict) and ('provider' in params or 'base_url' in params):
- from hermes_cli.route_identity import normalize_route_base_url
- provider = params.get('provider', model_cfg.get('provider'))
- if (str(model_cfg.get('provider') or '').strip().lower() != str(provider or '').strip().lower()
- or normalize_route_base_url(model_cfg.get('base_url'))
- != normalize_route_base_url(params.get('base_url', model_cfg.get('base_url')))):
+ from gateway.session_route_identity import resolved_route, same_route
+ saved = {'provider': params.get('provider', model_cfg.get('provider')), 'base_url': params.get('base_url')}
+ # ``auto`` matches every route, so an ``auto`` side proves nothing about the saved endpoint.
+ if ('auto' not in (resolved_route(model_cfg, config)[0], resolved_route(saved, config)[0])
+ and same_route(saved, model_cfg, config)):
+ # The profile still selects this endpoint: resume on the profile's route, so its own
+ # credential (inline key, ${ENV} key, key_env) keeps its normal precedence.
+ params.pop('provider', None)
+ params.pop('base_url', None)
+ else:
# Another route than the profile's: its endpoint, wire mode, context pin and credential
# (inline key, key_env pointer) stay with the profile, never paired with the legacy route.
config['model'] = {k: v for k, v in model_cfg.items() if k not in (
'base_url', 'api_mode', 'context_length', 'api_key', 'api', 'key_env', 'api_key_env')}
- if params.get('base_url'):
- params['provider'] = _endpoint_owner(
- {'provider': provider, 'base_url': params['base_url']}, config)
+ if saved['base_url']:
+ params['provider'] = _endpoint_owner(saved, config)
policy = build_policy(params, config, private_secrets=private)
if api_mode:
config = json.loads(policy.config_json)
diff --git a/tests/gateway/test_legacy_session_route_turn.py b/tests/gateway/test_legacy_session_route_turn.py
index 8961d74e470..8151bedf864 100644
--- a/tests/gateway/test_legacy_session_route_turn.py
+++ b/tests/gateway/test_legacy_session_route_turn.py
@@ -163,3 +163,30 @@ def test_named_provider_moved_never_sends_its_new_key_to_the_saved_endpoint(monk
assert result['final_response'] == 'legacy ok'
assert {r['auth'] for r in legacy.requests} == {'Bearer sk-legacy-own'}
+
[email protected]('shape', ['inline', 'env-ref', 'cli-billing', 'ollama-key-env'])
+def test_unchanged_route_keeps_the_profile_credential_for_that_endpoint(monkeypatch, shape):
+ """The profile still selects exactly the saved endpoint, with a valid credential for it: the
+ adopted session keeps using it (literal key, ``${ENV}`` key, CLI billing row, or an ``ollama``
+ profile whose saved provider is its resolved ``custom``)."""
+ with FakeLLMServer([Text('legacy ok')], api_key='sk-profile-only') as legacy:
+ home = Path(os.environ['HERMES_HOME'])
+ monkeypatch.setattr('gateway.run._hermes_home', home)
+ monkeypatch.setenv('PROFILE_PROBE_KEY', 'sk-profile-only')
+ credential = {'inline': 'api_key: sk-profile-only', 'cli-billing': 'api_key: sk-profile-only',
+ 'env-ref': 'api_key: ${PROFILE_PROBE_KEY}', 'ollama-key-env': 'key_env: PROFILE_PROBE_KEY'}[shape]
+ home.mkdir(parents=True, exist_ok=True)
+ (home / 'config.yaml').write_text(
+ f'model:\n default: profile-model\n provider: {"ollama" if shape == "ollama-key-env" else "custom"}\n'
+ f' base_url: {legacy.base_url}\n {credential}\n'
+ 'agent:\n api_max_retries: 1\nplatform_toolsets:\n tui: []\n')
+ if shape == 'cli-billing':
+ resolved = _adopt_and_resolve(home, {}, source='cli', billing={
+ 'billing_provider': 'custom', 'billing_base_url': legacy.base_url})
+ else:
+ resolved = _adopt_and_resolve(home, {'provider': 'custom', 'base_url': legacy.base_url})
+ policy, (model, runtime) = resolved
+ result = _run_turn(policy, model, runtime)
+ assert result['final_response'] == 'legacy ok'
+ assert [r['model'] for r in legacy.main_requests()] == ['legacy-model']
+ assert {r['auth'] for r in legacy.requests} == {'Bearer sk-profile-only'}