Skip to content

The agent that grows with you

fix(providers): runtime-endpoint plugin profiles resolve in /model, the model picker and hermes model

teknium16040512dabbe
diff --git a/hermes_cli/providers.py b/hermes_cli/providers.py
index 1d26b783981..4811f4fd712 100644
--- a/hermes_cli/providers.py
+++ b/hermes_cli/providers.py
@@ -208,28 +208,38 @@ def get_provider(name: str, *, allow_network: bool = True) -> Optional[ProviderD
overlay.base_url_override, "", "hermes")
# Plugin-registered profiles (plugins/model-providers/<name>/) absent from models.dev and
# HERMES_OVERLAYS would otherwise be "Unknown provider" in /model, --provider and model-switch
- # even though the picker lists them. Profiles may declare a literal or env-configured endpoint:
- # placeholder profiles like ``custom`` (aliases ollama/local/vllm) ship an empty base_url and
- # are completed by config.yaml custom_providers — resolving them would preempt
- # resolve_provider_full's custom step and collapse keyed ``custom:<name>`` ids to bare custom.
+ # even though the picker lists them. Only profiles with a literal or env-configured endpoint
+ # resolve at this rung: placeholder profiles like ``custom`` (aliases ollama/local/vllm) ship
+ # an empty base_url and are completed by config.yaml custom_providers — resolving them would
+ # preempt resolve_provider_full's custom step and collapse keyed ``custom:<name>`` ids to bare
+ # custom. Profiles whose endpoint is minted at runtime resolve at the END of
+ # resolve_provider_full, after every user-configured rung.
+ pdef = _plugin_profile_pdef(canonical)
+ if pdef is None or not (pdef.base_url or (pdef.auth_type == "api_key" and pdef.api_key_env_vars and pdef.base_url_env_var)):
+ return None
+ return pdef
+
+
+def _plugin_profile_pdef(name: str) -> Optional[ProviderDef]:
+ """The registered ``ProviderProfile`` for *name* (or one of its aliases) as a ProviderDef; the
+ id is the profile's canonical name so an alias switch persists and resolves credentials under
+ the same identity as the profile itself. URL-shaped env vars are the endpoint, not the key."""
try:
from providers import get_provider_profile as _profile
- _prof = _profile(canonical)
- if _prof is not None:
- _env_vars = tuple(_prof.env_vars or ())
- _url_vars = tuple(v for v in _env_vars if v.endswith(("_BASE_URL", "_URL")))
- _key_vars = tuple(v for v in _env_vars if v not in _url_vars)
- if not ((_prof.base_url or "").strip() or (_prof.auth_type == "api_key" and _key_vars and _url_vars)):
- return None
- _api_mode_to_transport = {v: k for k, v in TRANSPORT_TO_API_MODE.items()}
- return ProviderDef(id=_prof.name, name=_prof.display_name or _prof.name or canonical,
- transport=_api_mode_to_transport.get(_prof.api_mode, "openai_chat"),
- api_key_env_vars=_key_vars, base_url=_prof.base_url or "",
- base_url_env_var=next(iter(_url_vars), ""),
- auth_type=_prof.auth_type or "api_key", source="plugin-profile")
+ prof = _profile(name)
except Exception:
- pass
- return None
+ return None
+ if prof is None:
+ return None
+ env_vars = tuple(prof.env_vars or ())
+ url_vars = tuple(v for v in env_vars if v.endswith(("_BASE_URL", "_URL")))
+ key_vars = tuple(v for v in env_vars if v not in url_vars)
+ api_mode_to_transport = {v: k for k, v in TRANSPORT_TO_API_MODE.items()}
+ return ProviderDef(id=prof.name, name=prof.display_name or prof.name or name,
+ transport=api_mode_to_transport.get(prof.api_mode, "openai_chat"),
+ api_key_env_vars=key_vars, base_url=(prof.base_url or "").strip(),
+ base_url_env_var=next(iter(url_vars), ""),
+ auth_type=prof.auth_type or "api_key", source="plugin-profile")
def get_label(provider_id: str) -> str:
@@ -523,4 +533,10 @@ def resolve_provider_full(name: str, user_providers: Optional[Dict[str, Any]] =
base_url=mdev_info.api, source="models.dev")
except Exception:
pass
- return None
+ # Plugin profiles whose endpoint is minted at runtime (empty base_url, e.g. a token exchange
+ # that also returns the host) are still real providers: /model --provider, the model picker
+ # and `hermes model` must not reject them as unknown. Last rung, so every user-configured
+ # entry above wins; the bare ``custom`` placeholder is excluded because model-switch completes
+ # it from the current endpoint (see get_provider).
+ pdef = _plugin_profile_pdef(canonical)
+ return pdef if pdef is not None and pdef.id != "custom" else None
diff --git a/tests/hermes_cli/test_provider_profile_identity.py b/tests/hermes_cli/test_provider_profile_identity.py
index e8b88235f95..8df8bc5262b 100644
--- a/tests/hermes_cli/test_provider_profile_identity.py
+++ b/tests/hermes_cli/test_provider_profile_identity.py
@@ -1,9 +1,6 @@
-"""ProviderProfile identity/config regressions through discovery, CLI and runtime.
+"""Plugin ProviderProfiles resolve wherever a provider NAME is resolved at switch time (#69576).
-The on-disk discovery fixture is adapted from david-bowiegxw's #53054 tests
-(commit fcbc30e5defc112a66ef834f6e2e4e46d3cbcec4). The current auth bridge
-and transport fallback remain upstream-owned; these tests cover residual
-identity and configuration behavior.
+Discovery is real (a plugin dir under the test HERMES_HOME); only the remote model probe is stubbed.
"""
from __future__ import annotations
@@ -14,253 +11,88 @@ import pytest
import yaml
from hermes_cli import auth as auth_mod
-from hermes_cli import runtime_provider as rp
from hermes_cli.model_switch import switch_model
-from hermes_cli.providers import get_provider, resolve_provider_full
+from hermes_cli.providers import resolve_provider_full
from hermes_constants import get_hermes_home
+KEY = "test-profile-credential"
+ACCEPT = {"accepted": True, "persist": True, "recognized": True, "message": None}
-PLUGIN_NAME = "testgw"
-PLUGIN_ALIAS = "testgw-alias"
-PLUGIN_OTHER_ALIAS = "testgw-other"
-PLUGIN_ENV_VAR = "TESTGW_API_KEY"
-PLUGIN_URL_VAR = "TESTGW_BASE_URL"
-PLUGIN_BASE_URL = "https://gw.example.com/api/coding"
-ENV_BASE_URL = "https://env.example.test/api/coding"
-CONFIG_BASE_URL = "https://configured.example.test/api/coding"
-TEST_KEY = "test-profile-credential"
-
[email protected](params=[PLUGIN_BASE_URL, ""], ids=["default-url", "env-only-url"])
-def registered_plugin_provider(request, monkeypatch):
- """Discover an actual plugin file, then use the production auth bridge.
-
- Keep previously discovered bundled profiles and restore shared auth state;
- only this fixture's imported module is removed on teardown.
- """
+def install_profile(monkeypatch):
+ """Write one model-provider plugin, rediscover, and mirror it into the auth registry."""
import providers as profiles
monkeypatch.setattr(profiles, "_REGISTRY", dict(profiles._REGISTRY))
monkeypatch.setattr(profiles, "_ALIASES", dict(profiles._ALIASES))
monkeypatch.setattr(profiles, "_PROVIDER_LIST_CACHE", None)
- monkeypatch.setattr(profiles, "_discovered", False)
- auth_before = dict(auth_mod.PROVIDER_REGISTRY)
- module_name = "_hermes_user_provider_testgw"
- monkeypatch.delitem(sys.modules, module_name, raising=False)
monkeypatch.setattr("agent.models_dev.fetch_models_dev", lambda *a, **kw: {})
- monkeypatch.setenv(PLUGIN_ENV_VAR, TEST_KEY)
- monkeypatch.delenv(PLUGIN_URL_VAR, raising=False)
- monkeypatch.delenv("TESTGW_URL", raising=False)
- if not request.param:
- monkeypatch.setenv(PLUGIN_URL_VAR, ENV_BASE_URL)
-
- plugin_dir = get_hermes_home() / "plugins" / "model-providers" / PLUGIN_NAME
- plugin_dir.mkdir(parents=True)
- (plugin_dir / "plugin.yaml").write_text(
- f"name: {PLUGIN_NAME}\nkind: model-provider\nversion: 0.0.1\n"
- "description: Provider identity regression fixture\n"
- )
- (plugin_dir / "__init__.py").write_text(
- "from providers import register_provider\n"
- "from providers.base import ProviderProfile\n\n"
- "register_provider(ProviderProfile(\n"
- f" name={PLUGIN_NAME!r},\n"
- f" aliases={(PLUGIN_ALIAS, PLUGIN_OTHER_ALIAS)!r},\n"
- " display_name='Test Gateway',\n"
- " api_mode='anthropic_messages',\n"
- f" env_vars={(PLUGIN_URL_VAR, PLUGIN_ENV_VAR, 'TESTGW_URL')!r},\n"
- f" base_url={request.param!r},\n"
- " auth_type='api_key',\n"
- "))\n"
- "register_provider(ProviderProfile(\n"
- " name='foreign-provider', env_vars=('FOREIGN_TESTGW_API_KEY',),\n"
- " base_url='https://foreign.example.test/v1', api_mode='codex_responses',\n"
- "))\n"
- "register_provider(ProviderProfile(\n"
- " name='testgw-process', auth_type='external_process',\n"
- " base_url='acp+tcp://127.0.0.1:56789', env_vars=(),\n"
- "))\n"
- )
- try:
- for profile in profiles.list_providers():
- if profile.name not in auth_mod.PROVIDER_REGISTRY:
- auth_mod._register_plugin_provider(profile)
- profile = profiles.get_provider_profile(PLUGIN_NAME)
- assert profile is not None
- config = auth_mod.PROVIDER_REGISTRY[PLUGIN_NAME]
- assert config is auth_mod.PROVIDER_REGISTRY[PLUGIN_ALIAS]
- assert config.api_key_env_vars == (PLUGIN_ENV_VAR,)
- assert config.base_url_env_var == PLUGIN_URL_VAR
- yield profile
- finally:
- auth_mod.PROVIDER_REGISTRY.clear()
- auth_mod.PROVIDER_REGISTRY.update(auth_before)
- sys.modules.pop(module_name, None)
-
-
-def _write_model_config(provider, **settings):
- (get_hermes_home() / "config.yaml").write_text(
- yaml.safe_dump({"model": {"provider": provider, "default": "test-model", **settings}})
- )
-
-
[email protected]("requested", [PLUGIN_NAME, PLUGIN_ALIAS, PLUGIN_OTHER_ALIAS])
[email protected]("override", [None, "canonical", "raw"])
-def test_residual_profile_cli_identity(registered_plugin_provider, monkeypatch, requested, override):
- profile = registered_plugin_provider
- _write_model_config(PLUGIN_NAME)
- # Validation is an external model probe; discovery, auth, config and the
- # switch's runtime credential resolution are all real.
- monkeypatch.setattr(
- "hermes_cli.models_validate.validate_requested_model",
- lambda *a, **kw: {"accepted": True, "persist": True, "recognized": True, "message": None},
- )
- user_providers = {}
- expected_url = profile.base_url or ENV_BASE_URL
- expected_id = profile.name
- if override:
- user_providers[profile.name] = {
- "name": "Configured Gateway", "base_url": CONFIG_BASE_URL,
- "key_env": PLUGIN_ENV_VAR,
- }
- expected_url = CONFIG_BASE_URL
- if override == "raw":
- user_providers[requested] = {
- "name": "Raw Override", "base_url": "https://raw.example.test/api/coding",
- "key_env": PLUGIN_ENV_VAR,
- }
- expected_url = user_providers[requested]["base_url"]
- expected_id = requested
+ monkeypatch.setattr("hermes_cli.models_validate.validate_requested_model", lambda *a, **kw: ACCEPT)
+ auth_before = dict(auth_mod.PROVIDER_REGISTRY)
- result = switch_model(
- "test-model", current_provider="openrouter", current_model="previous-model",
- current_api_key="foreign-provider-key", explicit_provider=requested,
- user_providers=user_providers, custom_providers=[],
- )
+ def _install(name: str, *, aliases=(), base_url: str, env_var: str, api_mode: str = "chat_completions"):
+ monkeypatch.setenv(env_var, KEY)
+ monkeypatch.setattr(profiles, "_discovered", False)
+ monkeypatch.delitem(sys.modules, f"_hermes_user_provider_{name}", raising=False)
+ plugin_dir = get_hermes_home() / "plugins" / "model-providers" / name
+ plugin_dir.mkdir(parents=True, exist_ok=True)
+ (plugin_dir / "plugin.yaml").write_text(
+ f"name: {name}\nkind: model-provider\nversion: 0.0.1\ndescription: switch fixture\n", encoding="utf-8")
+ (plugin_dir / "__init__.py").write_text(
+ "from providers import register_provider\nfrom providers.base import ProviderProfile\n"
+ f"register_provider(ProviderProfile(name={name!r}, aliases={tuple(aliases)!r}, auth_type='api_key',\n"
+ f" env_vars=({env_var!r},), base_url={base_url!r}, api_mode={api_mode!r}))\n", encoding="utf-8")
+ auth_mod.sync_plugin_provider_registry()
+ return profiles.get_provider_profile(name)
+
+ yield _install
+ auth_mod.PROVIDER_REGISTRY.clear()
+ auth_mod.PROVIDER_REGISTRY.update(auth_before)
+
+
+def test_alias_switch_carries_the_profiles_canonical_identity(install_profile):
+ """`/model --provider <alias>` lands on the profile's own name (persisted config, credential and
+ runtime lookups all key on it); a user-configured block for that name still wins over the profile."""
+ install_profile("testgw", aliases=("testgw-alias",), base_url="https://gw.example.test/v1",
+ env_var="TESTGW_API_KEY", api_mode="anthropic_messages")
+
+ pdef = resolve_provider_full("testgw-alias", user_providers={}, custom_providers=[])
+ assert (pdef.id, pdef.source) == ("testgw", "plugin-profile")
+ result = switch_model("test-model", current_provider="openrouter", current_model="m",
+ explicit_provider="testgw-alias", user_providers={}, custom_providers=[])
assert result.success, result.error_message
- assert result.target_provider == expected_id
- assert result.base_url == expected_url
- assert result.api_key == TEST_KEY
+ assert (result.target_provider, result.api_mode, result.api_key) == ("testgw", "anthropic_messages", KEY)
- definition = resolve_provider_full(requested, user_providers=user_providers)
- assert definition is not None
- assert definition.id == expected_id
- assert definition.api_key_env_vars == (PLUGIN_ENV_VAR,)
- if not override:
- assert definition.base_url_env_var == PLUGIN_URL_VAR
- assert definition.source == "plugin-profile"
- assert result.api_mode == profile.api_mode
- else:
- assert definition.source == "user-config"
- assert definition.base_url == expected_url
+ user = {"testgw": {"name": "Configured", "base_url": "https://cfg.example.test/v1", "key_env": "TESTGW_API_KEY"}}
+ assert resolve_provider_full("testgw-alias", user_providers=user).source == "user-config"
[email protected]("route", ["pooled", "explicit-key", "no-pool"])
- "requested,persisted",
- [
- (PLUGIN_NAME, PLUGIN_NAME),
- (PLUGIN_NAME, PLUGIN_ALIAS),
- (PLUGIN_ALIAS, PLUGIN_NAME),
- (PLUGIN_ALIAS, PLUGIN_OTHER_ALIAS),
- (PLUGIN_OTHER_ALIAS, "foreign-provider"),
- (PLUGIN_OTHER_ALIAS, ""),
- ],
-)
-def test_residual_profile_runtime_config(registered_plugin_provider, monkeypatch, route, requested, persisted):
- from agent.credential_pool import CredentialPool
-
- profile = registered_plugin_provider
- # A URL without a configured provider deliberately selects the bare-custom
- # bypass. Leave it absent when testing the no-provider api_mode contract.
- _write_model_config(persisted, base_url=CONFIG_BASE_URL if persisted else "", api_mode="chat_completions")
- kwargs = {}
- if route == "explicit-key":
- kwargs["explicit_api_key"] = TEST_KEY
- elif route == "no-pool":
- # Force the fallback route with a real empty pool; credentials still
- # resolve through the real provider config and scoped environment.
- monkeypatch.setattr(rp, "load_pool", lambda provider: CredentialPool(provider, []))
- runtime = rp.resolve_runtime_provider(requested=requested, **kwargs)
-
- same_profile = persisted in (PLUGIN_NAME, PLUGIN_ALIAS, PLUGIN_OTHER_ALIAS)
- expected_url = profile.base_url or ENV_BASE_URL
- if same_profile and (route == "no-pool" or (route == "pooled" and profile.base_url)):
- expected_url = CONFIG_BASE_URL
- assert runtime["provider"] == profile.name
- assert runtime["requested_provider"] == requested
- assert runtime["base_url"] == expected_url
- assert runtime["api_key"] == TEST_KEY
- assert runtime["api_mode"] == ("chat_completions" if same_profile or not persisted else profile.api_mode)
- if route == "pooled":
- assert isinstance(runtime["credential_pool"], CredentialPool)
- assert runtime["source"] == f"env:{PLUGIN_ENV_VAR}"
- else:
- assert "credential_pool" not in runtime
- if route == "explicit-key":
- assert runtime["source"] == "explicit"
-
-
[email protected]("route", ["pooled", "explicit-key", "no-pool", "explicit-url"])
-def test_residual_profile_url_precedence(registered_plugin_provider, monkeypatch, route):
- from agent.credential_pool import CredentialPool
-
- profile = registered_plugin_provider
- _write_model_config(PLUGIN_ALIAS, base_url=CONFIG_BASE_URL)
- monkeypatch.setenv(PLUGIN_URL_VAR, "https://api.openai.com/v1")
- kwargs = {}
- if route in ("explicit-key", "explicit-url"):
- kwargs["explicit_api_key"] = TEST_KEY
- if route == "explicit-url":
- kwargs["explicit_base_url"] = PLUGIN_BASE_URL
- if route == "no-pool":
- monkeypatch.setattr(rp, "load_pool", lambda provider: CredentialPool(provider, []))
-
- runtime = rp.resolve_runtime_provider(requested=PLUGIN_OTHER_ALIAS, **kwargs)
- expected_url = {
- "pooled": "https://api.openai.com/v1", "explicit-key": "https://api.openai.com/v1",
- "no-pool": CONFIG_BASE_URL, "explicit-url": PLUGIN_BASE_URL,
- }[route]
- assert runtime["provider"] == profile.name
- assert runtime["base_url"] == expected_url
- assert runtime["api_mode"] == (
- "codex_responses" if route in ("pooled", "explicit-key") else profile.api_mode
- )
+def test_runtime_endpoint_profile_is_a_known_provider_but_placeholders_are_not(install_profile):
+ """A profile that mints its endpoint at runtime (empty base_url) resolves on the last rung, so the
+ switch accepts it; the bare ``custom`` placeholder and its local aliases still fall through to the
+ custom_providers / current-endpoint handling."""
+ install_profile("mintgw", base_url="", env_var="MINTGW_API_KEY")
+ pdef = resolve_provider_full("mintgw", user_providers={}, custom_providers=[])
+ assert (pdef.id, pdef.base_url, pdef.source) == ("mintgw", "", "plugin-profile")
+ result = switch_model("test-model", current_provider="openrouter", current_model="m",
+ explicit_provider="mintgw", user_providers={}, custom_providers=[])
+ assert result.success, result.error_message
+ assert (result.target_provider, result.api_key) == ("mintgw", KEY)
+ for placeholder in ("custom", "ollama", "custom:nope"):
+ assert resolve_provider_full(placeholder, user_providers={}, custom_providers=[]) is None
-def test_residual_profile_url_is_not_a_credential(registered_plugin_provider, monkeypatch):
- from hermes_cli.auth import AuthError
- monkeypatch.delenv(PLUGIN_ENV_VAR)
- monkeypatch.setenv(PLUGIN_URL_VAR, ENV_BASE_URL)
- definition = get_provider(PLUGIN_ALIAS, allow_network=False)
- assert definition is not None
- assert definition.api_key_env_vars == (PLUGIN_ENV_VAR,)
- with pytest.raises(AuthError, match="No usable credentials"):
- rp.resolve_runtime_provider(requested=PLUGIN_ALIAS)
+def test_persisted_alias_config_applies_to_the_canonical_runtime_provider(install_profile):
+ """config.yaml written under an alias (``provider: testgw-alias``) is the same provider at runtime:
+ its base_url override applies when the canonical name is requested."""
+ from hermes_cli import runtime_provider as rp
+ install_profile("testgw", aliases=("testgw-alias",), base_url="https://gw.example.test/v1", env_var="TESTGW_API_KEY")
+ (get_hermes_home() / "config.yaml").write_text(yaml.safe_dump(
+ {"model": {"provider": "testgw-alias", "default": "test-model", "base_url": "https://cfg.example.test/v1"}}),
+ encoding="utf-8")
-def test_residual_external_process_route_keeps_process_auth(registered_plugin_provider, monkeypatch):
- """Control: resolving an external profile does not require an API key or launch it."""
- _write_model_config(PLUGIN_ALIAS, base_url=CONFIG_BASE_URL, api_mode="anthropic_messages")
- monkeypatch.setattr(
- "hermes_cli.models_validate.validate_requested_model",
- lambda *a, **kw: {"accepted": True, "persist": True, "recognized": True, "message": None},
- )
- definition = get_provider("testgw-process", allow_network=False)
- assert definition is not None
- assert definition.auth_type == "external_process"
- assert definition.api_key_env_vars == ()
- runtime = rp.resolve_runtime_provider(requested=definition.id)
- assert runtime["source"] == "process"
- assert runtime["api_key"] == definition.id
- assert runtime["base_url"] == definition.base_url
- assert runtime["api_mode"] == "chat_completions"
- result = switch_model(
- "test-model", current_provider=PLUGIN_NAME, current_model="previous-model",
- explicit_provider=definition.id, user_providers={}, custom_providers=[],
- )
- assert result.success, result.error_message
- assert result.target_provider == definition.id
- assert result.api_key == runtime["api_key"]
- assert result.base_url == runtime["base_url"]
+ runtime = rp.resolve_runtime_provider(requested="testgw")
+ assert (runtime["provider"], runtime["base_url"]) == ("testgw", "https://cfg.example.test/v1")