Skip to content

The agent that grows with you

fix(gateway): changing provider works for sessions launched with --api-key and config secrets

Teknium5d8f48c8744f
diff --git a/gateway/session_policy.py b/gateway/session_policy.py
index 7f44d28fba5..b2a66cee138 100644
--- a/gateway/session_policy.py
+++ b/gateway/session_policy.py
@@ -229,7 +229,9 @@ def bind_launch_key(authority, session_id, policy, api_key, *, config_secrets=No
keys = authority._local_launch_keys = {}
ref = f'{authority.instance_id}:{authority.epoch}:{session_id}'
old = keys.get(ref)
- if old is not None and (api_key is None or not hmac.compare_digest(old, api_key)):
+ # Only a key that IS being bound can conflict: binding config secrets alone (a provider
+ # change drops the launch key) must not be refused by the key the session already holds.
+ if api_key is not None and old is not None and not hmac.compare_digest(old, api_key):
raise RuntimeStoreError('admission_conflict')
configs = getattr(authority, '_local_config_secrets', None)
if configs is None:
diff --git a/tests/gateway/test_session_mutation_model.py b/tests/gateway/test_session_mutation_model.py
index e0428c36457..cea358c3600 100644
--- a/tests/gateway/test_session_mutation_model.py
+++ b/tests/gateway/test_session_mutation_model.py
@@ -161,3 +161,31 @@ async def test_input_queued_while_a_model_receipt_commits_still_runs(tmp_path, m
await owner.close()
store.close_all_db_handles()
+
+async def test_provider_change_drops_the_launch_key_and_keeps_config_secrets(tmp_path, monkeypatch):
+ """A session created with an explicit --api-key AND secret-bearing config can change provider:
+ the launch key must not cross providers, the frozen config secrets stay bound."""
+ from dataclasses import asdict
+ from types import SimpleNamespace
+ from gateway.session_mutation_model import prepare_model
+ from gateway.session_policy import bind_launch_key, build_policy, restore_policy
+ from hermes_state import SessionDB
+ import hermes_cli.model_switch as model_switch
+ monkeypatch.setattr(model_switch, 'switch_model', lambda **k: model_switch.ModelSwitchResult(
+ success=True, new_model='b', target_provider='anthropic', provider_changed=True,
+ base_url='https://api.anthropic.com'))
+ with SessionDB(tmp_path / 'state.db') as db:
+ authority = SimpleNamespace(instance_id='i', epoch=1, profile_id='p', db=db, runner=SimpleNamespace(
+ _resolve_session_agent_runtime=lambda **k: (None, {'api_key': 'sk-launch'})))
+ private = {}
+ config = {'model': {'provider': 'openrouter', 'default': 'a'},
+ 'providers': {'mine': {'api_key': 'sk-config-secret', 'base_url': 'http://127.0.0.1:9/v1'}}}
+ policy = build_policy({'cwd': str(tmp_path), 'model': 'a', 'api_key': 'sk-launch'}, config, private_secrets=private)
+ policy = bind_launch_key(authority, 'sid', policy, 'sk-launch', config_secrets=private)
+ assert policy.credential_ref and policy.config_secret_ref
+ prepared = {'snapshot': {'receipt': {'session_id': 'sid', 'policy': asdict(policy)}}}
+ switched = restore_policy((await prepare_model(authority, SimpleNamespace(source=None, route='r'),
+ {'model': 'b', 'provider': 'anthropic'}, prepared))['policy'])
+ assert switched.credential_ref is None, 'the launch key crossed into another provider'
+ assert switched.config(authority)['providers']['mine']['api_key'] == 'sk-config-secret'