The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix: retain profile idle activity after session removal
Teknium44a583fcc861
diff --git a/hermes_cli/web_server_sessions.py b/hermes_cli/web_server_sessions.pyindex 44a0cda4870..106288207df 100644--- a/hermes_cli/web_server_sessions.py+++ b/hermes_cli/web_server_sessions.py@@ -234,12 +234,17 @@ def _skill_maintenance_idle_for(started_at: float) -> Optional[float]:"""Measure chat inactivity, not socket inactivity (Desktop stays connected)."""import tui_gateway.server as gateway+ from hermes_constants import get_hermes_home++ home = get_hermes_home().resolve()with gateway._sessions_lock:- if any(session.get("running") for session in gateway._sessions.values()):+ sessions = [session for session in gateway._sessions.values()+ if Path(session.get("profile_home") or home).resolve() == home]+ if any(session.get("running") for session in sessions):return Nonelast_active = max(- [started_at] + [float(session.get("last_active") or started_at)- for session in gateway._sessions.values()])+ [started_at, gateway._closed_session_activity.get(str(home), 0)]+ + [float(session.get("last_active") or started_at) for session in sessions])return max(0.0, time.time() - last_active)diff --git a/tests/hermes_cli/test_serve_skill_maintenance.py b/tests/hermes_cli/test_serve_skill_maintenance.pyindex c6b8d7ab84b..f744f30824a 100644--- a/tests/hermes_cli/test_serve_skill_maintenance.py+++ b/tests/hermes_cli/test_serve_skill_maintenance.py@@ -6,6 +6,53 @@ from contextlib import suppressimport pytest+async def test_removed_sessions_keep_profile_idle_watermark(tmp_path, monkeypatch):+ import time+ from pathlib import Path++ import tui_gateway.server as gateway+ from agent.curator import load_state, save_state+ from hermes_cli.web_server_sessions import _auto_archive_ticker_loop, _skill_maintenance_idle_for++ monkeypatch.setenv("HERMES_HOME", str(tmp_path))+ monkeypatch.setattr(Path, "home", lambda: tmp_path)+ (tmp_path / "skills").mkdir()+ (tmp_path / "config.yaml").write_text(+ "curator:\n enabled: true\n interval_hours: 168\n"+ " min_idle_hours: 0.002\n prune_builtins: false\n", encoding="utf-8")+ save_state({"last_run_at": "2020-01-01T00:00:00+00:00", "run_count": 0})+ # Let the actual timer age past its idle threshold before recent activity.+ task = asyncio.create_task(_auto_archive_ticker_loop(interval_s=.05, initial_delay_s=8))+ try:+ await asyncio.sleep(7.5)+ for reason in ("tui_close", "ws_orphan_reap"):+ recent = time.time()+ with gateway._sessions_lock:+ gateway._sessions["watermark"] = {+ "last_active": recent, "running": False, "profile_home": str(tmp_path)}+ assert gateway._close_session_by_id("watermark", end_reason=reason)+ assert _skill_maintenance_idle_for(recent - 3600) < 2+ # An active sibling profile must not hold this profile's idle clock.+ with gateway._sessions_lock:+ gateway._sessions["other-profile"] = {+ "running": True, "profile_home": str(tmp_path / "other")}+ assert _skill_maintenance_idle_for(recent - 3600) is not None+ await asyncio.sleep(2)+ assert load_state()["run_count"] == 0+ async with asyncio.timeout(12):+ while "consolidation off" not in (load_state().get("last_run_summary") or ""):+ await asyncio.sleep(.05)+ assert "consolidation off" in load_state()["last_run_summary"]+ finally:+ task.cancel()+ with suppress(asyncio.CancelledError):+ await task+ with gateway._sessions_lock:+ gateway._sessions.pop("other-profile", None)+ gateway._sessions.pop("watermark", None)++@pytest.mark.asyncioasync def test_serve_timer_runs_due_curator_once_and_honors_pause(tmp_path, monkeypatch):monkeypatch.setenv("HERMES_HOME", str(tmp_path))diff --git a/tui_gateway/session_lifecycle.py b/tui_gateway/session_lifecycle.pyindex 0679f1ea289..52f19cc7fed 100644--- a/tui_gateway/session_lifecycle.py+++ b/tui_gateway/session_lifecycle.py@@ -310,12 +310,21 @@ def _attach_worker(sid: str, session: dict, worker) -> None:worker.close()+# Wall-clock timestamps, like session last_active; retained after close/reap.+_closed_session_activity: dict[str, float] = {}++def _pop_session_by_id(sid: str) -> dict | None:"""Atomically detach one live session from the registry — the ownership claim for teardown (a concurrentclose/reaper no-ops). Separate from ``_teardown_session``: slow finalization must not run under the resume lock."""with _sessions_lock:session = _sessions.pop(sid, None)if session is not None:+ from hermes_constants import get_hermes_home++ home = str(Path(session.get("profile_home") or get_hermes_home()).resolve())+ last_active = time.time() if session.get("running") else float(session.get("last_active") or 0)+ _closed_session_activity[home] = max(_closed_session_activity.get(home, 0), last_active)session["_closing"] = Truesession["_sid"] = sid # out of _sessions now, so teardown can't recover the live id by scanningreturn sessiondiff --git a/website/docs/user-guide/features/curator.md b/website/docs/user-guide/features/curator.mdindex 8c8159d89e0..94b610d6e8a 100644--- a/website/docs/user-guide/features/curator.md+++ b/website/docs/user-guide/features/curator.md@@ -21,7 +21,7 @@ The curator is triggered by an inactivity check, not a cron job. On CLI session1. Enough time has passed since the last curator run (`interval_hours`, default **7 days**), and2. The agent has been idle long enough (`min_idle_hours`, default **2 hours**).-Desktop and other `hermes serve` backends share the existing hourly maintenance timer (first poll after 90 seconds), independently of cron jobs. They measure inactivity from process startup and the most recent chat activity, and skip curator while a turn is running. A connected but inactive window does not block maintenance. This timer also polls personal and organization Skill Sync, subject to those features' own opt-in gates. A running messaging gateway for the same profile owns these chores instead.+Desktop and other `hermes serve` backends share the existing hourly maintenance timer (first poll after 90 seconds), independently of cron jobs. They measure inactivity from process startup and the most recent chat activity in the same profile, retaining that activity timestamp after a session closes or is reaped, and skip curator while a turn in that profile is running. A connected but inactive window does not block maintenance. This timer also polls personal and organization Skill Sync, subject to those features' own opt-in gates. A running messaging gateway for the same profile owns these chores instead.The timer services its backend's profile. In-flight maintenance runs in a worker thread; closing the backend does not cooperatively interrupt that pass. Starting multiple independent serve processes for the same profile can still race the curator's interval check.