The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(runtime): local alias guard keys on a missing endpoint, /model passes the alias URL
teknium1fea239698131
diff --git a/hermes_cli/model_switch.py b/hermes_cli/model_switch.pyindex 8afc29414c4..ab13f3e72f6 100644--- a/hermes_cli/model_switch.py+++ b/hermes_cli/model_switch.py@@ -1366,8 +1366,12 @@ def _creds_for_switched_provider(st: _Switch) -> Optional[ModelSwitchResult]:st.api_key, st.base_url = st.current_api_key, st.current_base_urlst.api_mode = determine_api_mode(st.target_provider, st.base_url)else:+ # A URL-bearing direct alias supplies its endpoint HERE as well as in+ # _apply_direct_alias_endpoint: the resolver refuses a local alias (ollama, vllm) with no+ # endpoint configured anywhere, and this alias does have one.+ da = DIRECT_ALIASES.get(st.resolved_alias) if st.resolved_alias else Nonetry:- st.resolve_runtime(requested=st.target_provider)+ st.resolve_runtime(requested=st.target_provider, explicit_base_url=(da.base_url if da else None) or None)except Exception as e:return st.fail_on_target(f"{st.provider_label} is not connected: no API key or login was found for it. Add one with "diff --git a/hermes_cli/runtime_provider.py b/hermes_cli/runtime_provider.pyindex b2b7e7826f1..bfb0a0e4647 100644--- a/hermes_cli/runtime_provider.py+++ b/hermes_cli/runtime_provider.py@@ -422,7 +422,7 @@ def resolve_requested_provider(requested: Optional[str] = None) -> str:# ── extracted collaborators (re-exported; see module docstring) ────────────────────────────from hermes_cli.runtime_provider_custom import ( # noqa: E402,F401- _apply_custom_provider_extras, _custom_provider_request_overrides, _filter_capabilities, _find_custom_identity,+ _LLAMACPP_ALIASES, _apply_custom_provider_extras, _custom_provider_request_overrides, _filter_capabilities, _find_custom_identity,_get_named_custom_provider, _lift_common_custom_fields, _lift_extra_headers,_lift_model_capabilities, _normalize_base_url_for_match, _normalize_custom_provider_name, _resolve_named_custom_runtime,_try_resolve_from_custom_pool, canonical_custom_identity, find_custom_provider_identity,@@ -763,7 +763,6 @@ def _api_key_provider_runtime(provider, pconfig, requested_provider, model_cfg,_VERTEX_NAMES = ("vertex", "google-vertex", "vertex-ai", "gcp-vertex", "vertexai")_LOCAL_BYPASS_CLOUD_HOSTS = ("openrouter.ai", "anthropic.com", "openai.com")-_LOCAL_ENDPOINT_ALIASES = frozenset({"ollama", "vllm"})def _raise_if_provider_disabled(requested_provider: str) -> None:@@ -778,26 +777,31 @@ def _raise_if_provider_disabled(requested_provider: str) -> None:def _raise_if_local_alias_missing_endpoint(requested_provider: str, explicit_base_url: Optional[str]) -> None:- """Fail local aliases before the OpenRouter fallback when no endpoint was selected.-- ``ollama`` and ``vllm`` are aliases for the OpenAI-compatible custom- provider. Without an endpoint, their old path continued through the- generic resolver and could spend an unrelated cloud credential at- OpenRouter. A named custom entry, ``model.base_url``, and explicit endpoint- inputs remain valid ways to provide that endpoint.- """- if requested_provider not in _LOCAL_ENDPOINT_ALIASES:+ """A local-server alias (``ollama``, ``vllm`` — anything ``auth.resolve_provider`` maps to+ ``custom`` without a rung of its own) with NO endpoint configured anywhere would otherwise walk+ the whole ladder to the OpenRouter fallback and spend an unrelated cloud key there (#113703).+ Keyed on the ABSENCE of an endpoint, not on the alias name: ``/model <direct-alias>`` resolves+ the alias label with the alias endpoint as ``explicit_base_url`` and must keep working, which+ is why the name-keyed version was reverted (e9a54c48f2 / a9fabe43c4). Endpoint sources:+ explicit call base_url, ``CUSTOM_BASE_URL``, a trusted ``model.base_url``, or a+ ``providers.<alias>`` block carrying a ``base_url``. ``OPENROUTER_BASE_URL`` is never the+ alias endpoint, and an explicit api_key does not lift the guard — that key was meant for the+ alias's own server. ``llamacpp`` fails fast on its own managed-server rung."""+ requested_norm = (requested_provider or "").strip().lower()+ if (requested_norm in ("", "custom") or requested_norm in _LLAMACPP_ALIASES+ or not _resolves_to_custom(requested_norm)):returnif str(explicit_base_url or "").strip() or get_secret_str("CUSTOM_BASE_URL", "").strip():returnmodel_cfg = _get_model_config()- cfg_base_url = str(model_cfg.get("base_url") or "").strip()- if _config_base_url_trustworthy_for_bare_custom(cfg_base_url, _cfg_provider(model_cfg)):+ if _config_base_url_trustworthy_for_bare_custom(str(model_cfg.get("base_url") or ""), _cfg_provider(model_cfg)):return- if _get_named_custom_provider(requested_provider) is not None:+ if str((_get_named_custom_provider(requested_provider) or {}).get("base_url") or "").strip():returnraise AuthError(- f"Local provider '{requested_provider}' needs a base_url. Configure model.base_url or select a configured provider endpoint.",+ f"provider '{requested_provider}' has no endpoint configured, so the request is not sent anywhere "+ f"(it would otherwise fall back to OpenRouter). Set providers.{requested_norm}.base_url or "+ "model.base_url in config.yaml.",provider=requested_provider,code="missing_base_url",)diff --git a/tests/hermes_cli/test_runtime_provider_resolution.py b/tests/hermes_cli/test_runtime_provider_resolution.pyindex 09b9adab0e3..744bd7fe3f0 100644--- a/tests/hermes_cli/test_runtime_provider_resolution.py+++ b/tests/hermes_cli/test_runtime_provider_resolution.py@@ -787,7 +787,7 @@ def test_bare_custom_without_credentials_for_remote_endpoint_fails_fast(monkeypaassert error.value.provider == "custom"assert error.value.code == "missing_api_key"- with pytest.raises(rp.AuthError, match="Local provider 'ollama'.*base_url") as alias_error:+ with pytest.raises(rp.AuthError, match="provider 'ollama' has no endpoint") as alias_error:rp.resolve_runtime_provider(requested="ollama")assert alias_error.value.provider == "ollama"assert alias_error.value.code == "missing_base_url"@@ -799,69 +799,47 @@ def test_bare_custom_without_credentials_for_remote_endpoint_fails_fast(monkeypa@pytest.mark.parametrize("alias", ("ollama", "vllm"))[email protected]("key_name", ("OPENROUTER_API_KEY", "OPENAI_API_KEY"))-def test_local_alias_without_endpoint_does_not_fall_back_to_openrouter(monkeypatch, alias, key_name):- """#113703: local aliases need an endpoint before cloud credentials can resolve."""- monkeypatch.delenv("OPENAI_API_KEY", raising=False)- monkeypatch.delenv("OPENROUTER_API_KEY", raising=False)- monkeypatch.delenv("CUSTOM_BASE_URL", raising=False)- monkeypatch.delenv("OPENROUTER_BASE_URL", raising=False)- monkeypatch.setenv(key_name, "test-cloud-key")+def test_local_alias_without_any_endpoint_never_reaches_openrouter(monkeypatch, alias):+ """#113703: a local alias with no endpoint configured anywhere must raise a typed AuthError+ naming the alias instead of walking the ladder to the OpenRouter fallback with a cloud key.+ Neither ``OPENROUTER_BASE_URL`` (a mirror, not the alias endpoint) nor an explicit api_key+ (meant for the alias's own server) lifts the guard; bare ``custom`` keeps its own contract."""+ for name in ("OPENAI_API_KEY", "CUSTOM_BASE_URL", "OPENROUTER_BASE_URL"):+ monkeypatch.delenv(name, raising=False)+ monkeypatch.setenv("OPENROUTER_API_KEY", "sk-or-v1-cloud-key")monkeypatch.setattr(rp, "load_config", lambda: {"model": {"provider": alias}})monkeypatch.setattr(rp, "load_pool", lambda _provider: SimpleNamespace(has_credentials=lambda: False))- with pytest.raises(rp.AuthError, match=rf"Local provider '{alias}'.*base_url") as error:+ with pytest.raises(rp.AuthError, match=rf"provider '{alias}' has no endpoint.*providers\.{alias}\.base_url") as error:rp.resolve_runtime_provider(requested=alias)-- assert error.value.provider == alias- assert error.value.code == "missing_base_url"--[email protected]("alias", ("ollama", "vllm"))-def test_local_alias_with_configured_model_endpoint_still_resolves(monkeypatch, alias):- """#113703 control: model.base_url remains a valid local-alias endpoint."""- monkeypatch.setenv("OPENROUTER_API_KEY", "test-cloud-key")- monkeypatch.setattr(- rp,- "load_config",- lambda: {"model": {"provider": alias, "base_url": "http://127.0.0.1:8000/v1"}},- )+ assert (error.value.provider, error.value.code) == (alias, "missing_base_url")++ bare = rp.resolve_runtime_provider(requested="custom")+ assert bare["provider"] == "custom" and bare["api_key"] == "sk-or-v1-cloud-key"++ monkeypatch.setenv("OPENROUTER_BASE_URL", "https://mirror.example/api/v1")+ with pytest.raises(rp.AuthError, match="has no endpoint"):+ rp.resolve_runtime_provider(requested=alias, explicit_api_key="key-for-my-local-server")++[email protected]("configured", ("providers", "model", "explicit"))+def test_local_alias_with_an_endpoint_anywhere_still_resolves_to_it(monkeypatch, configured):+ """#113703 control: every place an ollama endpoint can be configured keeps resolving to that+ URL with the local placeholder key — the guard keys on the absence of an endpoint, not on the+ alias name (the name-keyed version broke ``/model <direct-alias>``; see a9fabe43c4)."""+ monkeypatch.setenv("OPENROUTER_API_KEY", "sk-or-v1-cloud-key")+ url = "http://192.168.1.5:11434/v1"+ config = {+ "providers": {"providers": {"ollama": {"base_url": url}}},+ "model": {"model": {"provider": "ollama", "base_url": url}},+ "explicit": {"model": {"provider": "ollama"}},+ }[configured]+ monkeypatch.setattr(rp, "load_config", lambda: config)monkeypatch.setattr(rp, "load_pool", lambda _provider: SimpleNamespace(has_credentials=lambda: False))- resolved = rp.resolve_runtime_provider(requested=alias)-- assert resolved["provider"] == "custom"- assert resolved["base_url"] == "http://127.0.0.1:8000/v1"- assert resolved["api_key"] == "no-key-required"+ resolved = rp.resolve_runtime_provider(requested="ollama", explicit_base_url=url if configured == "explicit" else None)-[email protected]("alias", ("ollama", "vllm"))-def test_local_alias_with_explicit_endpoint_still_resolves(monkeypatch, alias):- """#113703 control: one-off explicit endpoints bypass the missing-endpoint guard."""- monkeypatch.setenv("OPENROUTER_API_KEY", "test-cloud-key")- monkeypatch.setattr(rp, "load_config", lambda: {"model": {"provider": alias}})-- resolved = rp.resolve_runtime_provider(requested=alias, explicit_base_url="http://127.0.0.1:8000/v1")-- assert resolved["provider"] == "custom"- assert resolved["base_url"] == "http://127.0.0.1:8000/v1"- assert resolved["api_key"] == "no-key-required"--[email protected]("alias", ("ollama", "vllm"))-def test_local_alias_with_named_custom_provider_still_resolves(monkeypatch, alias):- """#113703 control: a named endpoint may keep a local-alias name."""- monkeypatch.setattr(- rp,- "load_config",- lambda: {"providers": {alias: {"api": "http://127.0.0.1:8000/v1"}}},- )-- resolved = rp.resolve_runtime_provider(requested=alias)-- assert resolved["provider"] == "custom"- assert resolved["base_url"] == "http://127.0.0.1:8000/v1"- assert resolved["api_key"] == "no-key-required"+ assert (resolved["provider"], resolved["base_url"], resolved["api_key"]) == ("custom", url, "no-key-required")def test_bare_custom_without_credentials_keeps_loopback_noauth(monkeypatch):diff --git a/website/docs/guides/local-ollama-setup.md b/website/docs/guides/local-ollama-setup.mdindex 2bb4b7cc311..28dbf33f10f 100644--- a/website/docs/guides/local-ollama-setup.md+++ b/website/docs/guides/local-ollama-setup.md@@ -268,6 +268,16 @@ This way, 90% of your usage is free (local), and only the hard tasks hit the pai## Troubleshooting+### "provider 'ollama' has no endpoint configured"++`hermes chat --provider ollama` (or `vllm`) stops with this error when no endpoint is configured for that alias anywhere — no `providers.ollama.base_url`, no `model.base_url`. Hermes refuses to send the request rather than fall back to OpenRouter with a cloud key (`OPENROUTER_API_KEY` / `OPENAI_API_KEY`) that happens to be set. Add the endpoint:++```yaml+providers:+ ollama:+ base_url: "http://localhost:11434/v1"+```+### "Connection refused" on startupOllama isn't running. Start it: