Skip to content

The agent that grows with you

refactor(state): move maybe_auto_archive beside archive_stale_sessions

Teknium81787cbf1f11
diff --git a/gateway/platforms/api_server.py b/gateway/platforms/api_server.py
index f50c17616db..9e8096c3233 100644
--- a/gateway/platforms/api_server.py
+++ b/gateway/platforms/api_server.py
@@ -3122,8 +3122,6 @@ class APIServerAdapter(OpenAICompatRoutesMixin, BasePlatformAdapter):
try:
db._resolve_title_conflict(conn, session_id, clean_title)
except ValueError as exc: # the DB's uniqueness rule; undo the INSERT
- # No ``on_session_delete`` here: this rolls back the row inserted in the
- # same transaction, so the session never existed — firing would double-count it.
conn.execute("DELETE FROM sessions WHERE id = ?", (session_id,))
return None, f"title:{exc}"
conn.execute("UPDATE sessions SET title = ? WHERE id = ?", (clean_title, session_id))
diff --git a/hermes_state_maintenance.py b/hermes_state_maintenance.py
index 345cd749e1d..c5d699504c9 100644
--- a/hermes_state_maintenance.py
+++ b/hermes_state_maintenance.py
@@ -331,6 +331,37 @@ class SessionMaintenanceMixin:
self._auto_archive_lineage(row[0])
return len(rows)
+ def maybe_auto_archive(
+ self, idle_days: float = 3, min_interval_hours: int = 24, exclude_pinned: bool = True,
+ ) -> dict[str, Any]:
+ """Idempotent, non-destructive auto-archive of sessions idle for ``idle_days``; state_meta
+ ``last_auto_archive`` gates runs within ``min_interval_hours``. Never raises."""
+ result: dict[str, Any] = {"skipped": False, "archived": 0}
+ try:
+ now = time.time()
+ try:
+ last = float(self.get_meta("last_auto_archive") or 0.0)
+ except (TypeError, ValueError):
+ last = 0.0 # corrupt meta; treat as no prior run
+ if last and now - last < min_interval_hours * 3600:
+ result["skipped"] = True
+ return result
+ # Startup-watchdog lease: the archive sweep is I/O-bound (near-zero CPU),
+ # which the watchdog's CPU fallback misreads as a parked deadlock.
+ # No-op when the watchdog is not armed; never raises.
+ report_startup_progress(900.0, phase="state_db_auto_archive")
+ archived = result["archived"] = self.archive_stale_sessions(idle_days, exclude_pinned=exclude_pinned)
+ # Record even a zero-archive run so we don't re-sweep every call.
+ self.set_meta("last_auto_archive", str(now))
+ if archived > 0:
+ logger.info(
+ "state.db auto-archive: archived %d session(s) idle >= %s days", archived, idle_days,
+ )
+ except Exception as exc:
+ logger.warning("state.db auto-archive failed: %s", exc)
+ result["error"] = str(exc)
+ return result
+
def prune_sessions(self, older_than_days: Optional[float] = 90, source: str | None = None,
sessions_dir: Optional[Path] = None, exclude_active_write_guards: bool = False,
deleted_ids: Optional[list] = None, **filters) -> int:
diff --git a/hermes_state_sessions.py b/hermes_state_sessions.py
index 7d70e8a6b6b..8ba80c448f1 100644
--- a/hermes_state_sessions.py
+++ b/hermes_state_sessions.py
@@ -14,7 +14,6 @@ from typing import Any, Callable, Dict, List, Optional, Sequence, Tuple, Union
from agent.session_activity import (
ActivityProvenance, bound_activity_description, normalize_activity_provenance,
)
-from hermes_startup_watchdog import report_startup_progress
from hermes_state_errors import SessionActiveWriteGuardError
from hermes_state_common import (
_LISTABLE_CHILD_SQL, _RECOVERABLE_END_REASONS,
@@ -1978,34 +1977,3 @@ class SessionSessionsMixin:
for row in rows:
self.set_session_archived(row["id"], True)
return len(rows)
-
- def maybe_auto_archive(
- self, idle_days: float = 3, min_interval_hours: int = 24, exclude_pinned: bool = True,
- ) -> dict[str, Any]:
- """Idempotent, non-destructive auto-archive of sessions idle for ``idle_days``; state_meta
- ``last_auto_archive`` gates runs within ``min_interval_hours``. Never raises."""
- result: dict[str, Any] = {"skipped": False, "archived": 0}
- try:
- now = time.time()
- try:
- last = float(self.get_meta("last_auto_archive") or 0.0)
- except (TypeError, ValueError):
- last = 0.0 # corrupt meta; treat as no prior run
- if last and now - last < min_interval_hours * 3600:
- result["skipped"] = True
- return result
- # Startup-watchdog lease: the archive sweep is I/O-bound (near-zero CPU),
- # which the watchdog's CPU fallback misreads as a parked deadlock.
- # No-op when the watchdog is not armed; never raises.
- report_startup_progress(900.0, phase="state_db_auto_archive")
- archived = result["archived"] = self.archive_stale_sessions(idle_days, exclude_pinned=exclude_pinned)
- # Record even a zero-archive run so we don't re-sweep every call.
- self.set_meta("last_auto_archive", str(now))
- if archived > 0:
- logger.info(
- "state.db auto-archive: archived %d session(s) idle >= %s days", archived, idle_days,
- )
- except Exception as exc:
- logger.warning("state.db auto-archive failed: %s", exc)
- result["error"] = str(exc)
- return result