Skip to content

The agent that grows with you

fix(gateway): a local session survives a frozen config secret the profile dropped

lnl211cb1f4deef
diff --git a/gateway/run_turn.py b/gateway/run_turn.py
index 683e61e8db2..d8667c1e4f1 100644
--- a/gateway/run_turn.py
+++ b/gateway/run_turn.py
@@ -1014,7 +1014,7 @@ class GatewayTurnMixin(GatewayTurnPrepareMixin, GatewayTurnHygieneMixin, Gateway
from gateway.session_policy import policy_for_source
policy = policy_for_source(self, source)
from gateway.session_authorities import active_authority
- user_config = policy.config(active_authority(self)) if policy else _load_gateway_config()
+ user_config = policy.config(active_authority(self), secrets=False) if policy else _load_gateway_config()
platform_key = policy.platform if policy else _platform_config_key(source.platform)
enabled_toolsets, disabled_toolsets = self._resolve_turn_toolsets(user_config, source, platform_key)
if policy:
diff --git a/gateway/session_policy_credentials.py b/gateway/session_policy_credentials.py
index a9947669437..7f247b94777 100644
--- a/gateway/session_policy_credentials.py
+++ b/gateway/session_policy_credentials.py
@@ -2,13 +2,26 @@
import hashlib
import hmac
import json
+import logging
from dataclasses import asdict
from pathlib import Path
from agent.credential_persistence import fingerprint_secret_value
from hermes_state_runtime import RuntimeStoreError
+logger = logging.getLogger(__name__)
PREFIX = 'profile-config-v1:'
+_ABSENT = object()
+
+
+def _defined(root, path):
+ """The value *root* defines at *path*, else ``_ABSENT`` when the key is simply not there."""
+ value = root
+ for key in path:
+ if not isinstance(value, dict) or key not in value:
+ return _ABSENT
+ value = value[key]
+ return value
def policy_identity(policy):
@@ -62,14 +75,25 @@ def recover_config_secrets(authority, policy, skip=None):
config = _load_gateway_config(home / 'config.yaml') if any(path[0] is not None for path, _ in entries) else {}
values = {}
for path, fingerprint in entries:
- value = config
- lookup = path
+ value, lookup = config, path
if path[0] is None:
from tools.terminal_scope import build_profile_terminal_scope
- value = build_profile_terminal_scope(home)
- lookup = path[1:]
- for key in lookup:
- value = value[key]
+ value, lookup = build_profile_terminal_scope(home), path[1:]
+ value = _defined(value, lookup)
+ if value is _ABSENT:
+ if len(path) > 1:
+ raise ValueError('credential removed')
+ # One top-level key the profile no longer defines at all: an env-routed setting the
+ # user has since moved out of config.yaml (``API_SERVER_KEY`` belongs in .env, where
+ # ``hermes config set`` writes it) is not this session's route credential, and the
+ # profile has no value left to recover or to substitute. Refusing it used to wedge
+ # every read of the chat - its model options, its reasoning picker, its turns - not
+ # just the credential it named, so the session simply keeps what the frozen tree
+ # already holds (None). A nested path still refuses: it can carry the route's own
+ # credential, and a session must never silently take a different endpoint's key.
+ logger.warning('Session %s: frozen config secret %s is no longer defined by the '
+ 'profile config; skipping it', source.get('session'), path)
+ continue
actual = _fingerprint(value)
if actual is None or not hmac.compare_digest(actual, fingerprint):
raise ValueError('credential changed')
diff --git a/tests/gateway/test_config_policy_recovery.py b/tests/gateway/test_config_policy_recovery.py
index fb1bc2ab8f2..ca2c773d610 100644
--- a/tests/gateway/test_config_policy_recovery.py
+++ b/tests/gateway/test_config_policy_recovery.py
@@ -59,3 +59,43 @@ async def test_config_creation_retry_does_not_rebind_current_credentials(tmp_pat
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', {})
+
+
+def test_dropped_top_level_setting_does_not_wedge_the_session(tmp_path, monkeypatch):
+ """A profile that stops defining a frozen top-level setting (an env-routed key the user moved to
+ .env, where ``hermes config set`` writes it) leaves the session running on what its frozen tree
+ holds instead of failing every read of the chat with ``launch_credentials_unavailable``. A
+ ROUTE credential that disappeared still refuses, and a settings-only read never needs either."""
+ from gateway.session_policy import build_policy, bind_launch_key
+ from hermes_state_runtime import RuntimeStoreError
+ home = tmp_path / 'profile'
+ home.mkdir()
+ monkeypatch.setenv('HERMES_HOME', str(home))
+ cfg = {'API_SERVER_KEY': 'owned-api-server-token',
+ 'model': {'provider': 'custom', 'api_key': 'route-token', 'base_url': 'http://127.0.0.1:1234/v1'}}
+ config_path = home / 'config.yaml'
+ config_path.write_text(json.dumps(cfg))
+
+ def owner():
+ # A fresh authority per read, like a restarted owner: no in-memory secret cache.
+ return SimpleNamespace(db=SimpleNamespace(db_path=home / 'state.db'),
+ profile_id='owned', instance_id='new', epoch=2)
+ private = {}
+ policy = build_policy({'cwd': str(home), 'model': 'frozen'}, cfg, private_secrets=private)
+ assert set(private) == {('API_SERVER_KEY',), ('model', 'api_key')}
+ policy = bind_launch_key(owner(), 'session', policy, None, config_secrets=private)
+ assert cfg['API_SERVER_KEY'] not in json.dumps(asdict(policy))
+ assert policy.config(owner())['API_SERVER_KEY'] == cfg['API_SERVER_KEY']
+
+ del cfg['API_SERVER_KEY'] # the user moves the env-routed key to .env
+ config_path.write_text(json.dumps(cfg))
+ config = policy.config(owner())
+ assert config['model']['api_key'] == 'route-token'
+ assert config.get('API_SERVER_KEY') is None
+
+ del cfg['model']['api_key'] # the session's own route credential is gone: still refused
+ config_path.write_text(json.dumps(cfg))
+ with pytest.raises(RuntimeStoreError, match='launch_credentials_unavailable'):
+ policy.config(owner())
+ settings = policy.config(owner(), secrets=False) # a display/model-option read needs no secret
+ assert settings['model']['provider'] == 'custom' and settings['model']['api_key'] is None