From 8d2d8cc728a08c263a769fd821a98fc7b3562f74 Mon Sep 17 00:00:00 2001 From: teknium1 Date: Wed, 4 Mar 2026 05:38:54 -0800 Subject: [PATCH] refactor: add exception handling and docstring to has_any_sessions Wrap session_count() in try/except so a DB error falls through to the heuristic fallback instead of crashing. Added a detailed docstring explaining why the DB approach is needed and the > 1 assumption (current session already exists when called). --- gateway/session.py | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/gateway/session.py b/gateway/session.py index 72b50fa0..091cb46a 100644 --- a/gateway/session.py +++ b/gateway/session.py @@ -395,15 +395,23 @@ class SessionStore: return False def has_any_sessions(self) -> bool: - """Check if any sessions have ever been created (across all platforms).""" + """Check if any sessions have ever been created (across all platforms). + + Uses the SQLite database as the source of truth because it preserves + historical session records (ended sessions still count). The in-memory + ``_entries`` dict replaces entries on reset, so ``len(_entries)`` would + stay at 1 for single-platform users — which is the bug this fixes. + + The current session is already in the DB by the time this is called + (get_or_create_session runs first), so we check ``> 1``. + """ if self._db: - # Database tracks all sessions ever created (including ended ones). - # This correctly handles session resets where the in-memory _entries - # dict replaces the entry for the same session_key, but the DB - # preserves the historical record. - # > 1 because the current session is already created. - return self._db.session_count() > 1 - # Fallback for when DB is not available (e.g., tests) + try: + return self._db.session_count() > 1 + except Exception: + pass # fall through to heuristic + # Fallback: check if sessions.json was loaded with existing data. + # This covers the rare case where the DB is unavailable. self._ensure_loaded() return len(self._entries) > 1