Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions hermes_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -1758,6 +1758,7 @@ def list_sessions_rich(
ORDER BY m.timestamp, m.id LIMIT 1),
''
) AS _preview_raw,
(SELECT COUNT(*) FROM messages m3 WHERE m3.session_id = s.id) AS _computed_message_count,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

COUNT(*) includes soft-archived and rewound rows. On current main, message_count is explicitly the live active = 1 count (hermes_state.py:3980-3984), so this needs an active-row predicate to avoid inflating compacted sessions.

COALESCE(
(SELECT MAX(m2.timestamp) FROM messages m2 WHERE m2.session_id = s.id),
s.started_at
Expand All @@ -1782,6 +1783,7 @@ def list_sessions_rich(
ORDER BY m.timestamp, m.id LIMIT 1),
''
) AS _preview_raw,
(SELECT COUNT(*) FROM messages m3 WHERE m3.session_id = s.id) AS _computed_message_count,
COALESCE(
(SELECT MAX(m2.timestamp) FROM messages m2 WHERE m2.session_id = s.id),
s.started_at
Expand All @@ -1800,6 +1802,12 @@ def list_sessions_rich(
s = dict(row)
# Build the preview from the raw substring
raw = s.pop("_preview_raw", "").strip()
# Override the denormalized message_count with the actual count
# from the messages table — the sessions.message_count column can
# lag behind (e.g. after a partial write, crash, or race).
computed = s.pop("_computed_message_count", None)
if computed is not None:
s["message_count"] = computed
if raw:
text = raw[:60]
s["preview"] = text + ("..." if len(raw) > 60 else "")
Expand Down Expand Up @@ -1859,6 +1867,7 @@ def _get_session_rich_row(self, session_id: str) -> Optional[Dict[str, Any]]:
ORDER BY m.timestamp, m.id LIMIT 1),
''
) AS _preview_raw,
(SELECT COUNT(*) FROM messages m3 WHERE m3.session_id = s.id) AS _computed_message_count,
COALESCE(
(SELECT MAX(m2.timestamp) FROM messages m2 WHERE m2.session_id = s.id),
s.started_at
Expand All @@ -1873,6 +1882,9 @@ def _get_session_rich_row(self, session_id: str) -> Optional[Dict[str, Any]]:
return None
s = dict(row)
raw = s.pop("_preview_raw", "").strip()
computed = s.pop("_computed_message_count", None)
if computed is not None:
s["message_count"] = computed
if raw:
text = raw[:60]
s["preview"] = text + ("..." if len(raw) > 60 else "")
Expand Down
Loading