diff --git a/gateway/slash_commands_status.py b/gateway/slash_commands_status.py index f25951cad0..a80df42f9f 100644 --- a/gateway/slash_commands_status.py +++ b/gateway/slash_commands_status.py @@ -612,14 +612,14 @@ class GatewayStatusCommandsMixin: return t("gateway.usage.no_data") async def _persisted_billing_route(self, source): - """``(provider, base_url)`` from the SessionDB row / dominant route when no agent is resident.""" + """``(provider, base_url)`` from the SessionDB row / most recent route when no agent is resident.""" async def _rows(): entry = await self.async_session_store.get_or_create_session(source) persisted = await self._session_db.get_session(entry.session_id) or {} - route = await self._session_db.get_dominant_session_model_route(entry.session_id) + route = await self._session_db.get_recent_session_model_route(entry.session_id) return persisted, route if isinstance(route, dict) else {} - persisted, dominant = await _quiet(_rows, ({}, {})) - row = dominant if dominant.get("billing_provider") else persisted + persisted, recent = await _quiet(_rows, ({}, {})) + row = recent if recent.get("billing_provider") else persisted return row.get("billing_provider"), row.get("billing_base_url") async def _handle_insights_command(self, event: MessageEvent) -> str: diff --git a/hermes_state_sessions.py b/hermes_state_sessions.py index 384c7895b5..0a7aa822c7 100644 --- a/hermes_state_sessions.py +++ b/hermes_state_sessions.py @@ -744,29 +744,11 @@ class SessionSessionsMixin: ) return self._session_row_dict(row) if row else None - def get_dominant_session_model_route(self, session_id: str) -> Optional[Dict[str, Any]]: - """Main-loop model route that served most API calls (``session_model_usage`` keeps the coherent - per-call tuple; ``sessions`` mixes route changes).""" - self.flush_token_counts() - row = self._read_one( - """SELECT model, billing_provider, billing_base_url, billing_mode, - api_call_count - FROM session_model_usage - WHERE session_id = ? - AND task = '' - AND model <> 'unknown' - AND billing_provider <> '' - ORDER BY api_call_count DESC, - (input_tokens + output_tokens + cache_read_tokens + - cache_write_tokens + reasoning_tokens) DESC, - last_seen DESC - LIMIT 1""", - (session_id,), - ) - return dict(row) if row else None - def get_recent_session_model_route(self, session_id: str) -> Optional[Dict[str, Any]]: - """Most recently used main-loop model route as one coherent per-call tuple.""" + """Most recently used main-loop model route as one coherent per-call tuple + (``session_model_usage`` keeps model+provider together; ``sessions`` mixes route changes). + Recency, not lifetime call count: on a long session a route retired weeks ago can hold the + highest ``api_call_count`` forever, and /status and /usage would keep calling it current.""" self.flush_token_counts() row = self._read_one( """SELECT model, billing_provider, billing_base_url, billing_mode, diff --git a/tests/gateway/test_usage_command.py b/tests/gateway/test_usage_command.py index ca83c56ecd..ae68e56fe3 100644 --- a/tests/gateway/test_usage_command.py +++ b/tests/gateway/test_usage_command.py @@ -163,14 +163,14 @@ class TestUsageAccountSection: assert "📈 **Account limits**" in result @pytest.mark.asyncio - async def test_usage_command_prefers_dominant_persisted_route(self, monkeypatch): + async def test_usage_command_prefers_recent_persisted_route(self, monkeypatch): runner = _make_runner(SK) runner._session_db = AsyncSessionDB(MagicMock()) runner._session_db._db.get_session.return_value = { "billing_provider": "nous", "billing_base_url": "https://inference-api.nousresearch.com/v1/", } - runner._session_db._db.get_dominant_session_model_route.return_value = { + runner._session_db._db.get_recent_session_model_route.return_value = { "model": "z-ai/glm-5.2", "billing_provider": "nvidia", "billing_base_url": "https://integrate.api.nvidia.com/v1/",