The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(picker): a rate-limited credential pool never hides its provider from a picker
Brooklyn Nicholson6590f13a1ba2
diff --git a/hermes_cli/inventory.py b/hermes_cli/inventory.pyindex 62ca4e3864b..cc41e49f342 100644--- a/hermes_cli/inventory.py+++ b/hermes_cli/inventory.py@@ -236,12 +236,13 @@ def build_model_options_payload(``for_picker=True`` keeps providers whose credential pool is entirely rate-limited visible:these are human-facing pickers, and hiding a temporarily exhausted pool makes providers vanishmid-session even though another model under the same provider may still work (same contract- as ``/model`` and the aux pickers, #66584 / #66624)."""+ as ``/model`` and the aux pickers, #66584 / #66624). Visibility only: ``fast_custom_probe=False``+ keeps the live probe of the current custom endpoint on its full 5s discovery budget."""refresh = bool(refresh)payload = build_models_payload(ctx, explicit_only=bool(explicit_only), include_unconfigured=bool(include_unconfigured),picker_hints=True, canonical_order=True, pricing=True, pricing_cache_only=not refresh,- capabilities=True, featured=True, for_picker=True,+ capabilities=True, featured=True, for_picker=True, fast_custom_probe=False,refresh=refresh, probe_custom_providers=refresh, probe_current_custom_provider=not refresh,non_blocking_catalogs=not refresh,)diff --git a/hermes_cli/model_switch_providers.py b/hermes_cli/model_switch_providers.pyindex bf08855fc14..b0860b6c890 100644--- a/hermes_cli/model_switch_providers.py+++ b/hermes_cli/model_switch_providers.py@@ -165,17 +165,19 @@ def _fetch_picker_live_models(_picker_prewarm_done = _threading.Event()-def _credential_pool_is_usable(provider: str, *, raw_pool_present: bool = False) -> bool:+def _credential_pool_is_usable(provider: str, *, raw_pool_present: bool = False, for_picker: bool = False) -> bool:"""Whether *provider* has a credential that can be selected now.Legacy opaque ``auth.json`` pool values that do not deserialize into ``PooledCredential``stay visible (``raw_pool_present``); a real pool's availability is authoritative — an- all-exhausted/dead pool is not authenticated."""+ all-exhausted/dead pool is not authenticated. ``for_picker`` (human-facing pickers) also+ accepts a pool whose entries are all in cooldown: a rate-limited provider is not a signed-out+ one, and limits are per-model for many providers, so another model may still work."""try:from agent.credential_pool import load_poolpool = load_pool(provider)if pool.has_credentials():- return pool.has_available()+ return for_picker or pool.has_available()except Exception:passreturn raw_pool_present@@ -318,21 +320,21 @@ def _auth_store_has_provider(*keys: str) -> bool:return False-def _raw_pool_usable(hermes_id: str) -> bool:+def _raw_pool_usable(hermes_id: str, *, for_picker: bool = False) -> bool:"""Section-1 pool check: only consult the pool when auth.json lists a raw entry."""try:from hermes_cli.auth import _load_auth_storestore = _load_auth_store()if store and store.get("credential_pool", {}).get(hermes_id):- return _credential_pool_is_usable(hermes_id, raw_pool_present=True)+ return _credential_pool_is_usable(hermes_id, raw_pool_present=True, for_picker=for_picker)except Exception:passreturn False-def _pool_usable(slug: str) -> bool:+def _pool_usable(slug: str, *, for_picker: bool = False) -> bool:try:- return _credential_pool_is_usable(slug)+ return _credential_pool_is_usable(slug, for_picker=for_picker)except Exception as exc:logger.debug("Credential pool check failed for %s: %s", slug, exc)return False@@ -837,7 +839,7 @@ def _lap_builtin_rows(b: _PickerBuild, data: dict, user_providers: dict) -> Nonefor hermes_id, mdev_id, pconfig, env_vars in _iter_builtin_candidates(data, b.excluded, b.seen_slugs):# Per-profile scope, never raw os.environ: a secondary profile's picker otherwise listed the# LAUNCH profile's env-keyed providers and hid its own .env-keyed ones.- if not (_any_env(env_vars, _scoped_key_env) or _raw_pool_usable(hermes_id)):+ if not (_any_env(env_vars, _scoped_key_env) or _raw_pool_usable(hermes_id, for_picker=b.for_picker)):continuemodel_ids = _live_or_curated_ids(hermes_id, b.curated, non_blocking=b.non_blocking_catalogs)# A providers.<built-in>.models block extends the discovered catalog; section 3 cannot@@ -878,16 +880,8 @@ def _overlay_has_creds(b: _PickerBuild, pid: str, hermes_slug: str, overlay) -># Full auto-seeding pool check catches external stores (Codex CLI ~/.codex/auth.json)# not yet in auth.json.try:- if _credential_pool_is_usable(hermes_slug):+ if _credential_pool_is_usable(hermes_slug, for_picker=b.for_picker):has_creds = True- elif b.for_picker:- # Show providers whose pool is entirely in cooldown: limits are per-model for- # many providers, so another model may work.- try:- from agent.credential_pool import load_pool- has_creds = load_pool(hermes_slug).has_credentials()- except Exception:- passexcept Exception as exc:logger.debug("Credential pool check failed for %s: %s", hermes_slug, exc)if not has_creds and hermes_slug == "anthropic":@@ -965,7 +959,7 @@ def _lap_canonical_rows(b: _PickerBuild) -> None:sib_vars = set(sib.api_key_env_vars) if sib else set()if lit and lit <= sib_vars < set(cp_config.api_key_env_vars) and cp.slug != b.current_provider:continue- has_creds = has_creds or _auth_store_has_provider(cp.slug) or _pool_usable(cp.slug) or (+ has_creds = has_creds or _auth_store_has_provider(cp.slug) or _pool_usable(cp.slug, for_picker=b.for_picker) or (_is_aws_sdk(cp_config) and _has_aws_sdk_creds_for_listing(cp.slug, b.current_provider))if not has_creds and cp_config is not None and cp_config.auth_type == "external_process":# Subprocess-backed providers own their auth; the binary resolving is the credentialdiff --git a/tests/hermes_cli/test_model_options_rate_limited_pool.py b/tests/hermes_cli/test_model_options_rate_limited_pool.pynew file mode 100644index 00000000000..ac48988e27e--- /dev/null+++ b/tests/hermes_cli/test_model_options_rate_limited_pool.py@@ -0,0 +1,86 @@+"""A rate-limited provider stays in the GUI model picker (#103829, #124510).++Claude Pro/Max and ChatGPT subscriptions hit usage windows constantly. The 429 puts the pooled+credential in cooldown (credential-wide ``exhausted``, or one model's ``model_cooldowns``), and+the Desktop / dashboard / TUI ``model.options`` payload treated that as signed out: the whole+provider vanished from the menu until the window reset, then came back. These tests drive the+real payload builder against a real ``auth.json`` pool in a temp home.+"""++import json+import time++import pytest++from hermes_cli.inventory import build_model_options_payload, load_picker_context+++def _pool_entry(provider: str, cooldown: str) -> dict:+ now = time.time()+ oauth = provider == "anthropic"+ entry = {+ "id": "e1", "label": "subscription", "priority": 0, "last_status": "ok",+ "auth_type": "oauth" if oauth else "api_key",+ "source": "manual:hermes_pkce" if oauth else "manual",+ "access_token": "sk-ant-oat01-test" if oauth else "test-key-123",+ "refresh_token": "refresh" if oauth else None,+ "expires_at_ms": int((now + 6 * 3600) * 1000),+ }+ if cooldown == "exhausted":+ entry.update(last_status="exhausted", last_status_at=now, last_error_code=429,+ last_error_reset_at=now + 3 * 3600)+ else:+ entry["model_cooldowns"] = {"some-model": now + 3 * 3600}+ return entry+++def pooled_home(tmp_path, monkeypatch):+ home = tmp_path / "hermes-home"+ home.mkdir()+ monkeypatch.setenv("HERMES_HOME", str(home))+ # Only the pooled credential under test may authenticate anything.+ (home / "config.yaml").write_text(+ "model:\n provider: nous\n default: test-model\nauth:\n adopt_external_logins: false\n")++ def write(provider: str, cooldown: str) -> None:+ (home / "auth.json").write_text(json.dumps({+ "version": 1, "providers": {},+ "credential_pool": {provider: [_pool_entry(provider, cooldown)]}}))++ return write+++# anthropic reaches the picker through the overlay section, gemini through the models.dev one.[email protected]("provider", ["anthropic", "gemini"])[email protected]("cooldown", ["exhausted", "model_cooldown"])[email protected]("explicit_only", [True, False])+def test_rate_limited_pool_keeps_its_provider_row(pooled_home, provider, cooldown, explicit_only):+ pooled_home(provider, cooldown)++ rows = build_model_options_payload(load_picker_context(), explicit_only=explicit_only)["providers"]++ row = next((r for r in rows if r["slug"] == provider), None)+ assert row is not None, f"a {cooldown} {provider} pool dropped the provider from model.options"+ assert row["models"], "the kept row must still offer the provider's models"+++def test_picker_visibility_keeps_the_full_custom_probe_budget(monkeypatch):+ """Keeping cooldown rows visible must not shorten the current custom endpoint's live probe."""+ monkeypatch.setattr("hermes_cli.models.cached_provider_model_ids", lambda *_a, **_kw: [])+ monkeypatch.setattr("hermes_cli.models.provider_model_ids", lambda *_a, **_kw: [])+ monkeypatch.setattr("hermes_cli.models.fetch_api_models", lambda *_a, **_kw: None)+ monkeypatch.setattr("hermes_cli.models_local.fetch_ollama_local_models", lambda *_a, **_kw: None)++ def answers_after_1_5s(_api_key, _api_url, _native, _preserve, headers=None, timeout=5.0,+ api_mode=None, **_kw):+ return ["slow-discovered-model"] if timeout >= 5.0 else None++ monkeypatch.setattr("hermes_cli.model_switch_providers._fetch_picker_live_models", answers_after_1_5s)+ ctx = load_picker_context().with_overrides(+ current_provider="custom", current_base_url="http://127.0.0.1:9999/v1", current_model="kept-model")++ rows = build_model_options_payload(ctx)["providers"]++ custom = next(r for r in rows if r["slug"] == "custom")+ assert "slow-discovered-model" in custom["models"]