From 41303b379e7a737dbb6b0bac561cb672d5c5e30f Mon Sep 17 00:00:00 2001 From: kshitijk4poor <82637225+kshitijk4poor@users.noreply.github.com> Date: Sat, 26 Sep 2026 21:18:03 +0530 Subject: [PATCH] refactor(sessions): export guard always counts every row; export_all reuses _active_clause assert_export_safe grew an include_inactive flag whose only production caller (the console export guard) always passes True, leaving the live-only branch and its default unused. Drop the parameter and count every row, which is what the transfer export materializes; the console call and docstring follow. The existing guard tests seed live rows only and are unchanged. export_all re-derived the live-row clause by hand; use the existing _active_clause(include_inactive, False) so "live row" stays defined in one place. Behaviour is unchanged. Co-authored-by: joaomarcos --- hermes_cli/console_engine.py | 2 +- hermes_state_portability.py | 2 +- hermes_state_sessions.py | 10 ++++------ 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/hermes_cli/console_engine.py b/hermes_cli/console_engine.py index 2506ac1080..83efad1f82 100644 --- a/hermes_cli/console_engine.py +++ b/hermes_cli/console_engine.py @@ -657,7 +657,7 @@ def _guard_exports(db, session_ids: list[str]) -> None: return try: for session_id in session_ids: - db.assert_export_safe(session_id, max_messages=limit, include_inactive=True) + db.assert_export_safe(session_id, max_messages=limit) except SessionExportTooLargeError as exc: raise ConsoleCommandError( f"Session '{exc.session_id}' has more than {limit:,} " diff --git a/hermes_state_portability.py b/hermes_state_portability.py index db5a193fd1..e51d1d1d5f 100644 --- a/hermes_state_portability.py +++ b/hermes_state_portability.py @@ -320,7 +320,7 @@ class SessionPortabilityMixin: return [self._with_messages(session, True, include_inactive) for session in sessions] messages_by_session = {session["id"]: [] for session in sessions} session_ids = list(messages_by_session) - active_clause = "" if include_inactive else " AND active = 1" + active_clause = self._active_clause(include_inactive, False) # Stay below SQLite's legacy 999-variable limit while replacing the per-session N+1 reads. for start in range(0, len(session_ids), 900): chunk = session_ids[start:start + 900] diff --git a/hermes_state_sessions.py b/hermes_state_sessions.py index 5763b78f3a..57b42c4162 100644 --- a/hermes_state_sessions.py +++ b/hermes_state_sessions.py @@ -1401,10 +1401,9 @@ class SessionSessionsMixin: ) return statuses - def assert_export_safe(self, session_id: str, max_messages: Optional[int] = None, - include_inactive: bool = False) -> int: - """Row count of this segment — live rows, or every row with ``include_inactive``, matching what - the export materializes — or raise SessionExportTooLargeError (the LIMITed subquery + def assert_export_safe(self, session_id: str, max_messages: Optional[int] = None) -> int: + """Row count of this segment — every row, archived included, as the transfer export materializes + it — or raise SessionExportTooLargeError (the LIMITed subquery stops once the bound is exceeded). ``None`` resolves ``sessions.max_export_messages``; 0 disables the guard.""" from hermes_state import SessionExportTooLargeError, resolved_max_export_messages @@ -1414,9 +1413,8 @@ class SessionSessionsMixin: raise ValueError("max_messages must be non-negative") if max_messages == 0: return 0 - active_clause = "" if include_inactive else " AND active = 1" row = self._read_one( - f"SELECT COUNT(*) FROM (SELECT 1 FROM messages WHERE session_id = ?{active_clause} LIMIT ?)", + "SELECT COUNT(*) FROM (SELECT 1 FROM messages WHERE session_id = ? LIMIT ?)", (session_id, max_messages + 1), ) message_count = int(row[0] if row else 0)