The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): a moved named provider's key never goes to its old saved endpoint
Teknium410a8fad2eae
diff --git a/gateway/session_local_migration.py b/gateway/session_local_migration.pyindex 7fc12125339..c171787fa38 100644--- a/gateway/session_local_migration.py+++ b/gateway/session_local_migration.py@@ -105,6 +105,25 @@ def legacy_launch_params(row):return params, route.get('api_mode')+def _endpoint_owner(saved, config):+ """The provider whose credential may serve the saved endpoint. A named entry that now points+ elsewhere owns a credential for THAT host: rebind to the entry still serving the saved URL, or+ refuse by name rather than send the moved entry's key to the old host."""+ from gateway.session_route_identity import resolved_route, same_route+ identity, entry_url = resolved_route({'provider': saved['provider']}, config)+ if not entry_url or same_route(saved, {'provider': identity, 'base_url': entry_url}, config):+ return saved['provider']+ names = list(config.get('providers') or {}) if isinstance(config.get('providers'), dict) else []+ legacy = config.get('custom_providers')+ names += [e.get('name') for e in legacy if isinstance(e, dict)] if isinstance(legacy, list) else []+ for name in names:+ owner, url = resolved_route({'provider': str(name or '')}, config)+ if url and same_route(saved, {'provider': owner, 'base_url': url}, config):+ return owner+ raise RuntimeStoreError('launch_credentials_unavailable', field='provider',+ detail=f'{identity} no longer serves the saved endpoint; select a model to continue')++def adopt_legacy_session(authority, actor, row):if actor.profile_id != authority.profile_id:raise RuntimeStoreError('profile_mismatch')@@ -137,6 +156,9 @@ def adopt_legacy_session(authority, actor, row):# (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)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.pyindex 62db8ca143b..8961d74e470 100644--- a/tests/gateway/test_legacy_session_route_turn.py+++ b/tests/gateway/test_legacy_session_route_turn.py@@ -127,3 +127,39 @@ def test_adopted_legacy_session_whose_provider_lost_credentials_is_refused_by_na_adopt_and_resolve(home, {'provider': 'custom:legacy-box', 'base_url': 'http://127.0.0.1:9/v1'})assert 'custom:legacy-box' in str(caught.value)assert profile.requests == []+++def _run_turn(policy, model, runtime):+ from run_agent import AIAgent+ agent = AIAgent(model=model, **{k: runtime[k] for k in ('api_key', 'base_url', 'provider', 'api_mode')},+ reasoning_config=policy.reasoning_config, quiet_mode=True, skip_context_files=True,+ skip_memory=True, enabled_toolsets=['memory'])+ return agent.run_conversation('hello again')++[email protected]('owner', ['recovered', 'gone'])+def test_named_provider_moved_never_sends_its_new_key_to_the_saved_endpoint(monkeypatch, owner):+ """The session saved ``custom:legacy-box`` at B; ``legacy-box`` now serves A with A's key. A's key+ belongs to A: B gets the credential of the entry that still serves B, or adoption is refused."""+ with FakeLLMServer([Text('new')]) as new, FakeLLMServer([Text('legacy ok')], api_key='sk-legacy-own') as legacy:+ home = Path(os.environ['HERMES_HOME'])+ monkeypatch.setattr('gateway.run._hermes_home', home)+ _write_profile(home, new.base_url, extra=(+ f'providers:\n legacy-box:\n base_url: {new.base_url}\n api_key: sk-new-endpoint-only\n'+ + (f'custom_providers:\n - name: old-box\n base_url: {legacy.base_url}\n api_key: sk-legacy-own\n'+ if owner == 'recovered' else '')))+ saved = {'provider': 'custom:legacy-box', 'base_url': legacy.base_url}+ if owner == 'recovered':+ policy, (model, runtime) = _adopt_and_resolve(home, saved)+ result = _run_turn(policy, model, runtime)+ else:+ with pytest.raises(Exception) as caught:+ _adopt_and_resolve(home, saved)+ assert getattr(caught.value, 'reason', None) == 'launch_credentials_unavailable'+ assert 'custom:legacy-box' in str(caught.value.detail)+ assert new.requests == []+ assert all('sk-new-endpoint-only' not in str(r['headers']) for r in legacy.requests)+ if owner == 'recovered':+ assert result['final_response'] == 'legacy ok'+ assert {r['auth'] for r in legacy.requests} == {'Bearer sk-legacy-own'}+