fix(session): serialize compaction with active turns
This commit is contained in:
1 parent
1d43b46e54
commit
2ad0da786d
6 files changed
+209
-29
No files matched your search
@@ -2,6 +2,7 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
from collections.abc import Collection
|
||||
from datetime import datetime
|
||||
from typing import TYPE_CHECKING, Any, Callable, Coroutine
|
||||
@@ -27,13 +28,15 @@ SessionEventFactory = Callable[[str], EventSink]
|
||||
class AutoCompact:
|
||||
def __init__(self, sessions: SessionManager, consolidator: Consolidator,
|
||||
session_ttl_minutes: int = 0,
|
||||
bind_events: SessionEventFactory | None = None):
|
||||
bind_events: SessionEventFactory | None = None,
|
||||
*, session_lock: Callable[[str], asyncio.Lock]):
|
||||
self.sessions = sessions
|
||||
self.consolidator = consolidator
|
||||
self._ttl = session_ttl_minutes
|
||||
self._archiving: set[str] = set()
|
||||
self._summaries: dict[str, SessionSummary] = {}
|
||||
self._bind_events = bind_events
|
||||
self._session_lock = session_lock
|
||||
|
||||
def _is_expired(self, ts: datetime | str | None,
|
||||
now: datetime | None = None) -> bool:
|
||||
@@ -91,19 +94,20 @@ class AutoCompact:
|
||||
self._archiving.discard(key)
|
||||
return
|
||||
try:
|
||||
summary = await self.consolidator.compact_idle_session(
|
||||
key,
|
||||
runtime=runtime,
|
||||
events=self._bind_events(key) if self._bind_events else NO_EVENTS,
|
||||
)
|
||||
if summary:
|
||||
session = self.sessions.get_or_create(key)
|
||||
stored = session_summary_from_metadata(
|
||||
session.metadata,
|
||||
fallback_last_active=session.updated_at,
|
||||
async with self._session_lock(key):
|
||||
summary = await self.consolidator.compact_idle_session(
|
||||
key,
|
||||
runtime=runtime,
|
||||
events=self._bind_events(key) if self._bind_events else NO_EVENTS,
|
||||
)
|
||||
if stored is not None:
|
||||
self._summaries[key] = stored
|
||||
if summary:
|
||||
session = self.sessions.get_or_create(key)
|
||||
stored = session_summary_from_metadata(
|
||||
session.metadata,
|
||||
fallback_last_active=session.updated_at,
|
||||
)
|
||||
if stored is not None:
|
||||
self._summaries[key] = stored
|
||||
except Exception:
|
||||
logger.exception("Auto-compact: failed for {}", key)
|
||||
finally:
|
||||
|
||||
@@ -437,6 +437,7 @@ class AgentLoop:
|
||||
consolidator=self.consolidator,
|
||||
session_ttl_minutes=session_ttl_minutes,
|
||||
bind_events=self._idle_events,
|
||||
session_lock=self._get_session_lock,
|
||||
)
|
||||
self._idle_compact_check_interval_s = idle_compact_check_interval_seconds
|
||||
self._next_idle_compact_check_at = time.monotonic()
|
||||
|
||||
+16
-14
@@ -210,20 +210,22 @@ class RuntimeClient:
|
||||
|
||||
async def compact_session(self, session_key: str) -> SessionSnapshot:
|
||||
"""Summarize one session and exclude its archived messages from replay."""
|
||||
session = self._loop.sessions.get_or_create(session_key)
|
||||
runtime = self._loop.runtime_for_session(session)
|
||||
await self._loop.consolidator.compact_idle_session(
|
||||
session_key,
|
||||
runtime=runtime,
|
||||
)
|
||||
return snapshot_from_session(self._loop.sessions.get_or_create(session_key))
|
||||
async with self._loop._get_session_lock(session_key): # pyright: ignore[reportPrivateUsage]
|
||||
session = self._loop.sessions.get_or_create(session_key)
|
||||
runtime = self._loop.runtime_for_session(session)
|
||||
await self._loop.consolidator.compact_idle_session(
|
||||
session_key,
|
||||
runtime=runtime,
|
||||
)
|
||||
return snapshot_from_session(self._loop.sessions.get_or_create(session_key))
|
||||
|
||||
async def compact_idle_session(self, session_key: str, *, max_suffix: int = 0) -> str | None:
|
||||
"""Return a replacement summary; legacy ``max_suffix`` no longer retains history."""
|
||||
session = self._loop.sessions.get_or_create(session_key)
|
||||
runtime = self._loop.runtime_for_session(session)
|
||||
return await self._loop.consolidator.compact_idle_session(
|
||||
session_key,
|
||||
runtime=runtime,
|
||||
max_suffix=max_suffix,
|
||||
)
|
||||
async with self._loop._get_session_lock(session_key): # pyright: ignore[reportPrivateUsage]
|
||||
session = self._loop.sessions.get_or_create(session_key)
|
||||
runtime = self._loop.runtime_for_session(session)
|
||||
return await self._loop.consolidator.compact_idle_session(
|
||||
session_key,
|
||||
runtime=runtime,
|
||||
max_suffix=max_suffix,
|
||||
)
|
||||
Reference in new issue
Block a user