Skip to content

The agent that grows with you

fix: retry local creation without binding replacement credentials

Teknium12899b65d51f
diff --git a/gateway/session_local.py b/gateway/session_local.py
index e4c1ae1a5fc..1e095dbdc13 100644
--- a/gateway/session_local.py
+++ b/gateway/session_local.py
@@ -97,7 +97,19 @@ def create_local_session(authority, actor, params):
from hermes_state_local import commit_local_session
authority._require_admission_open()
sid = local_identity(authority.profile_id, actor.subject, request_id)
- from gateway.session_policy import bind_launch_key
+ from gateway.session_policy import bind_launch_key, restore_policy, launch_key
+ if authority.db.get_session(sid) is not None:
+ from hermes_state_local import local_receipt
+ saved = local_receipt(authority.db, sid)
+ original = restore_policy(saved['policy'])
+ if original.request_json != policy.request_json:
+ raise RuntimeStoreError('invalid_params')
+ if original.credential_ref is not None or params.get('api_key') is not None:
+ import hmac
+ key = launch_key(authority, original)
+ if key is None or not hmac.compare_digest(key, params.get('api_key') or ''):
+ raise RuntimeStoreError('admission_conflict')
+ return restore_local_session(authority, sid)
policy = bind_launch_key(authority, sid, policy, params.get("api_key"), config_secrets=private_secrets)
source = SessionSource(platform=Platform.LOCAL, chat_id=sid,
user_id=actor.subject, chat_type='dm')
diff --git a/tests/gateway/test_config_policy_recovery.py b/tests/gateway/test_config_policy_recovery.py
index 35844848cdf..fb1bc2ab8f2 100644
--- a/tests/gateway/test_config_policy_recovery.py
+++ b/tests/gateway/test_config_policy_recovery.py
@@ -32,3 +32,30 @@ def test_config_credentials_recover_only_exact_source(tmp_path, monkeypatch):
(home / 'config.yaml').write_text(json.dumps(cfg))
with pytest.raises(RuntimeStoreError, match='launch_credentials_unavailable'):
policy.config(owner())
+
+
+
+async def test_config_creation_retry_does_not_rebind_current_credentials(tmp_path, monkeypatch):
+ from gateway.config import GatewayConfig, Platform
+ from gateway.session import SessionStore
+ from gateway.session_authority import initialize_session_authority
+ from gateway.session_contract import Principal
+ from gateway.session_local import create_local_session
+ from gateway import run
+ config = {'model': {'api_key': 'original-private'}, 'platform_toolsets': {'cli': []}}
+ monkeypatch.setattr(run, '_load_gateway_config', lambda: config)
+ def runner():
+ store = SessionStore(tmp_path / 'sessions', GatewayConfig())
+ return SimpleNamespace(session_store=store, _session_db=store._db, adapters={}, _draining=False)
+ first = await initialize_session_authority(runner(), profile_id='fixture', instance_id='first')
+ actor = Principal('owner', 'fixture', frozenset({'session:create'}), 'socket')
+ params = dict(request_id='retry', cwd=str(tmp_path), model='frozen')
+ ref = create_local_session(first, actor, params)
+ original = first.runner.adapters[Platform.LOCAL].policies[ref.session_id]
+ cold = await initialize_session_authority(runner(), profile_id='fixture', instance_id='cold')
+ config['model']['api_key'] = 'changed-private'
+ assert create_local_session(cold, actor, params) == ref
+ assert cold.runner.adapters[Platform.LOCAL].policies[ref.session_id] == original
+ assert not getattr(cold, '_local_config_secrets', {})
+ assert not getattr(cold, '_local_launch_keys', {})