The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(credits): re-warm the Nous catalog when a header finds it cold after the TTL
kshitijk4poor21cf53b88807
diff --git a/agent/credits_tracker.py b/agent/credits_tracker.pyindex 705172ec4a8..a274dde5190 100644--- a/agent/credits_tracker.py+++ b/agent/credits_tracker.py@@ -406,6 +406,45 @@ def _warm_nous_pricing_cache() -> None:logger.debug("credits ▸ nous pricing warm failed", exc_info=True)+def rewarm_pricing_before_depleted_notice(agent) -> bool:+ """Depleted account, model the peek cannot vouch for, Nous catalog COLD: start a background warm+ whose completion re-runs the policy, and return True so the caller leaves the depleted decision+ to that re-run instead of flashing a banner the warm catalog would suppress.++ Needed because the session-start warm is one-shot while the Nous catalog expires after+ ``_NOUS_CATALOG_TTL_SECONDS`` — every inference header after that re-evaluated against a cold+ peek and brought the banner back for a subscription-billed model. Returns False (decide now)+ when the peek is warm, the agent is not on Nous, or a warm is already in flight — including the+ warm's own re-run, which must decide against whatever the fetch produced (fail-open: a failed+ fetch leaves the peek cold and the banner shows).+ """+ base_url = getattr(agent, "base_url", "") or ""+ if getattr(agent, "provider", "") != "nous" or not base_url:+ return False+ try:+ from hermes_cli.models_pricing import peek_cached_pricing++ if peek_cached_pricing(base_url):+ return False+ except Exception:+ return False+ inflight = getattr(agent, "_credits_pricing_warm", None)+ if inflight is not None and inflight.is_alive():+ return False++ def _warm_then_rerun() -> None:+ _warm_nous_pricing_cache()+ if callable(emit := getattr(agent, "_emit_credits_notices", None)):+ emit()++ from agent.memory_provider import spawn_context_thread++ thread = spawn_context_thread(_warm_then_rerun, name="credits-pricing-warm")+ agent._credits_pricing_warm = thread+ thread.start()+ return True++def seed_credits_at_session_start(agent) -> bool:"""Hydrate agent._credits_state from the portal account (or a dev fixture) and fire the notice policy sowarnings show at session OPEN (TUI/desktop "ready" and plain-CLI first-turn setup). Idempotent once a seeddiff --git a/agent/rate_limit_credits.py b/agent/rate_limit_credits.pyindex 44c6f7a2003..a82543f77f1 100644--- a/agent/rate_limit_credits.py+++ b/agent/rate_limit_credits.py@@ -132,12 +132,16 @@ class RateLimitCreditsMixin:if not self._credits_notices_enabled() or state is None:returntry:- from agent.credits_tracker import evaluate_credits_notices, is_free_tier_model, new_credits_latch+ from agent.credits_tracker import (+ evaluate_credits_notices, is_free_tier_model, new_credits_latch, rewarm_pricing_before_depleted_notice,+ )latch = getattr(self, "_credits_latch", None)if latch is None:latch = self._credits_latch = new_credits_latch()# Free-model gate: a depleted account can still inference on a free model. Local data only.model_is_free = is_free_tier_model(getattr(self, "model", "") or "", getattr(self, "base_url", "") or "")+ if state.depleted and not model_is_free and rewarm_pricing_before_depleted_notice(self):+ return # a cold catalog cannot say the model is billed elsewhere; the warm's re-run decidesto_show, to_clear = evaluate_credits_notices(state, latch, model_is_free=model_is_free)for key in to_clear:self._emit_notice_clear(key)diff --git a/tests/agent/test_credits_cold_start.py b/tests/agent/test_credits_cold_start.pyindex 74c2ce87e91..7501ed8ddb6 100644--- a/tests/agent/test_credits_cold_start.py+++ b/tests/agent/test_credits_cold_start.py@@ -254,3 +254,76 @@ def test_bg_seed_reruns_the_policy_when_a_header_beat_it(monkeypatch):assert agent._credits_state is header_state # the seed never clobbers a live headerassert agent.emitted[0] == (["credits.depleted"], []) # cold: shownassert agent.emitted[-1] == ([], ["credits.depleted"]) # warm re-run: cleared, no "restored"+++# ── inference-header path: the catalog goes cold again after the Nous TTL ────+++def _mixin_agent(model="openai/gpt-5.6-luna"):+ """The real notice path (RateLimitCreditsMixin._emit_credits_notices) with a recording driver."""+ from agent.rate_limit_credits import RateLimitCreditsMixin+ from agent.status_output import StatusOutputMixin++ class _Agent(RateLimitCreditsMixin, StatusOutputMixin):+ provider = "nous"+ base_url = _NOUS_BASE++ def __init__(self):+ self.model = model+ self.shown: list = []+ self.cleared: list = []+ self.notice_callback = lambda n: self.shown.append(n.key)+ self.notice_clear_callback = self.cleared.append+ self._credits_state = _state(paid_access=False)++ return _Agent()+++def _join_pricing_warm(agent):+ thread = getattr(agent, "_credits_pricing_warm", None)+ if thread is not None:+ thread.join(timeout=10)+ assert not thread.is_alive(), "pricing warm hung"+++def test_header_after_ttl_expiry_rewarms_instead_of_flashing_the_banner(monkeypatch):+ """The session-start warm is one-shot; a Nous catalog expires after _NOUS_CATALOG_TTL_SECONDS. A+ header landing after that saw a cold peek and brought the depleted banner back for a+ subscription-billed model. Now the cold peek starts a re-warm and the banner never shows."""+ from agent import credits_tracker++ models_pricing = _cold_pricing_cache(monkeypatch)+ models_pricing._cache_catalog(+ _NOUS_BASE[:-3] + models_pricing._PRICING_AUTH_KEY_PREFIX + "abc",+ _SUBSCRIPTION_CATALOG[_NOUS_BASE[:-3]], models_pricing._NOUS_CATALOG_TTL_SECONDS)+ agent = _mixin_agent()+ agent._emit_credits_notices()+ assert agent.shown == [] # warm catalog: suppressed+ models_pricing._pricing_cache_retry_after = { # ...then the TTL passes+ k: v - models_pricing._NOUS_CATALOG_TTL_SECONDS - 1 for k, v in models_pricing._pricing_cache_retry_after.items()}+ monkeypatch.setattr(credits_tracker, "_warm_nous_pricing_cache",+ lambda: models_pricing._pricing_cache.update(_SUBSCRIPTION_CATALOG))++ agent._emit_credits_notices() # the next inference header+ _join_pricing_warm(agent)+ assert agent.shown == []+ assert agent.cleared == []+++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 the warm's own re-run does not spawn another warm."""+ import threading++ from agent import credits_tracker++ _cold_pricing_cache(monkeypatch)+ monkeypatch.setattr(credits_tracker, "_warm_nous_pricing_cache", lambda: None)+ agent = _mixin_agent()+ before = {t for t in threading.enumerate() if t.name == "credits-pricing-warm"}++ agent._emit_credits_notices()+ _join_pricing_warm(agent)+ assert agent.shown == ["credits.depleted"]+ assert len({t for t in threading.enumerate() if t.name == "credits-pricing-warm"} - before) == 0+ assert agent._credits_pricing_warm is not None and not agent._credits_pricing_warm.is_alive()