Skip to content

The agent that grows with you

fix(auxiliary): scope the pool peek and recovery of a pinned custom entry

unsupportedpastels6c0384cf08b6
diff --git a/agent/auxiliary_client.py b/agent/auxiliary_client.py
index 707182dc727..4403b1c4e9f 100644
--- a/agent/auxiliary_client.py
+++ b/agent/auxiliary_client.py
@@ -120,6 +120,7 @@ def aux_probe_mode():
from agent.credential_pool import load_pool
+from agent.custom_entry_pool import entry_scoped_pool, pool_for_aux_provider
from agent.model_metadata import MINIMUM_CONTEXT_LENGTH, get_model_context_length
from hermes_cli.config import get_hermes_home
from hermes_cli.config_providers import _canonical_api_mode
@@ -3683,8 +3684,8 @@ def _recoverable_pool_provider(
"endpoint mismatch, not a dead key; skipping credential rotation",
normalized, base_url_hostname(base), base_url_hostname(rt_base))
return None
- if normalized not in {"", "auto", "custom"}:
- return normalized
+ if normalized not in {"", "auto", "custom"}: # a pinned ``custom:<name>`` keeps its entry's identity
+ return pinned if (pinned := str(resolved_provider or "").strip().lower()).startswith("custom:") else normalized
known = _provider_for_host(base, _POOL_PROVIDER_BY_HOST)
if known is not None:
return known
@@ -3715,7 +3716,7 @@ def _recover_provider_pool(provider: str, exc: Exception, *, failed_api_key: str
"""
normalized = _normalize_aux_provider(provider)
try:
- pool = load_pool(normalized)
+ pool = pool_for_aux_provider(provider, normalized, load_pool)
except Exception as load_exc:
logger.debug("Auxiliary client: could not load pool for %s recovery: %s", normalized, load_exc)
return False
@@ -4777,7 +4778,6 @@ def _named_custom_api_key(custom_entry: dict[str, Any], provider: str, custom_ba
if not custom_key:
with contextlib.suppress(Exception):
from agent.credential_pool import custom_provider_pool_key_candidates
- from agent.custom_entry_pool import entry_scoped_pool
pool_name = custom_entry.get("provider_key") or custom_entry.get("name") or provider
for pool_key in custom_provider_pool_key_candidates(custom_base, pool_name):
try:
@@ -6045,11 +6045,11 @@ def _get_cached_client(
del _client_cache[cache_key]
# Build outside the lock. For pool-backed providers derive the key from the pool entry:
# resolve_api_key_provider_credentials prefers env vars, which would bypass pool rotation
- # and retry an exhausted key.
+ # and retry an exhausted key. A pinned ``custom:<name>`` peeks only its entry's rows.
effective_api_key = api_key
if not effective_api_key:
- _pe = _peek_pool_entry(_normalize_aux_provider(provider))
- if _pe is not None:
+ _pool = pool_for_aux_provider(provider, _normalize_aux_provider(provider), lambda k: _load_pool_with_credentials(k, " (peek)"))
+ if _pool is not None and (_pe := _peek_pool_entry(provider, _pool)) is not None:
effective_api_key = _pool_runtime_api_key(_pe) or api_key
client, default_model = resolve_provider_client(
provider, model, async_mode, explicit_base_url=base_url, explicit_api_key=effective_api_key,
diff --git a/agent/custom_entry_pool.py b/agent/custom_entry_pool.py
index 69c9014dc27..1a73ba2e9a1 100644
--- a/agent/custom_entry_pool.py
+++ b/agent/custom_entry_pool.py
@@ -10,7 +10,7 @@ declared credential. The built-in provider itself still loads, and persists, the
from __future__ import annotations
-from typing import Any, Optional
+from typing import Any, Callable, Optional
_DECLARED_CREDENTIAL_KEYS = ("api_key", "key_env", "api_key_env", "key_cmd")
@@ -61,15 +61,30 @@ def custom_entry_for_endpoint(
if not base_url:
return None
url = cp._norm_url(base_url)
- aliases = cp._requested_custom_name_aliases(provider_name) if provider_name else set()
- if aliases:
- for norm_name, entry in cp._iter_custom_providers(config):
- if aliases & cp._custom_entry_name_aliases(norm_name, entry):
- entry_url = cp._norm_url(entry.get("base_url"))
- return None if entry_url and entry_url != url else (norm_name, entry)
+ if (named := named_custom_entry(provider_name, config)) is not None:
+ entry_url = cp._norm_url(named[1].get("base_url"))
+ return None if entry_url and entry_url != url else named
return next(((n, e) for n, e in cp._iter_custom_providers(config) if url and cp._norm_url(e.get("base_url")) == url), None)
+def named_custom_entry(provider_name: Optional[str], config: Optional[dict] = None) -> Optional[tuple[str, dict[str, Any]]]:
+ """``(normalized_name, entry)`` of the configured custom entry ``provider_name`` names, else None."""
+ from agent import credential_pool as cp
+ aliases = cp._requested_custom_name_aliases(provider_name) if provider_name else set()
+ return next(((n, e) for n, e in cp._iter_custom_providers(config) if aliases & cp._custom_entry_name_aliases(n, e)), None) if aliases else None
+
+
+def pool_for_aux_provider(provider: Optional[str], pool_key: str, load: Callable[[str], Any]) -> Any:
+ """``load(pool_key)`` for an auxiliary pool read (the key peek, credential recovery). A pinned
+ ``custom:<name>`` normalizes to its entry's slug, which may also be a same-named built-in's pool,
+ so it gets the entry-scoped view; a bare built-in name keeps its whole pool."""
+ pool = load(pool_key)
+ if pool is None or not str(provider or "").strip().lower().startswith("custom:"):
+ return pool
+ match = named_custom_entry(provider)
+ return entry_scoped_pool(pool, pool_key, match[1] if match else None)
+
+
def load_runtime_pool(provider: Optional[str], base_url: Optional[str], requested_provider: Optional[str] = None) -> Any:
"""The pool for a runtime snapshot's identity. A custom runtime (``custom`` or ``custom:<name>``)
is the entry's, so it gets the entry-scoped view; a built-in identity loads its whole pool.
diff --git a/tests/hermes_cli/test_custom_entry_shadow_key_env.py b/tests/hermes_cli/test_custom_entry_shadow_key_env.py
index 18fdb490cc3..9b123fe6a4a 100644
--- a/tests/hermes_cli/test_custom_entry_shadow_key_env.py
+++ b/tests/hermes_cli/test_custom_entry_shadow_key_env.py
@@ -350,3 +350,58 @@ def test_a_genuinely_bare_custom_snapshot_still_takes_the_first_entry_on_its_url
from agent.custom_entry_pool import load_runtime_pool
assert MANUAL_KEY in {e.access_token for e in load_runtime_pool("custom", ENDPOINT).entries()}
+
+
+def _pin_title_generation(home, provider):
+ cfg = yaml.safe_load((home / "config.yaml").read_text())
+ cfg["auxiliary"] = {"title_generation": {"provider": provider, "model": "m"}}
+ (home / "config.yaml").write_text(yaml.safe_dump(cfg), encoding="utf-8")
+
+
+def test_aux_task_pinned_to_the_entry_sends_its_key_env(tmp_path, monkeypatch):
+ """``auxiliary.title_generation.provider: custom:deepseek`` through ``call_llm``: the pool peek
+ read the built-in's whole ``deepseek`` pool and its env row beat the entry's key_env."""
+ from unittest.mock import MagicMock, patch
+
+ home = _home(tmp_path, monkeypatch, "deepseek", "DEEPSEEK_API_KEY")
+ _pin_title_generation(home, "custom:deepseek")
+ from agent import auxiliary_client as ac
+
+ monkeypatch.setattr(ac, "_client_cache", {})
+ with patch("agent.auxiliary_client.OpenAI") as mock_openai:
+ mock_openai.return_value.base_url = ENDPOINT
+ mock_openai.return_value.chat.completions.create.return_value = MagicMock()
+ ac.call_llm(task="title_generation", messages=[{"role": "user", "content": "hi"}])
+ sent = {(c.kwargs.get("base_url"), c.kwargs.get("api_key")) for c in mock_openai.call_args_list}
+ assert sent == {(ENDPOINT, ENTRY_KEY)}
+
+
+def test_aux_task_pinned_to_the_builtin_keeps_its_env_key(tmp_path, monkeypatch):
+ from unittest.mock import patch
+
+ _home(tmp_path, monkeypatch, "deepseek", "DEEPSEEK_API_KEY")
+ from agent import auxiliary_client as ac
+
+ monkeypatch.setattr(ac, "_client_cache", {})
+ with patch("agent.auxiliary_client.OpenAI") as mock_openai:
+ ac._get_cached_client("deepseek", "m")
+ assert mock_openai.call_args.kwargs["api_key"] == BUILTIN_KEY
+
+
+def test_aux_auth_failure_on_the_entry_never_rotates_the_builtin_pool(tmp_path, monkeypatch):
+ """A 401 from the entry's endpoint says nothing about the built-in's env-seeded row (the
+ ladder's pool-recovery rung, from the pinned ``custom:<name>`` route to the rotation)."""
+ from types import SimpleNamespace
+
+ _home(tmp_path, monkeypatch, "deepseek", "DEEPSEEK_API_KEY")
+ from agent import auxiliary_client as ac
+ from agent.credential_pool import load_pool
+
+ load_pool("deepseek")
+ pool_provider = ac._recoverable_pool_provider("custom:deepseek", SimpleNamespace(base_url=ENDPOINT, api_key=ENTRY_KEY))
+ assert pool_provider
+ err = type("AuthError", (Exception,), {"status_code": 401})("Error code: 401")
+ assert ac._recover_provider_pool(pool_provider, err, failed_api_key=ENTRY_KEY) is False
+ assert ac._recover_provider_pool(pool_provider, err) is False
+ builtin = [e for e in load_pool("deepseek").entries() if e.source == "env:DEEPSEEK_API_KEY"]
+ assert builtin and all(e.last_status != "exhausted" for e in builtin)