Skip to content

The agent that grows with you

fix(sessions): include live session activity in prune recency

sy0u1ti3547c30c85b1
diff --git a/hermes_state_maintenance.py b/hermes_state_maintenance.py
index 31132f98fe7..a870470cb08 100644
--- a/hermes_state_maintenance.py
+++ b/hermes_state_maintenance.py
@@ -15,11 +15,7 @@ from hermes_startup_watchdog import report_startup_progress
# caplog tests pin the "hermes_state" logger name.
logger = logging.getLogger("hermes_state")
-_LAST_ACTIVE_SQL = """COALESCE(
- (SELECT MAX(m.timestamp) FROM messages m
- WHERE m.session_id = s.id),
- s.started_at
- )"""
+_LAST_ACTIVE_SQL = _sql_session_last_active("s")
_TOKENS_SQL = "(COALESCE(s.input_tokens, 0) + COALESCE(s.output_tokens, 0))"
_COST_SQL = "COALESCE(s.actual_cost_usd, s.estimated_cost_usd, 0)"
@@ -217,11 +213,7 @@ class SessionMaintenanceMixin:
where, params = self._prune_where(older_than_days, source, filters)
return [dict(row) for row in self._read_all(
f"""SELECT s.id, s.source, s.title, s.model, s.started_at,
- COALESCE(
- (SELECT MAX(m.timestamp) FROM messages m
- WHERE m.session_id = s.id),
- s.started_at
- ) AS last_active,
+ {_LAST_ACTIVE_SQL} AS last_active,
s.ended_at, s.message_count, s.archived
FROM sessions s WHERE {where}
ORDER BY last_active ASC, s.started_at ASC""", params)]
diff --git a/tests/hermes_state/test_prune_activity_recency.py b/tests/hermes_state/test_prune_activity_recency.py
new file mode 100644
index 00000000000..ad96160ec6f
--- /dev/null
+++ b/tests/hermes_state/test_prune_activity_recency.py
@@ -0,0 +1,47 @@
+import time
+from contextlib import closing
+import pytest
+from hermes_state import SessionDB
+
+
+def test_prune_sessions_respects_touch_session_activity(tmp_path):
+ with closing(SessionDB(tmp_path / "state.db")) as db:
+ now = time.time()
+ old_time = now - 100 * 86400 # 100 days ago
+ recent_time = now - 2 * 86400 # 2 days ago
+
+ # Session with older messages and started_at, but recent heartbeat / activity
+ db.create_session("active_by_heartbeat", source="cli")
+ db.append_message("active_by_heartbeat", "user", "hello", timestamp=old_time)
+ db.end_session("active_by_heartbeat", "done")
+ db._conn.execute(
+ "UPDATE sessions SET started_at = ?, ended_at = ? WHERE id = ?",
+ (old_time, old_time, "active_by_heartbeat"),
+ )
+ db._conn.commit()
+ db.touch_session_activity("active_by_heartbeat", ts=recent_time)
+
+ # Truly old session
+ db.create_session("truly_old", source="cli")
+ db.append_message("truly_old", "user", "old message", timestamp=old_time)
+ db.end_session("truly_old", "done")
+ db._conn.execute(
+ "UPDATE sessions SET started_at = ?, ended_at = ? WHERE id = ?",
+ (old_time, old_time, "truly_old"),
+ )
+ db._conn.commit()
+
+ # Check dry-run candidates
+ candidates = db.list_prune_candidates(older_than_days=10)
+ candidate_ids = [c["id"] for c in candidates]
+ assert "active_by_heartbeat" not in candidate_ids
+ assert "truly_old" in candidate_ids
+
+ candidate_map = {c["id"]: c for c in db.list_prune_candidates(older_than_days=None)}
+ assert candidate_map["active_by_heartbeat"]["last_active"] == pytest.approx(recent_time, abs=1.0)
+
+ # Pruning sessions older than 10 days should spare active_by_heartbeat
+ pruned = db.prune_sessions(older_than_days=10)
+ assert pruned == 1
+ assert db.get_session("truly_old") is None
+ assert db.get_session("active_by_heartbeat") is not None