The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(nous): read Portal recommendations from the R2 mirror; gateway refreshes them only for Nous accounts
Robin Fernandes943a972ca7fe
diff --git a/hermes_cli/model_catalog.py b/hermes_cli/model_catalog.pyindex 9a0454913c2..a520fed9c4e 100644--- a/hermes_cli/model_catalog.py+++ b/hermes_cli/model_catalog.py@@ -290,18 +290,21 @@ def refresh_interval_seconds() -> float:def refresh_catalogs() -> bool:- """Force-refresh every remote catalog the picker reads (manifest, OpenRouter live list, Nous Portal- recommendations), writing each disk cache so the next ``/model`` open in ANY process sees them.- Blocking; run it off the event loop."""+ """Force-refresh every remote catalog the picker reads (manifest, OpenRouter live list, and the+ Nous Portal recommendations for a Nous account, the only reader), writing each disk cache so the+ next ``/model`` open in ANY process sees them. Blocking; run it off the event loop."""if not _load_catalog_config()["enabled"]:return Falseclear_fetch_backoff() # provider overrides are re-asked too, not only the forced manifestcatalog = get_catalog(force_refresh=True)try:- from hermes_cli.models import fetch_nous_recommended_models, fetch_openrouter_models+ from hermes_cli.anon_auth import current_nous_state, is_guest_state+ from hermes_cli.auth import read_credential_pool+ from hermes_cli.models import _resolve_nous_portal_url, fetch_nous_recommended_models, fetch_openrouter_modelsfetch_openrouter_models(force_refresh=True)- fetch_nous_recommended_models(force_refresh=True)+ if any(e and not is_guest_state(e) for e in [current_nous_state(), *read_credential_pool("nous")]):+ fetch_nous_recommended_models(_resolve_nous_portal_url(), force_refresh=True)except Exception:logger.debug("provider catalog refresh failed", exc_info=True)return bool(catalog)diff --git a/hermes_cli/models.py b/hermes_cli/models.pyindex 065e82c8a7c..6b040633890 100644--- a/hermes_cli/models.py+++ b/hermes_cli/models.py@@ -27,6 +27,7 @@ from typing import Any, Optional, TYPE_CHECKINGif TYPE_CHECKING:from typing import TypeGuard+from agent.proxy_bypass import is_loopback_hostfrom hermes_cli.route_identity import normalize_route_base_urlfrom hermes_cli.urllib_security import open_credentialed_urlfrom hermes_cli.version_info import hermes_user_agent@@ -345,7 +346,7 @@ def check_nous_free_tier(*, force_fresh: bool = False, cached_only: bool = False# [{modelName}], {paid,free}Recommended{Compaction,Vision}Model: {modelName} | null# ----------------------------------------------------------------------------NOUS_RECOMMENDED_MODELS_PATH = "/api/nous/recommended-models"+NOUS_RECOMMENDED_MODELS_MIRROR = "https://web-assets.nousresearch.com/dynamic"_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]] = {}@@ -387,12 +388,9 @@ def _write_nous_recommended_disk(base: str, data: dict[str, Any]) -> None:def fetch_nous_recommended_models(portal_base_url: str = "", timeout: float = 5.0, *, force_refresh: bool = False) -> dict[str, Any]:- """Fetch the Portal's public ``/api/nous/recommended-models`` payload (no auth).-- Reuse successful results for ``_NOUS_RECOMMENDED_CACHE_TTL`` seconds, including across- process restarts. ``force_refresh`` bypasses both caches. Stale disk data remains a fallback- on live failure; reading it never renews its freshness.- """+ """Fetch the Portal's public recommended models (no auth): its R2 mirror, else the Portal itself.+ Results are reused for ``_NOUS_RECOMMENDED_CACHE_TTL`` s across restarts unless ``force_refresh``;+ stale disk data is the fallback on failure, and reading it never renews its freshness."""base = (portal_base_url or "https://portal.nousresearch.com").rstrip("/")now = time.monotonic()cache_key = (_pricing_profile_key(), base)@@ -404,19 +402,19 @@ def fetch_nous_recommended_models(data, age = disk_nous_recommended_cache[cache_key] = (data, now - age)return data- try:- data = _get_json(- f"{base}{NOUS_RECOMMENDED_MODELS_PATH}", timeout=timeout,- headers={"Accept": "application/json", "Accept-Encoding": "gzip"}- )- if not isinstance(data, dict):- data = {}- except Exception:- data = {}- if data:- _write_nous_recommended_disk(base, data)+ parts = 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+ try:+ data = _get_json(f"{origin}/api/nous/recommended-models", timeout=timeout,+ headers={"Accept": "application/json", "Accept-Encoding": "gzip"})+ except Exception:+ continue+ if isinstance(data, dict) and data:+ _write_nous_recommended_disk(base, data)+ breakelse:- data = disk[0] if disk is not None else data+ data = disk[0] if disk is not None else {}_nous_recommended_cache[cache_key] = (data, now)return datadiff --git a/tests/hermes_cli/test_model_catalog.py b/tests/hermes_cli/test_model_catalog.pyindex 0f36d5d32e1..5d55f8ed14d 100644--- a/tests/hermes_cli/test_model_catalog.py+++ b/tests/hermes_cli/test_model_catalog.py@@ -432,12 +432,10 @@ class TestRefreshCadence:with patch.object(model_catalog, "_load_catalog_config", return_value={"enabled": True, "url": "http://master", "ttl_hours": 1.0, "providers": {},}), patch.object(model_catalog, "get_catalog", return_value=_valid_manifest()) as gc, \- patch("hermes_cli.models.fetch_openrouter_models") as orm, \- patch("hermes_cli.models.fetch_nous_recommended_models") as nous:+ patch("hermes_cli.models.fetch_openrouter_models") as orm:assert model_catalog.refresh_catalogs() is Truegc.assert_called_once_with(force_refresh=True)orm.assert_called_once_with(force_refresh=True)- nous.assert_called_once_with(force_refresh=True)class TestIntegrationWithModelsModule:diff --git a/tests/hermes_cli/test_nous_recommendation_mirror.py b/tests/hermes_cli/test_nous_recommendation_mirror.pynew file mode 100644index 00000000000..342a5b7208d--- /dev/null+++ b/tests/hermes_cli/test_nous_recommendation_mirror.py@@ -0,0 +1,78 @@+"""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."""+import json+import threading+from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer++import pytest++from hermes_constants import get_hermes_home++PAYLOAD = {"paidRecommendedModels": [{"modelName": "mirrored"}]}+++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 = []++ class Handler(BaseHTTPRequestHandler):+ def do_GET(self):+ seen.append(self.path)+ self.send_response(404 if self.path.startswith("/dynamic/127.0.0.1") else 200)+ self.end_headers()+ self.wfile.write(json.dumps(PAYLOAD).encode())++ def log_message(self, *_args):+ pass++ httpd = ThreadingHTTPServer(("127.0.0.1", 0), Handler)+ thread = threading.Thread(target=httpd.serve_forever, daemon=True)+ thread.start()+ from hermes_cli import models+ monkeypatch.setattr(models, "NOUS_RECOMMENDED_MODELS_MIRROR", f"http://127.0.0.1:{httpd.server_port}/dynamic")+ yield seen+ httpd.shutdown()+ httpd.server_close()++[email protected]("portal, host", [+ ("", "portal.nousresearch.com"),+ ("https://portal.staging-nousresearch.com/", "portal.staging-nousresearch.com"),+])+def test_recommendations_come_from_the_portal_hosts_mirror(paths, portal, host):+ from hermes_cli.models import fetch_nous_recommended_models++ assert fetch_nous_recommended_models(portal, force_refresh=True) == PAYLOAD+ assert paths == [f"/dynamic/{host}/api/nous/recommended-models"]+++def test_a_portal_missing_from_the_mirror_is_asked_directly(paths, monkeypatch):+ from hermes_cli import models++ monkeypatch.setattr(models, "is_loopback_host", lambda _host: False) # treat the local server as remote+ portal = models.NOUS_RECOMMENDED_MODELS_MIRROR.removesuffix("/dynamic")+ assert models.fetch_nous_recommended_models(portal, force_refresh=True) == PAYLOAD+ host = portal.removeprefix("http://")+ assert paths == [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),+ ({"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):+ from hermes_cli import model_catalog, models++ monkeypatch.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")++ model_catalog.refresh_catalogs()++ assert paths == ([f"/dynamic/{host}/api/nous/recommended-models"] if host else [])