From a6a489e0fa0783d161ddad3ffa3e53452661f415 Mon Sep 17 00:00:00 2001 From: chengyongru <2755839590@qq.com> Date: Tue, 30 Jun 2026 21:13:53 +0800 Subject: [PATCH] refactor: tighten session recency cleanup maintainer edit: remove defensive branches that normal session storage cannot produce and keep the idle-expiry helper direct. --- nanobot/agent/autocompact.py | 21 +++++---------------- nanobot/webui/session_list_index.py | 2 -- 2 files changed, 5 insertions(+), 18 deletions(-) diff --git a/nanobot/agent/autocompact.py b/nanobot/agent/autocompact.py index cd776281..38a2a390 100644 --- a/nanobot/agent/autocompact.py +++ b/nanobot/agent/autocompact.py @@ -26,24 +26,13 @@ class AutoCompact: self._archiving: set[str] = set() self._summaries: dict[str, tuple[str, datetime]] = {} - @staticmethod - def _timestamp(ts: datetime | str | None) -> float | None: - if not ts: - return None - if isinstance(ts, str): - try: - ts = datetime.fromisoformat(ts) - except ValueError: - return None - return ts.timestamp() - def _is_expired(self, ts: datetime | str | None, now: datetime | None = None) -> bool: - ts_epoch = self._timestamp(ts) - if self._ttl <= 0 or ts_epoch is None: + if self._ttl <= 0 or not ts: return False - now_epoch = self._timestamp(now or datetime.now()) - return now_epoch is not None and now_epoch - ts_epoch >= self._ttl * 60 + if isinstance(ts, str): + ts = datetime.fromisoformat(ts) + return ((now or datetime.now()) - ts).total_seconds() >= self._ttl * 60 def _has_compactable_idle_tail(self, key: str) -> bool: session = self.sessions.get_or_create(key) @@ -52,7 +41,7 @@ class AutoCompact: return False probe = Session( key=session.key, - messages=tail.copy(), + messages=tail, created_at=session.created_at, updated_at=session.updated_at, metadata={}, diff --git a/nanobot/webui/session_list_index.py b/nanobot/webui/session_list_index.py index e5b0547c..ccadcd07 100644 --- a/nanobot/webui/session_list_index.py +++ b/nanobot/webui/session_list_index.py @@ -284,8 +284,6 @@ def _scan_session_row(session_manager: SessionManager, path: Path) -> dict[str, if not line.strip(): continue item = json.loads(line) - if item.get("_type") == "metadata": - continue timestamp = _visible_message_timestamp(item) if timestamp is not None: visible_message_at = _latest_updated_at(visible_message_at, timestamp)