refactor(/new): background archival with guaranteed persistence
Replace fire-and-forget consolidation with archive_messages(), which retries until the raw-dump fallback triggers — making it effectively infallible. /new now clears the session immediately and archives in the background. Pending archive tasks are drained on shutdown via close_mcp() so no data is lost on process exit.
This commit is contained in:
@@ -290,14 +290,14 @@ class MemoryConsolidator:
|
||||
self._get_tool_definitions(),
|
||||
)
|
||||
|
||||
async def archive_unconsolidated(self, session: Session) -> bool:
|
||||
"""Archive the full unconsolidated tail for /new-style session rollover."""
|
||||
lock = self.get_lock(session.key)
|
||||
async with lock:
|
||||
snapshot = session.messages[session.last_consolidated:]
|
||||
if not snapshot:
|
||||
async def archive_messages(self, messages: list[dict[str, object]]) -> bool:
|
||||
"""Archive messages with guaranteed persistence (retries until raw-dump fallback)."""
|
||||
if not messages:
|
||||
return True
|
||||
for _ in range(self.store._MAX_FAILURES_BEFORE_RAW_ARCHIVE):
|
||||
if await self.consolidate_messages(messages):
|
||||
return True
|
||||
return await self.consolidate_messages(snapshot)
|
||||
return True
|
||||
|
||||
async def maybe_consolidate_by_tokens(self, session: Session) -> None:
|
||||
"""Loop: archive old messages until prompt fits within half the context window."""
|
||||
|
||||
Reference in New Issue
Block a user