The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(model): block local aliases without endpoints
KoNit-K47b5e578f588
diff --git a/hermes_cli/runtime_provider.py b/hermes_cli/runtime_provider.pyindex 09e3baa6d59..b2b7e7826f1 100644--- a/hermes_cli/runtime_provider.py+++ b/hermes_cli/runtime_provider.py@@ -763,6 +763,7 @@ 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:@@ -776,6 +777,32 @@ def _raise_if_provider_disabled(requested_provider: str) -> None:f"(providers.{requested_provider}.enabled: false)")+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:+ return+ if str(explicit_base_url or "").strip() or get_secret_str("CUSTOM_BASE_URL", "").strip():+ return+ model_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)):+ return+ if _get_named_custom_provider(requested_provider) is not None:+ return+ raise AuthError(+ f"Local provider '{requested_provider}' needs a base_url. Configure model.base_url or select a configured provider endpoint.",+ provider=requested_provider,+ code="missing_base_url",+ )++def _resolve_vertex_runtime(requested_provider: str) -> Dict[str, Any]:"""Vertex AI (OAuth2). The credential *path* (GOOGLE_APPLICATION_CREDENTIALS) must never betreated as a static API key; a short-lived token is minted per call, and mid-session expiry is@@ -855,6 +882,7 @@ def resolve_runtime_provider(*, requested: Optional[str] = None, explicit_api_keOpenCode Zen/Go where different models route through different API surfaces)."""requested_provider = resolve_requested_provider(requested)_raise_if_provider_disabled(requested_provider)+ _raise_if_local_alias_missing_endpoint(requested_provider, explicit_base_url)runtime = next(r for r in _ladder_rungs(requested_provider, explicit_api_key, explicit_base_url, target_model) if r)_raise_for_credentialless_bare_custom(requested_provider, runtime)return runtimediff --git a/tests/hermes_cli/test_runtime_provider_resolution.py b/tests/hermes_cli/test_runtime_provider_resolution.pyindex b255192065d..09b9adab0e3 100644--- a/tests/hermes_cli/test_runtime_provider_resolution.py+++ b/tests/hermes_cli/test_runtime_provider_resolution.py@@ -761,9 +761,9 @@ def test_bare_custom_resolves_providers_dict_entry_named_custom(monkeypatch):def test_bare_custom_without_credentials_for_remote_endpoint_fails_fast(monkeypatch):"""#111741: a bare ``custom`` placeholder that falls through to the OpenRouter default with nokey must raise a typed AuthError naming the request at resolve time, instead of returning a- dead runtime that dies later as "No LLM provider configured". A local alias (``ollama``) in the- same state keeps resolving tolerantly: ``/model`` direct-alias switching supplies its endpoint- after this call. With an OpenRouter key present the bare request resolves exactly as before."""+ dead runtime that dies later as "No LLM provider configured". Local aliases also need an+ endpoint: without one they must not adopt a cloud credential through OpenRouter. With an+ OpenRouter key present, the bare custom request resolves exactly as before."""monkeypatch.delenv("OPENAI_API_KEY", raising=False)monkeypatch.delenv("OPENROUTER_API_KEY", raising=False)monkeypatch.delenv("CUSTOM_BASE_URL", raising=False)@@ -787,8 +787,10 @@ def test_bare_custom_without_credentials_for_remote_endpoint_fails_fast(monkeypaassert error.value.provider == "custom"assert error.value.code == "missing_api_key"- alias = rp.resolve_runtime_provider(requested="ollama")- assert alias["provider"] == "custom" and not alias["api_key"]+ with pytest.raises(rp.AuthError, match="Local provider 'ollama'.*base_url") as alias_error:+ rp.resolve_runtime_provider(requested="ollama")+ assert alias_error.value.provider == "ollama"+ assert alias_error.value.code == "missing_base_url"monkeypatch.setenv("OPENROUTER_API_KEY", "sk-or-v1-usable-key")resolved = rp.resolve_runtime_provider(requested="custom")@@ -796,6 +798,72 @@ def test_bare_custom_without_credentials_for_remote_endpoint_fails_fast(monkeypaassert resolved["api_key"] == "sk-or-v1-usable-key"[email protected]("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")+ 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:+ 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"}},+ )+ 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"++[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"++def test_bare_custom_without_credentials_keeps_loopback_noauth(monkeypatch):"""Control: a configured no-auth local endpoint still resolves with the placeholder key."""monkeypatch.setattr(