The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
refactor(sessions): one helper builds the not-pinned predicate
kshitijk4poor91c189bb3339
diff --git a/hermes_cli/sessions_cmd.py b/hermes_cli/sessions_cmd.pyindex 5a3a1d95a6a..efa49497db1 100644--- a/hermes_cli/sessions_cmd.py+++ b/hermes_cli/sessions_cmd.py@@ -651,10 +651,9 @@ def _prune_never_active_keyed(db, args):def _note_pinned_skipped(db, filters, action):"""Tell the user how many pinned rows bulk prune/archive spared (pin = durable keep; only`prune --include-pinned` opts in, archive always spares them)."""- _base = {k: v for k, v in filters.items() if k != "include_pinned"}# Count matching pinned rows only: neither the unpinned ancestors a pinned tip spares nor the# unpinned continuations a pinned segment protects carry the pin.- skipped = int(db.count_prune_matches(**_base, include_pinned=True, pinned_only=True))+ skipped = int(db.count_prune_matches(**filters, pinned_only=True))if not skipped:returnsuffix = "" if skipped == 1 else "s"diff --git a/hermes_state_maintenance.py b/hermes_state_maintenance.pyindex 2092e3012b5..81746a3c00c 100644--- a/hermes_state_maintenance.py+++ b/hermes_state_maintenance.py@@ -100,7 +100,11 @@ _PINNED_TAIL_SQL = ("WITH RECURSIVE tail(id) AS ("" SELECT c.id FROM tail t JOIN sessions p ON p.id = t.id JOIN sessions c ON c.parent_session_id = p.id"f" WHERE {_CONTINUATION_EDGE_SQL}"") SELECT id FROM tail")-_NOT_PINNED_SQL = f"COALESCE(s.pinned, 0) = 0 AND s.id NOT IN ({_PINNED_TAIL_SQL})"+++def _not_pinned_sql(alias: str = "s") -> str:+ """Predicate sparing pinned rows and the unpinned continuations a pinned segment covers."""+ return f"COALESCE({alias}.pinned, 0) = 0 AND {alias}.id NOT IN ({_PINNED_TAIL_SQL})"class SessionMaintenanceMixin:@@ -182,8 +186,7 @@ class SessionMaintenanceMixin:if not (hb_grace is not None and hb_grace >= 0):hb_grace = hb_stalenesscutoff = (now := time.time()) - max_idle_seconds- pin_scope = (f" AND COALESCE(pinned, 0) = 0 AND sessions.id NOT IN ({_PINNED_TAIL_SQL})"- if exclude_pinned else "")+ pin_scope = f" AND {_not_pinned_sql('sessions')}" if exclude_pinned else ""orphan_predicate = f"started_at < ? AND {_sql_session_last_active('sessions')} < ?"heartbeat_params: Tuple[float, ...] = ()if respect_gateway_heartbeats:@@ -235,7 +238,7 @@ class SessionMaintenanceMixin:clauses.append(f"s.archived = {int(archived)}")# Pinned is a durable "keep" flag: bulk prune/delete/archive exclude pinned rows unless opted in.if not include_pinned:- clauses.append(_NOT_PINNED_SQL)+ clauses.append(_not_pinned_sql())return " AND ".join(clauses), paramsdef _prune_where(self, older_than_days, source, filters, *, whole_lineages: bool = False) -> Tuple[str, list]:@@ -272,6 +275,8 @@ class SessionMaintenanceMixin:pinned_only: bool = False, **filters) -> int:"""Count-only :meth:`list_prune_candidates`; ``pinned_only`` counts rows carrying the pinitself, not the continuations it protects (CLI reports spared pinned sessions)."""+ if pinned_only:+ filters["include_pinned"] = Truewhere, params = self._prune_where(older_than_days, source, filters)if pinned_only:where += " AND COALESCE(s.pinned, 0) = 1"@@ -298,7 +303,7 @@ class SessionMaintenanceMixin:if idle_days is None or idle_days < 0:return 0cutoff = time.time() - float(idle_days) * 86400.0- pin_clause = f"AND {_NOT_PINNED_SQL}" if exclude_pinned else ""+ pin_clause = f"AND {_not_pinned_sql()}" if exclude_pinned else ""rows = self._read_all(f"""SELECT s.id FROM sessions sdiff --git a/tests/hermes_cli/test_sessions_delete.py b/tests/hermes_cli/test_sessions_delete.pyindex 7015e47c5ca..03df47f48b4 100644--- a/tests/hermes_cli/test_sessions_delete.py+++ b/tests/hermes_cli/test_sessions_delete.py@@ -81,8 +81,8 @@ def _run_prune(monkeypatch, capsys, argv_tail, candidates=None, skipped_open=0):assert kwargs == {k: v for k, v in seen.items() if k != "whole_lineages"}return skipped_open- def count_prune_matches(self, **kwargs):- return len(rows)+ def count_prune_matches(self, pinned_only=False, **kwargs):+ return 0 if pinned_only else len(rows)def prune_sessions(self, **kwargs):return len(rows)