Skip to content

The agent that grows with you

fix(models): expire the Nous catalog so policy changes land

Mariano Nicolini79972c67814a
diff --git a/hermes_cli/models.py b/hermes_cli/models.py
index c5a3d1e5d0a..32fff0244da 100644
--- a/hermes_cli/models.py
+++ b/hermes_cli/models.py
@@ -2242,12 +2242,22 @@ def _cached_catalog(cache_key: str) -> Optional[dict[str, dict[str, Any]]]:
def _cache_catalog(
- cache_key: str, result: dict[str, dict[str, Any]]
+ cache_key: str,
+ result: dict[str, dict[str, Any]],
+ ttl_seconds: Optional[float] = None,
) -> dict[str, dict[str, Any]]:
- """Cache a catalog result, giving an empty one an expiry."""
+ """Cache a catalog result, giving an empty one an expiry.
+
+ *ttl_seconds* expires a non-empty result too. Only a catalog whose contents
+ depend on server-side state the client cannot observe needs it — an org's
+ model policy can change while a long-lived process holds the entry.
+ """
_pricing_cache[cache_key] = result
if result:
- _pricing_cache_retry_after.pop(cache_key, None)
+ if ttl_seconds:
+ _pricing_cache_retry_after[cache_key] = time.monotonic() + ttl_seconds
+ else:
+ _pricing_cache_retry_after.pop(cache_key, None)
else:
_pricing_cache_retry_after[cache_key] = (
time.monotonic() + _FAILED_CATALOG_TTL_SECONDS
@@ -2424,6 +2434,7 @@ def fetch_models_with_pricing(
*,
force_refresh: bool = False,
include_sale_original: bool = False,
+ cache_ttl_seconds: Optional[float] = None,
) -> dict[str, dict[str, Any]]:
"""Fetch ``/v1/models`` and return ``{model_id: {prompt, completion, ...}}``.
@@ -2497,7 +2508,7 @@ def fetch_models_with_pricing(
entry["original"] = orig_entry
result[mid] = entry
- return _cache_catalog(cache_key, result)
+ return _cache_catalog(cache_key, result, cache_ttl_seconds)
def fetch_ai_gateway_pricing(
@@ -2638,6 +2649,7 @@ def nous_policy_allowed_ids(*, force_refresh: bool = False) -> Optional[set[str]
base_url=base_url,
force_refresh=force_refresh,
include_sale_original=True,
+ cache_ttl_seconds=_NOUS_CATALOG_TTL_SECONDS,
)
return set(pricing) or None
@@ -2646,6 +2658,13 @@ def nous_policy_allowed_ids(*, force_refresh: bool = False) -> Optional[set[str]
# allowlist, and is not worth showing in place of an empty picker.
_NOUS_POLICY_APPEND_MAX = 64
+# How long a Nous catalog stays trusted. Its contents depend on the org's
+# policy, which an admin can change at any time and the client cannot observe,
+# so a long-lived process must re-ask instead of holding the first answer for
+# its whole life. Other providers' catalogs carry no such state and keep the
+# default no-expiry caching.
+_NOUS_CATALOG_TTL_SECONDS = 300.0
+
def restrict_to_nous_policy(
model_ids: list[str],
@@ -2705,6 +2724,7 @@ def get_pricing_for_provider(provider: str, *, force_refresh: bool = False) -> d
force_refresh=force_refresh,
# Sale chrome (pricing.original) is Nous Portal-only.
include_sale_original=True,
+ cache_ttl_seconds=_NOUS_CATALOG_TTL_SECONDS,
)
return {}
diff --git a/tests/hermes_cli/test_pricing_cache_auth_key.py b/tests/hermes_cli/test_pricing_cache_auth_key.py
index df3946ac390..9292f376f33 100644
--- a/tests/hermes_cli/test_pricing_cache_auth_key.py
+++ b/tests/hermes_cli/test_pricing_cache_auth_key.py
@@ -152,3 +152,37 @@ class TestPeekCachedPricing:
def test_never_fetches(self, catalog):
peek_cached_pricing(BASE)
assert catalog == []
+
+
+class TestNousCatalogExpiry:
+ """A Nous catalog reflects the org's policy, which an admin can change while
+ a long-lived process holds the entry."""
+
+ def test_entry_expires_so_a_policy_change_is_picked_up(self, catalog, monkeypatch):
+ from hermes_cli.models import _NOUS_CATALOG_TTL_SECONDS
+
+ fetch_models_with_pricing(
+ api_key="sk-test", base_url=BASE,
+ cache_ttl_seconds=_NOUS_CATALOG_TTL_SECONDS,
+ )
+ assert len(catalog) == 1
+
+ now = models_mod.time.monotonic()
+ monkeypatch.setattr(
+ models_mod.time, "monotonic",
+ lambda: now + _NOUS_CATALOG_TTL_SECONDS + 1,
+ )
+ fetch_models_with_pricing(
+ api_key="sk-test", base_url=BASE,
+ cache_ttl_seconds=_NOUS_CATALOG_TTL_SECONDS,
+ )
+ assert len(catalog) == 2, "expired entry should be re-read"
+
+ def test_no_ttl_keeps_the_entry_indefinitely(self, catalog, monkeypatch):
+ """Other providers' catalogs carry no policy and must not start
+ re-fetching."""
+ fetch_models_with_pricing(api_key="sk-test", base_url=BASE)
+ now = models_mod.time.monotonic()
+ monkeypatch.setattr(models_mod.time, "monotonic", lambda: now + 86_400)
+ fetch_models_with_pricing(api_key="sk-test", base_url=BASE)
+ assert len(catalog) == 1