Skip to content

Your lightweight open-source personal agent.

Merge remote-tracking branch 'origin/main' into pr-1961

Xubin Ren6d63e22e865d
diff --cc .gitignore
index e5f9baf44,ef52b9c44..fce6e07f8
--- a/.gitignore
+++ b/.gitignore
@@@ -20,6 -20,4 +20,6 @@@ __pycache__
poetry.lock
.pytest_cache/
botpy.log
-nano.*.save
+nano.*.save
+.DS_Store
- uv.lock
++uv.lock
diff --cc nanobot/agent/memory.py
index 9a4e0d77e,5fdfa7a06..64ec7711a
--- a/nanobot/agent/memory.py
+++ b/nanobot/agent/memory.py
@@@ -270,35 -290,17 +341,35 @@@ 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."""
+ def maybe_consolidate_by_tokens(self, session: Session) -> None:
+ """Schedule token-based consolidation to run asynchronously in background.
+
+ This method is synchronous and just schedules the consolidation task.
+ The actual consolidation runs in the background when the session is idle.
+ """
+ if not session.messages or self.context_window_tokens <= 0:
+ return
+ # Schedule for background execution
+ asyncio.create_task(self._schedule_consolidation(session))
+
+ async def _schedule_consolidation(self, session: Session) -> None:
+ """Internal method to run consolidation asynchronously."""
+ await self.maybe_consolidate_by_tokens_async(session)
+
+ async def maybe_consolidate_by_tokens_async(self, session: Session) -> None:
+ """Async version: Loop and archive old messages until prompt fits within half the context window.
+
+ This is called from the background task when a session is idle.
+ """
if not session.messages or self.context_window_tokens <= 0:
return