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 <joaomarcosdias444@gmail.com>
This commit is contained in:
@@ -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:,} "
|
||||
|
||||
@@ -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]
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user