Your lightweight open-source personal agent.
Python agent-frameworkai-agentai-agentschatbotchatopsdiscord-botllm-agentsllmslocal-llmmcpmodel-context-protocolmulti-agentopenai-compatibleopenclawpersonal-ai-assistantpythonself-hostedtelegram-bot-ai-assistantwebuiworkflow-automation
refactor: use WeakValueDictionary for consolidation locks — auto-cleanup, no manual pop
Re-bin8410f859f734
diff --git a/nanobot/agent/loop.py b/nanobot/agent/loop.pyindex b605ae4a9..d8e5cad20 100644--- a/nanobot/agent/loop.py+++ b/nanobot/agent/loop.py@@ -5,6 +5,7 @@ from __future__ import annotationsimport asyncioimport jsonimport re+import weakreffrom contextlib import AsyncExitStackfrom pathlib import Pathfrom typing import TYPE_CHECKING, Any, Awaitable, Callable@@ -100,7 +101,7 @@ class AgentLoop:self._mcp_connecting = Falseself._consolidating: set[str] = set() # Session keys with consolidation in progressself._consolidation_tasks: set[asyncio.Task] = set() # Strong refs to in-flight tasks- self._consolidation_locks: dict[str, asyncio.Lock] = {}+ self._consolidation_locks: weakref.WeakValueDictionary[str, asyncio.Lock] = weakref.WeakValueDictionary()self._active_tasks: dict[str, list[asyncio.Task]] = {} # session_key -> tasksself._processing_lock = asyncio.Lock()self._register_default_tools()@@ -373,8 +374,6 @@ class AgentLoop:)finally:self._consolidating.discard(session.key)- if not lock.locked():- self._consolidation_locks.pop(session.key, None)session.clear()self.sessions.save(session)@@ -396,8 +395,6 @@ class AgentLoop:await self._consolidate_memory(session)finally:self._consolidating.discard(session.key)- if not lock.locked():- self._consolidation_locks.pop(session.key, None)_task = asyncio.current_task()if _task is not None:self._consolidation_tasks.discard(_task)diff --git a/tests/test_consolidate_offset.py b/tests/test_consolidate_offset.pyindex 675512406..a3213ddc8 100644--- a/tests/test_consolidate_offset.py+++ b/tests/test_consolidate_offset.py@@ -786,10 +786,8 @@ class TestConsolidationDeduplicationGuard:)@pytest.mark.asyncio- async def test_new_cleans_up_consolidation_lock_for_invalidated_session(- self, tmp_path: Path- ) -> None:- """/new should remove lock entry for fully invalidated session key."""+ async def test_new_clears_session_and_responds(self, tmp_path: Path) -> None:+ """/new clears session and returns confirmation."""from nanobot.agent.loop import AgentLoopfrom nanobot.bus.events import InboundMessagefrom nanobot.bus.queue import MessageBus@@ -801,7 +799,6 @@ class TestConsolidationDeduplicationGuard:loop = AgentLoop(bus=bus, provider=provider, workspace=tmp_path, model="test-model", memory_window=10)-loop.provider.chat = AsyncMock(return_value=LLMResponse(content="ok", tool_calls=[]))loop.tools.get_definitions = MagicMock(return_value=[])@@ -811,10 +808,6 @@ class TestConsolidationDeduplicationGuard:session.add_message("assistant", f"resp{i}")loop.sessions.save(session)- # Ensure lock exists before /new.- loop._consolidation_locks.setdefault(session.key, asyncio.Lock())- assert session.key in loop._consolidation_locks-async def _ok_consolidate(sess, archive_all: bool = False) -> bool:return True@@ -825,4 +818,4 @@ class TestConsolidationDeduplicationGuard:assert response is not Noneassert "new session started" in response.content.lower()- assert session.key not in loop._consolidation_locks+ assert loop.sessions.get_or_create("cli:test").messages == []