fix(sessions): transfer export round-trips compaction-archived turns

The console `sessions export` backup went through export_session /
export_all, whose batched read filters `AND active = 1`, so an
export-then-import restore silently dropped every compaction-archived
turn (display 9 -> 3 in the C13 probe). export_all now takes
include_inactive (storage-order rows with their active/compacted flags,
which import_sessions restores as archived), the console export passes
it for both single-session and all-session exports, and
assert_export_safe sizes the same projection it guards.

Ported from #123267's transfer/backup hunks; its import-normalisation
hunks are superseded by #122680's checkpoint-correct import.

Co-authored-by: John Paul Soliva <soliva.johnpaul@icloud.com>
This commit is contained in:
joaomarcos
2026-09-26 18:23:52 +05:30
committed by kshitij
parent 0325349f96
commit 85deb4553a
5 changed files with 81 additions and 14 deletions

View File

@@ -1401,8 +1401,10 @@ class SessionSessionsMixin:
)
return statuses
def assert_export_safe(self, session_id: str, max_messages: Optional[int] = None) -> int:
"""Active row count of this segment, or raise SessionExportTooLargeError (the LIMITed subquery
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
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
@@ -1412,8 +1414,9 @@ 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(
"SELECT COUNT(*) FROM (SELECT 1 FROM messages WHERE session_id = ? AND active = 1 LIMIT ?)",
f"SELECT COUNT(*) FROM (SELECT 1 FROM messages WHERE session_id = ?{active_clause} LIMIT ?)",
(session_id, max_messages + 1),
)
message_count = int(row[0] if row else 0)