Skip to content

The agent that grows with you

refactor(agent/reasoning_params,rate_limit_credits): module-level helpers keep AIAgent attribute surface identical

Tekniumc13d0b023e91
diff --git a/agent/rate_limit_credits.py b/agent/rate_limit_credits.py
index 1dcbb3178e4..9dc0f2d45e2 100644
--- a/agent/rate_limit_credits.py
+++ b/agent/rate_limit_credits.py
@@ -22,6 +22,13 @@ def _pct(fraction) -> str:
return ("%.0f%%" % (fraction * 100)) if fraction is not None else "n/a"
+def _adopt_credits_state(agent, state) -> None:
+ """Retain-last-known: overwrite state and latch session-start remaining on the first header ever seen."""
+ agent._credits_state = state
+ if agent._credits_session_start_micros is None:
+ agent._credits_session_start_micros = state.remaining_micros
+
+
class RateLimitCreditsMixin:
"""Rate-limit + credits header capture and notices (see module docstring)."""
@@ -60,7 +67,7 @@ class RateLimitCreditsMixin:
except Exception:
fixture = None
if fixture is not None:
- self._adopt_credits_state(fixture)
+ _adopt_credits_state(self, fixture)
latch = getattr(self, "_credits_latch", None)
if isinstance(latch, dict):
# Only seen_below_90 — priming seen_grant_unspent would fire grant_spent on first observation.
@@ -89,7 +96,7 @@ class RateLimitCreditsMixin:
"(miss — producer off / non-Nous path / >TTL stale)")
return
- self._adopt_credits_state(state)
+ _adopt_credits_state(self, state)
if dev:
# HERMES_DEV_CREDITS streams each capture to agent.log (`hermes logs -f`, grep 'credits ▸').
spent = self.get_credits_spent_micros()
@@ -101,12 +108,6 @@ class RateLimitCreditsMixin:
(" · disabled=%s" % state.disabled_reason) if state.disabled_reason else "")
self._emit_credits_notices()
- def _adopt_credits_state(self, state) -> None:
- """Retain-last-known: overwrite state and latch session-start remaining on the first header ever seen."""
- self._credits_state = state
- if self._credits_session_start_micros is None:
- self._credits_session_start_micros = state.remaining_micros
-
def _emit_credits_notices(self) -> None:
"""Run the threshold policy and emit notices (shared by the warm path and the cold-start seed).
diff --git a/agent/reasoning_params.py b/agent/reasoning_params.py
index 2660c4dcff8..3186f072cbb 100644
--- a/agent/reasoning_params.py
+++ b/agent/reasoning_params.py
@@ -23,6 +23,27 @@ _OPENROUTER_REASONING_PREFIXES = (
_PROBE_TTL_S = 60
+def _cached_probe(agent, cache_attr: str, probe, unknown, definitive):
+ """``probe(model, base_url, api_key)`` once per (model, base_url); ``unknown`` is what a raising probe
+ yields, ``definitive(value)`` decides permanent vs 60s TTL."""
+ cache = getattr(agent, cache_attr, None)
+ if cache is None:
+ cache = {}
+ setattr(agent, cache_attr, cache)
+ key = (agent.model, agent.base_url)
+ cached = cache.get(key)
+ if cached is not None:
+ value, ts = cached
+ if definitive(value) or (time.monotonic() - ts) < _PROBE_TTL_S:
+ return value
+ try:
+ value = probe(agent.model, agent.base_url, getattr(agent, "api_key", ""))
+ except Exception:
+ value = unknown
+ cache[key] = (value, time.monotonic())
+ return value
+
+
class ReasoningParamsMixin:
"""Reasoning-parameter gating and echo policy (see module docstring)."""
@@ -61,33 +82,13 @@ class ReasoningParamsMixin:
model = (self.model or "").lower()
return any(model.startswith(prefix) for prefix in _OPENROUTER_REASONING_PREFIXES)
- def _cached_probe(self, cache_attr: str, probe, unknown, definitive):
- """``probe(model, base_url, api_key)`` once per (model, base_url); ``unknown`` is what a raising probe
- yields, ``definitive(value)`` decides permanent vs 60s TTL."""
- cache = getattr(self, cache_attr, None)
- if cache is None:
- cache = {}
- setattr(self, cache_attr, cache)
- key = (self.model, self.base_url)
- cached = cache.get(key)
- if cached is not None:
- value, ts = cached
- if definitive(value) or (time.monotonic() - ts) < _PROBE_TTL_S:
- return value
- try:
- value = probe(self.model, self.base_url, getattr(self, "api_key", ""))
- except Exception:
- value = unknown
- cache[key] = (value, time.monotonic())
- return value
-
def _lmstudio_reasoning_options_cached(self) -> list[str]:
"""LM Studio's published reasoning ``allowed_options`` (gate + clamp so toggle models don't 400 on ``high``)."""
try:
from hermes_cli.models import lmstudio_model_reasoning_options
except Exception:
return []
- return self._cached_probe("_lm_reasoning_opts_cache", lmstudio_model_reasoning_options, [], bool)
+ return _cached_probe(self, "_lm_reasoning_opts_cache", lmstudio_model_reasoning_options, [], bool)
def _ollama_supports_thinking_cached(self) -> bool:
"""True only if Ollama's ``/api/show`` declares the ``thinking`` capability."""
@@ -95,7 +96,7 @@ class ReasoningParamsMixin:
from hermes_cli.models import ollama_model_supports_thinking
except Exception:
return False
- return bool(self._cached_probe("_ollama_thinking_cache", ollama_model_supports_thinking, None, lambda v: v is not None))
+ return bool(_cached_probe(self, "_ollama_thinking_cache", ollama_model_supports_thinking, None, lambda v: v is not None))
def _resolve_lmstudio_summary_reasoning_effort(self) -> Optional[str]:
"""Safe top-level ``reasoning_effort`` for LM Studio; shared with the iteration-limit summary call."""