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:
@@ -26,24 +26,13 @@ class AutoCompact:
|
|||||||
self._archiving: set[str] = set()
|
self._archiving: set[str] = set()
|
||||||
self._summaries: dict[str, tuple[str, datetime]] = {}
|
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,
|
def _is_expired(self, ts: datetime | str | None,
|
||||||
now: datetime | None = None) -> bool:
|
now: datetime | None = None) -> bool:
|
||||||
ts_epoch = self._timestamp(ts)
|
if self._ttl <= 0 or not ts:
|
||||||
if self._ttl <= 0 or ts_epoch is None:
|
|
||||||
return False
|
return False
|
||||||
now_epoch = self._timestamp(now or datetime.now())
|
if isinstance(ts, str):
|
||||||
return now_epoch is not None and now_epoch - ts_epoch >= self._ttl * 60
|
ts = datetime.fromisoformat(ts)
|
||||||
|
return ((now or datetime.now()) - ts).total_seconds() >= self._ttl * 60
|
||||||
|
|
||||||
def _has_compactable_idle_tail(self, key: str) -> bool:
|
def _has_compactable_idle_tail(self, key: str) -> bool:
|
||||||
session = self.sessions.get_or_create(key)
|
session = self.sessions.get_or_create(key)
|
||||||
@@ -52,7 +41,7 @@ class AutoCompact:
|
|||||||
return False
|
return False
|
||||||
probe = Session(
|
probe = Session(
|
||||||
key=session.key,
|
key=session.key,
|
||||||
messages=tail.copy(),
|
messages=tail,
|
||||||
created_at=session.created_at,
|
created_at=session.created_at,
|
||||||
updated_at=session.updated_at,
|
updated_at=session.updated_at,
|
||||||
metadata={},
|
metadata={},
|
||||||
|
|||||||
@@ -284,8 +284,6 @@ def _scan_session_row(session_manager: SessionManager, path: Path) -> dict[str,
|
|||||||
if not line.strip():
|
if not line.strip():
|
||||||
continue
|
continue
|
||||||
item = json.loads(line)
|
item = json.loads(line)
|
||||||
if item.get("_type") == "metadata":
|
|
||||||
continue
|
|
||||||
timestamp = _visible_message_timestamp(item)
|
timestamp = _visible_message_timestamp(item)
|
||||||
if timestamp is not None:
|
if timestamp is not None:
|
||||||
visible_message_at = _latest_updated_at(visible_message_at, timestamp)
|
visible_message_at = _latest_updated_at(visible_message_at, timestamp)
|
||||||
|
|||||||
Reference in New Issue
Block a user