Skip to content

The agent that grows with you

test(auth): cover keyed providers.<key> credential-pool lookup

xxxigm43470980bf82
diff --git a/tests/agent/test_auxiliary_named_custom_providers.py b/tests/agent/test_auxiliary_named_custom_providers.py
index ca49f167987..40336b720c4 100644
--- a/tests/agent/test_auxiliary_named_custom_providers.py
+++ b/tests/agent/test_auxiliary_named_custom_providers.py
@@ -1,5 +1,6 @@
"""Tests for named custom provider and 'main' alias resolution in auxiliary_client."""
+import json
from unittest.mock import patch, MagicMock
import pytest
@@ -122,6 +123,38 @@ class TestResolveProviderClientNamedCustom:
assert client is not None
# no-key-required should be used
+ def test_providers_dict_uses_durable_pool_when_no_inline_key(self, tmp_path):
+ """Titles/compression/vision must read credential_pool.<key>, not a placeholder."""
+ _write_config(tmp_path, {
+ "providers": {
+ "b-ai": {
+ "name": "B.AI",
+ "base_url": "https://api.b.ai/v1",
+ },
+ },
+ })
+ auth_path = tmp_path / ".hermes" / "auth.json"
+ auth_path.write_text(json.dumps({
+ "version": 1,
+ "providers": {},
+ "credential_pool": {
+ "b-ai": [
+ {
+ "id": "k1",
+ "label": "primary",
+ "auth_type": "api_key",
+ "priority": 0,
+ "source": "manual",
+ "access_token": "sk-real-b-ai-pool-key-12345",
+ }
+ ]
+ },
+ }))
+ from agent.auxiliary_client import resolve_provider_client
+ client, _model = resolve_provider_client("b-ai", "b-ai-model")
+ assert client is not None
+ assert "api.b.ai" in str(client.base_url)
+ assert client.api_key == "sk-real-b-ai-pool-key-12345"
class TestResolveProviderClientModelNormalization:
diff --git a/tests/agent/test_credential_pool_provider_boundary.py b/tests/agent/test_credential_pool_provider_boundary.py
index 5eb5edc16d9..00bbc947ad4 100644
--- a/tests/agent/test_credential_pool_provider_boundary.py
+++ b/tests/agent/test_credential_pool_provider_boundary.py
@@ -142,6 +142,53 @@ def test_runtime_pool_key_resolves_modern_provider_in_mixed_config():
)
+def test_keyed_provider_pool_matches_runtime_aliases():
+ configured = [
+ (
+ "b.ai",
+ {
+ "name": "B.AI",
+ "provider_key": "b-ai",
+ "base_url": "https://api.b.ai/v1",
+ },
+ )
+ ]
+ with patch("agent.credential_pool._iter_custom_providers", return_value=configured):
+ assert credential_pool_matches_provider(
+ "b-ai", "b-ai", base_url="https://api.b.ai/v1"
+ )
+ assert credential_pool_matches_provider(
+ "b-ai", "custom", base_url="https://api.b.ai/v1"
+ )
+ assert credential_pool_matches_provider(
+ "b-ai", "custom:b.ai", base_url="https://api.b.ai/v1"
+ )
+ assert not credential_pool_matches_provider(
+ "b-ai", "custom", base_url="https://other.example/v1"
+ )
+ assert not credential_pool_matches_provider(
+ "b-ai", "deepseek", base_url="https://api.b.ai/v1"
+ )
+
+
+def test_runtime_pool_key_prefers_durable_provider_slug():
+ endpoint = "https://api.b.ai/v1"
+ configured = [
+ (
+ "b.ai",
+ {
+ "name": "B.AI",
+ "provider_key": "b-ai",
+ "base_url": endpoint,
+ },
+ )
+ ]
+ with patch("agent.credential_pool._iter_custom_providers", return_value=configured):
+ assert resolve_runtime_pool_key("b-ai", endpoint) == "b-ai"
+ assert resolve_runtime_pool_key("custom", endpoint) == "b-ai"
+ assert resolve_runtime_pool_key("custom:b.ai", endpoint) == "b-ai"
+
+
def test_runtime_pool_key_preserves_non_custom_identity():
with patch("agent.credential_pool._iter_custom_providers", return_value=[]):
assert (
diff --git a/tests/hermes_cli/test_keyed_provider_credential_pool.py b/tests/hermes_cli/test_keyed_provider_credential_pool.py
new file mode 100644
index 00000000000..3200a407d06
--- /dev/null
+++ b/tests/hermes_cli/test_keyed_provider_credential_pool.py
@@ -0,0 +1,102 @@
+"""Keyed ``providers.<key>`` entries must use the durable pool slug.
+
+``hermes auth add b-ai`` stores keys under ``credential_pool.b-ai``. Runtime
+used to look up ``custom:<display-name>`` (e.g. ``custom:b.ai`` from
+``name: B.AI``), miss the pool, and send the ``no-key-required`` placeholder
+to an auth-required endpoint (HTTP 401 Invalid api_key format).
+"""
+
+from __future__ import annotations
+
+import json
+
+import yaml
+
+
+POOL_KEY = "sk-real-b-ai-pool-key-12345"
+LEGACY_KEY = "sk-legacy-custom-b-ai-pool-key"
+ENDPOINT = "https://api.b.ai/v1"
+
+
+def _write_keyed_provider_home(tmp_path, monkeypatch, *, pool_id="b-ai", extra_config=None):
+ hermes_home = tmp_path / ".hermes"
+ hermes_home.mkdir()
+ monkeypatch.setenv("HERMES_HOME", str(hermes_home))
+ config = {
+ "model": {"default": "b-ai-model", "provider": "b-ai"},
+ "providers": {
+ "b-ai": {
+ "name": "B.AI",
+ "base_url": ENDPOINT,
+ }
+ },
+ }
+ if extra_config:
+ config["providers"]["b-ai"].update(extra_config)
+ (hermes_home / "config.yaml").write_text(yaml.safe_dump(config), encoding="utf-8")
+ (hermes_home / "auth.json").write_text(
+ json.dumps(
+ {
+ "version": 1,
+ "providers": {},
+ "credential_pool": {
+ pool_id: [
+ {
+ "id": "k1",
+ "label": "primary",
+ "auth_type": "api_key",
+ "priority": 0,
+ "source": "manual",
+ "access_token": POOL_KEY if pool_id == "b-ai" else LEGACY_KEY,
+ }
+ ]
+ },
+ }
+ ),
+ encoding="utf-8",
+ )
+ return hermes_home
+
+
+def test_get_named_custom_provider_exposes_provider_key_and_key_env(
+ tmp_path, monkeypatch
+):
+ _write_keyed_provider_home(
+ tmp_path, monkeypatch, extra_config={"key_env": "B_AI_API_KEY"}
+ )
+ monkeypatch.setenv("B_AI_API_KEY", "sk-from-env-not-the-pool")
+
+ from hermes_cli.runtime_provider import _get_named_custom_provider
+
+ entry = _get_named_custom_provider("b-ai")
+ assert entry is not None
+ assert entry.get("provider_key") == "b-ai"
+ assert entry.get("key_env") == "B_AI_API_KEY"
+ assert entry.get("name") == "B.AI"
+ assert entry.get("base_url") == ENDPOINT
+
+
+def test_keyed_provider_runtime_uses_durable_pool_slug(tmp_path, monkeypatch):
+ """Main turns must send the pooled key, not the no-key-required placeholder."""
+ _write_keyed_provider_home(tmp_path, monkeypatch)
+
+ from hermes_cli import runtime_provider as rp
+
+ resolved = rp.resolve_runtime_provider(requested="b-ai")
+ assert resolved["base_url"] == ENDPOINT
+ assert resolved["api_key"] == POOL_KEY
+ assert resolved["api_key"] != "no-key-required"
+ assert str(resolved.get("source") or "").startswith("pool:")
+
+
+def test_keyed_provider_runtime_falls_back_to_legacy_custom_namespace(
+ tmp_path, monkeypatch
+):
+ """Older auth.json rows stored under custom:<display-name> must still work."""
+ _write_keyed_provider_home(tmp_path, monkeypatch, pool_id="custom:b.ai")
+
+ from hermes_cli import runtime_provider as rp
+
+ resolved = rp.resolve_runtime_provider(requested="b-ai")
+ assert resolved["api_key"] == LEGACY_KEY
+ assert resolved["api_key"] != "no-key-required"