Skip to content

The agent that grows with you

fix(profiles): reuse named provider lookup without credential reads

Gillec8a777ffc2b3
diff --git a/hermes_cli/profile_model_seed.py b/hermes_cli/profile_model_seed.py
index 222f5a02185..2f71f400418 100644
--- a/hermes_cli/profile_model_seed.py
+++ b/hermes_cli/profile_model_seed.py
@@ -1,22 +1,13 @@
"""Copy the model and its selected custom endpoint when creating an independent profile."""
from hermes_cli.config import (
+ find_provider_entry,
get_compatible_custom_providers,
- is_provider_enabled,
- providers_dict_to_custom_providers,
)
-from hermes_cli.providers import custom_provider_aliases
-from hermes_cli.runtime_provider_custom import _shadowed_by_builtin
-
-
-def _matches_custom_provider(requested: str, entries: list[dict]) -> bool:
- return bool(
- entries
- and requested
- in custom_provider_aliases(
- entries[0]["name"], entries[0].get("provider_key", "")
- )
- )
+from hermes_cli.runtime_provider_custom import (
+ _get_named_custom_provider,
+ _shadowed_by_builtin,
+)
def launch_model_seed(source_cfg: dict) -> dict:
@@ -31,23 +22,29 @@ def launch_model_seed(source_cfg: dict) -> dict:
requested = provider.strip().lower().replace(" ", "-")
providers = source_cfg.get("providers")
providers = providers if isinstance(providers, dict) else {}
- exact = providers.get(provider)
- # Exact config keys also carry built-in provider tuning, without an endpoint URL.
- if isinstance(exact, dict) and is_provider_enabled(exact):
- seed["providers"] = {provider: exact}
+ selected = _get_named_custom_provider(
+ provider, config=source_cfg, resolve_credentials=False
+ )
+ if selected is None:
+ # Exact built-in keys carry provider tuning even without an endpoint URL.
+ exact = providers.get(provider)
+ if (
+ isinstance(exact, dict)
+ and requested not in {"", "auto"}
+ and _shadowed_by_builtin(requested)
+ ):
+ seed["providers"] = {provider: exact}
return seed
- if not requested or requested == "auto" or _shadowed_by_builtin(requested):
+ key, entry = find_provider_entry(providers, selected.get("provider_key"))
+ if entry is not None:
+ seed["providers"] = {key: entry}
return seed
- for key, entry in providers.items():
- if _matches_custom_provider(
- requested, providers_dict_to_custom_providers({key: entry})
- ):
- seed["providers"] = {key: entry}
- return seed
custom_providers = source_cfg.get("custom_providers")
for entry in custom_providers if isinstance(custom_providers, list) else ():
- if _matches_custom_provider(
- requested, get_compatible_custom_providers({"custom_providers": [entry]})
+ normalized = get_compatible_custom_providers({"custom_providers": [entry]})
+ if normalized and (normalized[0]["name"], normalized[0]["base_url"]) == (
+ selected["name"],
+ selected["base_url"],
):
seed["custom_providers"] = [entry]
return seed
diff --git a/hermes_cli/runtime_provider_custom.py b/hermes_cli/runtime_provider_custom.py
index 698f0eddafe..428f4512b34 100644
--- a/hermes_cli/runtime_provider_custom.py
+++ b/hermes_cli/runtime_provider_custom.py
@@ -136,7 +136,9 @@ def _shadowed_by_builtin(requested_norm: str) -> bool:
return (canonical or "").strip().lower() == requested_norm
-def _match_new_style_provider(requested_norm: str, providers: dict[str, Any]) -> Optional[dict[str, Any]]:
+def _match_new_style_provider(
+ requested_norm: str, providers: dict[str, Any], *, resolve_credentials: bool = True,
+) -> Optional[dict[str, Any]]:
"""Scan ``providers:`` (new-style, keyed) for ``requested_norm``."""
from hermes_cli.config import is_provider_enabled
rp = _rp()
@@ -152,7 +154,7 @@ def _match_new_style_provider(requested_norm: str, providers: dict[str, Any]) ->
# Resolve credentials only after identity and endpoint validation. Merely scanning an
# unrelated entry must not read its profile-scoped secret.
key_env = _clean(entry.get("key_env") or entry.get("api_key_env"))
- api_key = get_secret_str(key_env, "").strip() if key_env else ""
+ api_key = get_secret_str(key_env, "").strip() if key_env and resolve_credentials else ""
result: dict[str, Any] = {"name": entry.get("name", ep_name), "base_url": base_url.strip(),
"api_key": api_key or _clean(entry.get("api_key", "")), "model": entry.get("default_model", "")}
# Command that PRINTS a short-lived credential; wrapped in a per-request token provider.
@@ -188,14 +190,23 @@ def _match_legacy_custom_provider(requested_norm: str, custom_providers) -> Opti
return None
-def _get_named_custom_provider(requested_provider: str) -> Optional[dict[str, Any]]:
+def _get_named_custom_provider(
+ requested_provider: str, *, config: Optional[dict[str, Any]] = None,
+ resolve_credentials: bool = True,
+) -> Optional[dict[str, Any]]:
+ """Resolve a named endpoint. Creation-time config copying supplies raw config and skips
+ credential reads; ordinary runtime callers keep their existing scoped resolution."""
requested_norm = _normalize_custom_provider_name(requested_provider or "")
if not requested_norm or requested_norm == "auto" or _shadowed_by_builtin(requested_norm):
return None
rp = _rp()
- config = rp.load_config()
+ if config is None:
+ config = rp.load_config()
providers = config.get("providers")
- found = _match_new_style_provider(requested_norm, providers) if isinstance(providers, dict) else None
+ found = (
+ _match_new_style_provider(requested_norm, providers, resolve_credentials=resolve_credentials)
+ if isinstance(providers, dict) else None
+ )
if found:
return found
if isinstance(config.get("custom_providers"), dict):
diff --git a/tests/hermes_cli/test_profile_model_seed.py b/tests/hermes_cli/test_profile_model_seed.py
index 5a96d49a9ab..49d18101123 100644
--- a/tests/hermes_cli/test_profile_model_seed.py
+++ b/tests/hermes_cli/test_profile_model_seed.py
@@ -11,6 +11,7 @@ from hermes_cli.config import (
read_user_config_raw,
)
from hermes_cli.profiles import create_profile
+from hermes_cli.profile_model_seed import launch_model_seed
from hermes_cli import runtime_provider
@@ -129,3 +130,63 @@ def test_fresh_profile_keeps_explicit_builtin_provider_settings(tmp_path, monkey
read_user_config_raw(profile / "config.yaml")["providers"]
== source["providers"]
)
+
+
+ "providers,legacy",
+ [
+ (
+ {"lab": {"request_timeout_seconds": 150}},
+ [{"name": "Lab", "base_url": "http://127.0.0.1:11434/v1"}],
+ ),
+ (
+ {
+ "other": {"name": "Lab", "base_url": "http://127.0.0.1:11434/v1"},
+ "lab": {"base_url": "http://127.0.0.1:11435/v1"},
+ },
+ [],
+ ),
+ ],
+)
+def test_model_seed_keeps_runtime_endpoint_selection(monkeypatch, providers, legacy):
+ source = {
+ "model": {"provider": "lab", "default": "synthetic-model"},
+ "providers": providers,
+ "custom_providers": legacy,
+ }
+ monkeypatch.setattr(runtime_provider, "load_config", lambda: source)
+ selected = runtime_provider._get_named_custom_provider("lab")
+ assert selected is not None
+
+ seed = launch_model_seed(source)
+
+ monkeypatch.setattr(runtime_provider, "load_config", lambda: seed)
+ inherited = runtime_provider._get_named_custom_provider("lab")
+ assert inherited is not None
+ assert inherited["base_url"] == selected["base_url"]
+
+
+def test_model_seed_uses_supplied_config_without_reading_credentials(monkeypatch):
+ import hermes_cli.runtime_provider_custom as custom_runtime
+
+ def unexpected_read(*args, **kwargs):
+ pytest.fail(
+ "model seed must use the supplied config without resolving credentials"
+ )
+
+ monkeypatch.setattr(runtime_provider, "load_config", unexpected_read)
+ monkeypatch.setattr(custom_runtime, "get_secret_str", unexpected_read)
+ source = {
+ "model": {"provider": "custom:lab", "default": "synthetic-model"},
+ "providers": {
+ "lab": {
+ "api": "http://127.0.0.1:11434/v1",
+ "key_env": "LAB_TOKEN",
+ "api_key": "${LAB_TOKEN}",
+ }
+ },
+ }
+
+ seed = launch_model_seed(source)
+
+ assert seed == source