Skip to content

The agent that grows with you

fix(models): only cache unreachability, not HTTP errors; key the entry via base_url_origin

kshitijk4poore2bd40023315
diff --git a/hermes_cli/models.py b/hermes_cli/models.py
index c082e776b79..77e203696f8 100644
--- a/hermes_cli/models.py
+++ b/hermes_cli/models.py
@@ -2172,20 +2172,11 @@ _PROBE_NEG_TTL = 60.0 # seconds
def _probe_neg_key(base_url: str) -> Optional[str]:
- """Return a ``host:port`` key for *base_url*, or None if it has no host."""
- normalized = (base_url or "").strip().lower()
- if not normalized:
- return None
- url = normalized if "://" in normalized else f"http://{normalized}"
- try:
- parsed = urllib.parse.urlparse(url)
- host = parsed.hostname or ""
- if not host:
- return None
- port = parsed.port or (443 if parsed.scheme == "https" else 80)
- except Exception:
- return None
- return f"{host}:{port}"
+ """``host:port`` for *base_url* (both URL candidates share one entry), or None without a host."""
+ from utils import base_url_origin
+
+ _, host, port = base_url_origin(base_url)
+ return f"{host}:{port}" if host else None
def _probe_result(
@@ -2219,7 +2210,6 @@ def probe_api_models(
candidates.append((alternate_base, True))
tried: list[str] = []
- # ponytail: short-TTL negative cache; full re-probe once it expires.
_neg_key = _probe_neg_key(normalized)
if _neg_key is not None:
_neg_seen = _probe_neg_cache.get(_neg_key)
@@ -2249,11 +2239,17 @@ def probe_api_models(
_ssl_context = _custom_provider_ssl_context(normalized)
if _ssl_context is not None:
_open_kwargs["ssl_context"] = _ssl_context
+ reachable = False
for candidate_base, is_fallback in candidates:
url = candidate_base.rstrip("/") + "/models"
tried.append(url)
try:
data = _get_json(url, timeout=timeout, headers=headers, **_open_kwargs)
+ except urllib.error.HTTPError:
+ # The host answered: an auth/404 failure is not unreachability, and a user fixing
+ # their key must not be served a cached "no models" for the next TTL window.
+ reachable = True
+ continue
except Exception:
continue
if _neg_key is not None:
@@ -2262,7 +2258,7 @@ def probe_api_models(
[m.get("id", "") for m in data.get("data", [])], url, candidate_base.rstrip("/"),
alternate_base if alternate_base != candidate_base else normalized, is_fallback)
- if _neg_key is not None:
+ if _neg_key is not None and not reachable:
_probe_neg_cache[_neg_key] = time.monotonic()
return _probe_result(
None, tried[0] if tried else normalized.rstrip("/") + "/models", normalized,
diff --git a/tests/hermes_cli/test_cached_fetch_api_models.py b/tests/hermes_cli/test_cached_fetch_api_models.py
index da03301207c..f321244cc1f 100644
--- a/tests/hermes_cli/test_cached_fetch_api_models.py
+++ b/tests/hermes_cli/test_cached_fetch_api_models.py
@@ -410,6 +410,19 @@ class TestProbeApiModelsNegativeCache:
assert calls == [] # /v1 + root share one host:port entry
assert r2["probed_url"] == "https://blackhole.invalid/v1/models"
+ def test_http_error_from_a_reachable_host_is_not_cached_as_unreachable(self, monkeypatch):
+ import urllib.error
+
+ import hermes_cli.models as mod
+
+ def _unauthorized(req, **kw):
+ raise urllib.error.HTTPError(req.full_url, 401, "Unauthorized", {}, None)
+
+ monkeypatch.setattr(mod, "_urlopen_model_catalog_request", _unauthorized)
+ assert mod.probe_api_models("bad-key", "https://reachable.invalid/v1", timeout=1.0)["models"] is None
+ # The host answered; a corrected key must probe again immediately.
+ assert "reachable.invalid:443" not in mod._probe_neg_cache
+
def test_expired_entry_reprobes_and_success_clears_it(self, monkeypatch):
import hermes_cli.models as mod