Skip to content

The agent that grows with you

refactor(credits): gate the re-warm on the pricing cache's own failure window

kshitijk4poor59f9ff8dbc75
diff --git a/agent/credits_tracker.py b/agent/credits_tracker.py
index c94b308e7cb..c6cb0f87d09 100644
--- a/agent/credits_tracker.py
+++ b/agent/credits_tracker.py
@@ -417,18 +417,17 @@ def rewarm_pricing_before_depleted_notice(agent) -> bool:
``_NOUS_CATALOG_TTL_SECONDS``, and a cold peek would bring the banner back for a
subscription-billed model. False = decide now: peek warm, not on Nous, a warm in flight (the
warm's own re-run included — fail-open, a failed fetch leaves the peek cold and the banner
- shows), or a warm already failed within the failed-catalog window (no per-turn re-spawn)."""
+ shows), or a fetch already failed and the cache is still refusing to dial. May raise; the notice
+ path that calls it catches."""
base_url = getattr(agent, "base_url", "") or ""
if getattr(agent, "provider", "") != "nous" or not base_url:
return False
- from hermes_cli.models_pricing import _FAILED_CATALOG_TTL_SECONDS, peek_cached_pricing
+ from hermes_cli.models_pricing import peek_cached_pricing, pricing_fetch_suppressed
- if peek_cached_pricing(base_url):
+ if peek_cached_pricing(base_url) or pricing_fetch_suppressed(base_url):
return False
- previous = getattr(agent, "_credits_pricing_warm", None)
- if previous is not None and (
- previous.is_alive() or time.monotonic() - previous.started_at < _FAILED_CATALOG_TTL_SECONDS
- ):
+ inflight = getattr(agent, "_credits_pricing_warm", None)
+ if inflight is not None and inflight.is_alive():
return False
def _warm_then_rerun() -> None:
@@ -437,10 +436,8 @@ def rewarm_pricing_before_depleted_notice(agent) -> bool:
from agent.memory_provider import spawn_context_thread
- thread = spawn_context_thread(_warm_then_rerun, name="credits-pricing-warm")
- thread.started_at = time.monotonic()
- agent._credits_pricing_warm = thread
- thread.start()
+ agent._credits_pricing_warm = spawn_context_thread(_warm_then_rerun, name="credits-pricing-warm")
+ agent._credits_pricing_warm.start()
return True
diff --git a/hermes_cli/models_pricing.py b/hermes_cli/models_pricing.py
index d681678082c..8409af26d17 100644
--- a/hermes_cli/models_pricing.py
+++ b/hermes_cli/models_pricing.py
@@ -92,6 +92,19 @@ def peek_cached_pricing(base_url: str) -> dict[str, dict[str, Any]]:
return _cached_catalog(root) or {}
+def pricing_fetch_suppressed(base_url: str) -> bool:
+ """A fetch for *base_url* failed recently and its empty result is still cached, so re-fetching now
+ returns that ``{}`` without dialing (see ``_cache_catalog``). Lets a caller that would otherwise
+ start a background refresh on a cold peek skip it for the rest of the failure window."""
+ root = _strip_v1((base_url or "").rstrip("/"))
+ now = time.monotonic()
+ return any(
+ _pricing_cache.get(key) == {} and _pricing_cache_retry_after.get(key, 0.0) > now
+ for key in list(_pricing_cache)
+ if key == root or key.startswith(root + _PRICING_AUTH_KEY_PREFIX)
+ )
+
+
def _strip_v1(url: str) -> str:
return url[:-3].rstrip("/") if url.endswith("/v1") else url
diff --git a/tests/agent/test_credits_cold_start.py b/tests/agent/test_credits_cold_start.py
index c97b97484df..cc6585cae3c 100644
--- a/tests/agent/test_credits_cold_start.py
+++ b/tests/agent/test_credits_cold_start.py
@@ -311,19 +311,24 @@ def test_header_after_ttl_expiry_rewarms_instead_of_flashing_the_banner(monkeypa
def test_header_on_a_cold_catalog_still_warns_when_the_warm_fails(monkeypatch):
- """Fail-open guard rail: a warm that leaves the catalog cold decides against the cold peek — the
- banner shows — and neither the warm's own re-run nor the next header (still inside the
- failed-catalog window) starts another warm."""
+ """Fail-open guard rail: a warm whose fetch fails (the cache keeps the empty result for its retry
+ window) decides against the cold peek — the banner shows — and neither the warm's own re-run nor
+ the next header inside that window starts another warm."""
from agent import credits_tracker
- _cold_pricing_cache(monkeypatch)
+ models_pricing = _cold_pricing_cache(monkeypatch)
warms: list = []
- monkeypatch.setattr(credits_tracker, "_warm_nous_pricing_cache", lambda: warms.append(1))
+
+ def _failed_fetch():
+ warms.append(1)
+ models_pricing._cache_catalog(_NOUS_BASE[:-3] + models_pricing._PRICING_AUTH_KEY_PREFIX + "abc", {})
+
+ monkeypatch.setattr(credits_tracker, "_warm_nous_pricing_cache", _failed_fetch)
agent = _mixin_agent()
agent._emit_credits_notices()
_join_pricing_warm(agent)
assert agent.shown == ["credits.depleted"]
- agent._emit_credits_notices() # next header, catalog still cold
+ agent._emit_credits_notices() # next header, still inside the failed-fetch window
_join_pricing_warm(agent)
assert warms == [1]