The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): a bare custom route's URL-matched key is its route credential
Teknium8a7080503955
diff --git a/gateway/session_policy_credentials.py b/gateway/session_policy_credentials.pyindex ad640f96926..2eef0c16548 100644--- a/gateway/session_policy_credentials.py+++ b/gateway/session_policy_credentials.py@@ -31,13 +31,19 @@ def _route_paths(policy):"""Secret paths the session's own route authenticates with: its ``model:`` block, a direct alias'skey (``api_key_from``) and the one ``providers:`` / ``custom_providers:`` entry the runtime's ownnamed-provider selection routes its provider through."""- from hermes_cli.runtime_provider_custom import named_custom_provider_config_path+ from hermes_cli.runtime_provider_custom import (custom_provider_config_path_for_url,+ named_custom_provider_config_path)config = json.loads(policy.config_json)model = config.get('model') if isinstance(config.get('model'), dict) else {}paths = {('model', key) for key in model}if isinstance(model.get('api_key_from'), list):paths.add(tuple(model['api_key_from']))- selected = named_custom_provider_config_path(str(model.get('provider') or ''), config)+ provider = str(model.get('provider') or '')+ if provider.strip().lower() == 'custom':+ # A bare custom route authenticates with the entry owning its base_url (the pool's owner).+ selected = custom_provider_config_path_for_url(model.get('base_url'), config)+ else:+ selected = named_custom_provider_config_path(provider, config)entry = _defined(config, selected) if selected else _ABSENTif isinstance(entry, dict):paths |= {selected + (field,) for field in entry}diff --git a/hermes_cli/runtime_provider_custom.py b/hermes_cli/runtime_provider_custom.pyindex d65e60e3247..42753844f75 100644--- a/hermes_cli/runtime_provider_custom.py+++ b/hermes_cli/runtime_provider_custom.py@@ -207,6 +207,19 @@ def named_custom_provider_config_path(requested_provider: str, config: dict[str,return found[1] if found else None+def custom_provider_config_path_for_url(base_url: Any, config: dict[str, Any]) -> Optional[tuple]:+ """The config path of the entry a bare ``custom`` route at *base_url* takes its key from: the+ first compatible ``custom_providers`` / ``providers`` entry with that URL (trailing slash+ ignored), the same owner the credential pool seeds from. Reads no credential."""+ url = str(base_url or "").strip().rstrip("/")+ if not url or isinstance(config.get("custom_providers"), dict):+ return None+ for entry in _rp().get_compatible_custom_providers(config) or ():+ if isinstance(entry, dict) and str(entry.get("base_url") or "").strip().rstrip("/") == url:+ return _compatible_entry_path(config, entry)+ return None++def _select_named_custom_provider(requested_provider: str, *, config: Optional[dict[str, Any]], resolve_credentials: bool,) -> Optional[tuple[dict[str, Any], Optional[tuple]]]:diff --git a/tests/gateway/test_config_policy_recovery.py b/tests/gateway/test_config_policy_recovery.pyindex b5ef66b26c2..2eecdcafaff 100644--- a/tests/gateway/test_config_policy_recovery.py+++ b/tests/gateway/test_config_policy_recovery.py@@ -214,3 +214,29 @@ def test_a_custom_prefixed_providers_key_is_the_route(tmp_path, monkeypatch):with pytest.raises(RuntimeStoreError) as refused:policy.config(owner())assert refused.value.field == 'providers.custom:foo.api_key'+++def test_a_bare_custom_key_selected_by_url_is_the_route(tmp_path, monkeypatch):+ """A bare ``model.provider: custom`` with a ``base_url`` authenticates with the+ ``custom_providers`` entry owning that URL. Rotating that key and recovering cold used to skip+ the frozen value as an unrelated secret, so the pool seeded the rotated key from live config;+ it now refuses by name, like a named route."""+ from hermes_state_runtime import RuntimeStoreError+ url = 'http://127.0.0.1:1234/v1'+ cfg = {'model': {'provider': 'custom', 'base_url': url, 'default': 'm'},+ 'custom_providers': [+ {'name': 'Other', 'base_url': 'http://127.0.0.1:9999/v1', 'api_key': 'other-token'},+ {'name': 'Local', 'base_url': url + '/', 'api_key': 'url-token'}]}+ policy, owner, config_path = _bound_session(tmp_path, monkeypatch, cfg)+ assert policy.config(owner())['custom_providers'][1]['api_key'] == 'url-token'++ cfg['custom_providers'][0]['api_key'] = 'rotated-other' # not the route: simply not supplied+ config_path.write_text(json.dumps(cfg))+ assert policy.config(owner())['custom_providers'][0]['api_key'] is None++ cfg['custom_providers'][1]['api_key'] = 'rotated-url-token'+ config_path.write_text(json.dumps(cfg))+ with pytest.raises(RuntimeStoreError) as refused:+ policy.config(owner())+ assert refused.value.field == 'custom_providers.1.api_key'+ assert 'rotated' not in str(refused.value.detail)