diff --git a/gateway/slash_commands_status.py b/gateway/slash_commands_status.py index b382d459a9..0a741cea77 100644 --- a/gateway/slash_commands_status.py +++ b/gateway/slash_commands_status.py @@ -83,7 +83,7 @@ def _quiet_sync(call, default=None): def _status_model_route(status_agent, persisted_route: dict, session_row: dict, session_entry): """``(model, provider, context_used, context_total)`` for /status. - Order: live/cached agent route -> persisted dominant route -> SessionDB row -> gateway config + Order: live/cached agent route -> persisted recent route -> SessionDB row -> gateway config (only loaded when something is still missing). """ from gateway.run import _AGENT_PENDING_SENTINEL, _load_gateway_config, _resolve_gateway_model @@ -308,7 +308,7 @@ class GatewayStatusCommandsMixin: _int_value(session_row.get(k)) for k in ("input_tokens", "output_tokens", "cache_read_tokens", "cache_write_tokens", "reasoning_tokens") ) - route = await _quiet(lambda: db.get_dominant_session_model_route(session_id)) + route = await _quiet(lambda: db.get_recent_session_model_route(session_id)) return title, session_row, db_total_tokens, route if isinstance(route, dict) else {} @staticmethod diff --git a/hermes_state_sessions.py b/hermes_state_sessions.py index 37834b9227..384c7895b5 100644 --- a/hermes_state_sessions.py +++ b/hermes_state_sessions.py @@ -765,6 +765,23 @@ class SessionSessionsMixin: ) 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.""" + 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 last_seen DESC + LIMIT 1""", + (session_id,), + ) + return dict(row) if row else None + def resolve_session_id(self, session_id_or_prefix: str) -> Optional[str]: """Exact id, else the single unambiguous prefix match, else None.""" exact = self.get_session(session_id_or_prefix) diff --git a/tests/gateway/test_status_command.py b/tests/gateway/test_status_command.py index f06ffd4cc3..07ad34f20c 100644 --- a/tests/gateway/test_status_command.py +++ b/tests/gateway/test_status_command.py @@ -142,8 +142,8 @@ async def test_status_command_includes_live_agent_model_and_context(): @pytest.mark.asyncio -async def test_status_command_uses_dominant_persisted_model_route(tmp_path): - """Persisted status must not combine a model and provider from different calls.""" +async def test_status_command_uses_most_recent_persisted_model_route(tmp_path): + """Persisted status uses the latest coherent route, not the lifetime-dominant route.""" session_entry = SessionEntry( session_key=build_session_key(_make_source()), session_id="sess-1", @@ -183,8 +183,8 @@ async def test_status_command_uses_dominant_persisted_model_route(tmp_path): result = await runner._handle_message(_make_event("/status")) - assert "**Model:** `z-ai/glm-5.2` (nvidia)" in result - assert "**Model:** `z-ai/glm-5.2` (nous)" not in result + assert "**Model:** `upstage/solar-pro4:free` (nous)" in result + assert "**Model:** `z-ai/glm-5.2` (nvidia)" not in result finally: db.close()