refactor: tighten session recency cleanup

maintainer edit: remove defensive branches that normal session storage cannot produce and keep the idle-expiry helper direct.
This commit is contained in:
chengyongru
2026-06-30 23:38:32 +08:00
committed by Xubin Ren
parent 840ba5af33
commit a6a489e0fa
2 changed files with 5 additions and 18 deletions
+5 -16
View File
@@ -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={},
-2
View File
@@ -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)