The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(state): sweep turn-lease rows left behind by dead holders
kshitijk4poorc8173e038727
diff --git a/hermes_state_compression.py b/hermes_state_compression.pyindex f0a82bc601a..c09196cc58b 100644--- a/hermes_state_compression.py+++ b/hermes_state_compression.py@@ -49,6 +49,10 @@ _CHAIN_STEP_SQL = f""""""+# Turn-lease rows expired longer than this are swept by the next acquisition of any conversation.+_TURN_LEASE_SWEEP_GRACE_S = 86400.0++def _cooldown_row(exists: bool, cooldown_until, error) -> Dict[str, Any]:return {"session_exists": exists,"cooldown_until": float(cooldown_until) if cooldown_until is not None else None, "error": error}@@ -555,6 +559,12 @@ class SessionCompressionMixin:now = time.time()expires_at = now + max(0.1, float(ttl_seconds))def _do(conn):+ # Sweep rows that expired long ago: a holder that died without releasing leaves its row+ # behind, and nothing else revisits a conversation nobody resumes. The grace keeps the+ # recent expiries a still-live owner can renew from a starved refresher; it is also the+ # longest a suspended holder can go unrefreshed and still keep its lease.+ conn.execute("DELETE FROM session_turn_leases WHERE expires_at < ?",+ (now - _TURN_LEASE_SWEEP_GRACE_S,))conversation_id = self._session_turn_lease_key_on_conn(conn, session_id)return _claim_lease_row(conn, "session_turn_leases", "conversation_id", conversation_id, holder, now, expires_at,diff --git a/tests/hermes_state/test_session_turn_lease.py b/tests/hermes_state/test_session_turn_lease.pyindex a38f48c7baa..d2322276f81 100644--- a/tests/hermes_state/test_session_turn_lease.py+++ b/tests/hermes_state/test_session_turn_lease.py@@ -509,6 +509,34 @@ def test_turn_lease_revives_expired_row_still_owned_by_writer(tmp_path):)+def test_turn_lease_acquisition_sweeps_long_expired_rows_of_other_conversations(tmp_path):+ """A holder that died without releasing leaves its row behind; the next acquisition anywhere+ removes rows expired past the grace, and keeps recent expiries their live owner may renew."""+ db = SessionDB(tmp_path / "state.db")+ for sid in ("abandoned", "recent", "within-grace", "fresh"):+ db.create_session(sid, source="test")+ raw = sqlite3.connect(tmp_path / "state.db")+ with raw:+ raw.executemany(+ "INSERT INTO session_turn_leases (conversation_id, holder, acquired_at, expires_at) "+ "VALUES (?, ?, ?, ?)",+ [("abandoned", "pid=1:turn=crashed", time.time() - 86400 - 120, time.time() - 86400 - 60),+ ("recent", f"pid={os.getpid()}:turn=starved", time.time() - 60, time.time() - 10),+ ("within-grace", "pid=1:turn=suspended", time.time() - 86400, time.time() - 86400 + 60)])++ assert db.try_acquire_session_turn_lease("fresh", f"pid={os.getpid()}:turn=next", ttl_seconds=5)++ rows = dict(raw.execute("SELECT conversation_id, holder FROM session_turn_leases").fetchall())+ raw.close()+ assert rows == {"recent": f"pid={os.getpid()}:turn=starved",+ "within-grace": "pid=1:turn=suspended",+ "fresh": f"pid={os.getpid()}:turn=next"}+ assert db.append_messages_batch(+ "recent", [{"role": "assistant", "content": "after a starved refresher"}],+ turn_lease_holder=f"pid={os.getpid()}:turn=starved", turn_lease_ttl_seconds=5,+ ) == 1++def test_turn_lease_fences_flush_when_row_is_absent(tmp_path):db = SessionDB(tmp_path / "state.db")db.create_session("shared", source="test")