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).
This commit is contained in:
teknium1 2026-03-04 05:38:54 -08:00
parent 4ae61b0886
commit 8d2d8cc728

View file

@ -395,15 +395,23 @@ class SessionStore:
return False return False
def has_any_sessions(self) -> bool: 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: if self._db:
# Database tracks all sessions ever created (including ended ones). try:
# This correctly handles session resets where the in-memory _entries return self._db.session_count() > 1
# dict replaces the entry for the same session_key, but the DB except Exception:
# preserves the historical record. pass # fall through to heuristic
# > 1 because the current session is already created. # Fallback: check if sessions.json was loaded with existing data.
return self._db.session_count() > 1 # This covers the rare case where the DB is unavailable.
# Fallback for when DB is not available (e.g., tests)
self._ensure_loaded() self._ensure_loaded()
return len(self._entries) > 1 return len(self._entries) > 1