fix: use dedicated _shutting_down flag for cancel-time checkpoint restore
Test Suite / Detect changes (push) Has been cancelled
Test Suite / webui (push) Has been cancelled
Test Suite / docker (push) Has been cancelled
Test Suite / Python (Windows, 3.14) (push) Has been cancelled
Test Suite / Python (minimum, 3.11) (push) Has been cancelled
Test Suite / Python (latest, 3.14 + coverage) (push) Has been cancelled

The previous guard used self._running, which is False both before run()
starts and during shutdown. Tests and process_direct() dispatch messages
with _running=False, so cancelled turns there silently skipped the
checkpoint restore and lost partial context. Introduce _shutting_down,
set only by stop(), and gate the restore on it.
This commit is contained in:
nanobot
2026-07-31 15:49:31 +08:00
parent c7270b8d81
commit 298b7ac6f6
+5 -3
View File
@@ -378,6 +378,7 @@ class AgentLoop:
)
self._unified_session = unified_session
self._running = False
self._shutting_down = False
self._mcp_servers = mcp_servers or {}
self._mcp_stacks: dict[str, MCPConnection] = {}
self._mcp_connecting = False
@@ -1224,11 +1225,11 @@ class AgentLoop:
# it into session history now makes it visible in the
# next conversation turn.
#
# During gateway shutdown (self._running is False), skip
# this so the pending markers survive and
# During gateway shutdown (self._shutting_down is True),
# skip this so the pending markers survive and
# recover_stale_sessions() can notify the user and
# re-trigger the interrupted turn on next startup.
if self._running:
if not self._shutting_down:
try:
key = self._effective_session_key(msg)
session = self.sessions.get_or_create(key)
@@ -1318,6 +1319,7 @@ class AgentLoop:
def stop(self) -> None:
"""Stop the agent loop."""
self._running = False
self._shutting_down = True
logger.info("Agent loop stopping")
async def _process_message(