The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(nous): a hanging mirror costs 2 s, and an API-key-only profile keeps its background refresh
Teknium7bc3f149335d
diff --git a/hermes_cli/model_catalog.py b/hermes_cli/model_catalog.pyindex a520fed9c4e..8927b9c950d 100644--- a/hermes_cli/model_catalog.py+++ b/hermes_cli/model_catalog.py@@ -298,12 +298,14 @@ def refresh_catalogs() -> bool:clear_fetch_backoff() # provider overrides are re-asked too, not only the forced manifestcatalog = get_catalog(force_refresh=True)try:+ from agent.secret_scope import get_secretfrom hermes_cli.anon_auth import current_nous_state, is_guest_statefrom hermes_cli.auth import read_credential_poolfrom hermes_cli.models import _resolve_nous_portal_url, fetch_nous_recommended_models, fetch_openrouter_modelsfetch_openrouter_models(force_refresh=True)- if any(e and not is_guest_state(e) for e in [current_nous_state(), *read_credential_pool("nous")]):+ logins = [current_nous_state(), *read_credential_pool("nous")]+ if any(e and not is_guest_state(e) for e in logins) or get_secret("NOUS_API_KEY"):fetch_nous_recommended_models(_resolve_nous_portal_url(), force_refresh=True)except Exception:logger.debug("provider catalog refresh failed", exc_info=True)diff --git a/hermes_cli/models.py b/hermes_cli/models.pyindex 6b040633890..0a00250817d 100644--- a/hermes_cli/models.py+++ b/hermes_cli/models.py@@ -347,6 +347,7 @@ def check_nous_free_tier(*, force_fresh: bool = False, cached_only: bool = False# ---------------------------------------------------------------------------NOUS_RECOMMENDED_MODELS_MIRROR = "https://web-assets.nousresearch.com/dynamic"+_NOUS_MIRROR_TIMEOUT: float = 2.0 # seconds: a hanging mirror delays the Portal fallback by at most this_NOUS_RECOMMENDED_CACHE_TTL: int = 600 # seconds (10 minutes)# (result_dict, monotonic timestamp), scoped to the profile and portal._nous_recommended_cache: dict[tuple[str, str], tuple[dict[str, Any], float]] = {}@@ -403,10 +404,11 @@ def fetch_nous_recommended_models(_nous_recommended_cache[cache_key] = (data, now - age)return dataparts = urllib.parse.urlsplit(base)- mirror = [] if is_loopback_host(parts.hostname) else [f"{NOUS_RECOMMENDED_MODELS_MIRROR}/{parts.netloc}"]- for origin in [*mirror, base]: # the Portal itself when its mirror is missing (404) or failing+ mirror = [] if is_loopback_host(parts.hostname) else [+ (f"{NOUS_RECOMMENDED_MODELS_MIRROR}/{parts.netloc}", min(timeout, _NOUS_MIRROR_TIMEOUT))]+ for origin, origin_timeout in [*mirror, (base, timeout)]: # the Portal itself when its mirror is missing (404) or failingtry:- data = _get_json(f"{origin}/api/nous/recommended-models", timeout=timeout,+ data = _get_json(f"{origin}/api/nous/recommended-models", timeout=origin_timeout,headers={"Accept": "application/json", "Accept-Encoding": "gzip"})except Exception:continuediff --git a/tests/hermes_cli/test_nous_recommendation_mirror.py b/tests/hermes_cli/test_nous_recommendation_mirror.pyindex 342a5b7208d..213c670d21f 100644--- a/tests/hermes_cli/test_nous_recommendation_mirror.py+++ b/tests/hermes_cli/test_nous_recommendation_mirror.py@@ -1,7 +1,9 @@"""Nous recommendations come from each Portal's static mirror (the Portal itself when the mirror has-no copy), and the gateway's background refresh fetches them only for a profile with a Nous account."""+no copy or stalls), and the gateway's background refresh fetches them only for a profile with a Nous+account."""import jsonimport threading+import timefrom http.server import BaseHTTPRequestHandler, ThreadingHTTPServerimport pytest@@ -12,14 +14,18 @@ PAYLOAD = {"paidRecommendedModels": [{"modelName": "mirrored"}]}@pytest.fixture-def paths(monkeypatch):- """A local server standing in for the mirror (which holds no copy for a 127.0.0.1 Portal) and for- a Portal at its own origin, recording the paths it is asked for."""- seen = []+def server(monkeypatch):+ """A local server standing in for the mirror (which holds no copy for a 127.0.0.1 Portal, or stalls+ on it when ``mirror_stalls`` is set) and for a Portal at its own origin, recording the paths asked."""+ state = {"seen": [], "mirror_stalls": False}+ release = threading.Event()class Handler(BaseHTTPRequestHandler):def do_GET(self):- seen.append(self.path)+ state["seen"].append(self.path)+ if self.path.startswith("/dynamic/127.0.0.1") and state["mirror_stalls"]:+ release.wait(30)+ returnself.send_response(404 if self.path.startswith("/dynamic/127.0.0.1") else 200)self.end_headers()self.wfile.write(json.dumps(PAYLOAD).encode())@@ -28,11 +34,13 @@ def paths(monkeypatch):passhttpd = ThreadingHTTPServer(("127.0.0.1", 0), Handler)+ httpd.daemon_threads = Truethread = threading.Thread(target=httpd.serve_forever, daemon=True)thread.start()from hermes_cli import modelsmonkeypatch.setattr(models, "NOUS_RECOMMENDED_MODELS_MIRROR", f"http://127.0.0.1:{httpd.server_port}/dynamic")- yield seen+ yield state+ release.set()httpd.shutdown()httpd.server_close()@@ -41,38 +49,46 @@ def paths(monkeypatch):("", "portal.nousresearch.com"),("https://portal.staging-nousresearch.com/", "portal.staging-nousresearch.com"),])-def test_recommendations_come_from_the_portal_hosts_mirror(paths, portal, host):+def test_recommendations_come_from_the_portal_hosts_mirror(server, portal, host):from hermes_cli.models import fetch_nous_recommended_modelsassert fetch_nous_recommended_models(portal, force_refresh=True) == PAYLOAD- assert paths == [f"/dynamic/{host}/api/nous/recommended-models"]+ assert server["seen"] == [f"/dynamic/{host}/api/nous/recommended-models"]-def test_a_portal_missing_from_the_mirror_is_asked_directly(paths, monkeypatch):[email protected]("mirror_stalls", [False, True], ids=["mirror-404", "mirror-stalls"])+def test_a_portal_the_mirror_cannot_serve_is_asked_directly(server, monkeypatch, mirror_stalls):from hermes_cli import models+ server["mirror_stalls"] = mirror_stallsmonkeypatch.setattr(models, "is_loopback_host", lambda _host: False) # treat the local server as remoteportal = models.NOUS_RECOMMENDED_MODELS_MIRROR.removesuffix("/dynamic")- assert models.fetch_nous_recommended_models(portal, force_refresh=True) == PAYLOAD+ started = time.monotonic()+ assert models.fetch_nous_recommended_models(portal, timeout=10.0, force_refresh=True) == PAYLOAD+ # A stalled mirror costs its own short timeout, not the caller's whole budget.+ assert time.monotonic() - started < 6.0host = portal.removeprefix("http://")- assert paths == [f"/dynamic/{host}/api/nous/recommended-models", "/api/nous/recommended-models"]+ assert server["seen"] == [f"/dynamic/{host}/api/nous/recommended-models", "/api/nous/recommended-models"][email protected]("auth_store, host", [- (None, None),- ({"providers": {"nous": {"auth_method": "anonymous", "access_token": "guest"}}}, None),[email protected]("auth_store, env_key, host", [+ (None, None, None),+ ({"providers": {"nous": {"auth_method": "anonymous", "access_token": "guest"}}}, None, None),({"providers": {"nous": {"access_token": "a", "portal_base_url": "https://portal.staging-nousresearch.com"}}},- "portal.staging-nousresearch.com"),- ({"credential_pool": {"nous": [{"access_token": "a"}]}}, "portal.nousresearch.com"),-], ids=["no-account", "guest", "signed-in", "pooled"])-def test_gateway_refresh_fetches_recommendations_only_for_a_nous_account(paths, monkeypatch, auth_store, host):+ None, "portal.staging-nousresearch.com"),+ ({"credential_pool": {"nous": [{"access_token": "a"}]}}, None, "portal.nousresearch.com"),+ (None, "sk-nous-test", "portal.nousresearch.com"),+], ids=["no-account", "guest", "signed-in", "pooled", "api-key-env"])+def test_gateway_refresh_fetches_recommendations_only_for_a_nous_account(server, monkeypatch, auth_store, env_key, host):from hermes_cli import model_catalog, modelsmonkeypatch.setattr(model_catalog, "get_catalog", lambda **_kw: {})monkeypatch.setattr(models, "fetch_openrouter_models", lambda **_kw: [])if auth_store is not None:(get_hermes_home() / "auth.json").write_text(json.dumps(auth_store), encoding="utf-8")+ if env_key is not None:+ monkeypatch.setenv("NOUS_API_KEY", env_key)model_catalog.refresh_catalogs()- assert paths == ([f"/dynamic/{host}/api/nous/recommended-models"] if host else [])+ assert server["seen"] == ([f"/dynamic/{host}/api/nous/recommended-models"] if host else [])