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
fix(agent): discard stopped follow-up recovery journal
yu-xin-c1127b3ea20bd
diff --git a/nanobot/agent/loop.py b/nanobot/agent/loop.pyindex 6443344a0..3ef4258f2 100644--- a/nanobot/agent/loop.py+++ b/nanobot/agent/loop.py@@ -85,6 +85,7 @@ from nanobot.session.recovery import (RECOVERY_INBOUND_METADATA_KEY,RecoveryAdmission,acknowledge_pending_followups,+ pending_followups,record_pending_followup,restore_pending_interruption,restore_runtime_checkpoint,@@ -855,6 +856,19 @@ class AgentLoop:Returns the total number of cancelled tasks, subagents, and exec sessions."""+ journal_session = self.sessions.get_cached(key) or self.sessions.read_session_snapshot(key)+ journaled_followup_ids = (+ tuple(+ followup_id+ for followup in pending_followups(journal_session)+ if isinstance(+ followup_id := followup.metadata.get(PENDING_FOLLOWUP_ID_KEY),+ str,+ )+ )+ if journal_session is not None+ else ()+ )pending = self._pending_queues.get(key)tasks = tuple(self._active_tasks.pop(key, set()))cancelled = sum(1 for t in tasks if not t.done() and t.cancel())@@ -866,6 +880,13 @@ class AgentLoop:# cleanup handler. Only reclaim that worker's original inbox.self._pending_queues.pop(key, None)await self._cancel_pending_messages(key, pending, asyncio.CancelledError())+ if journaled_followup_ids and journal_session is not None:+ # Explicit session cancellation owns the follow-ups accepted before+ # it began. Gateway shutdown uses aclose() directly and keeps this+ # journal intact for startup recovery.+ current_session = self.sessions.get_cached(key) or journal_session+ acknowledge_pending_followups(current_session, journaled_followup_ids)+ self.sessions.save(current_session)sub_cancelled = await self.subagents.cancel_by_session(key)exec_cancelled = await self._exec_session_manager.terminate_by_owner(key)return cancelled + sub_cancelled + exec_cancelleddiff --git a/tests/agent/test_session_inbox_controls.py b/tests/agent/test_session_inbox_controls.pyindex 9173b0186..fcb82b054 100644--- a/tests/agent/test_session_inbox_controls.py+++ b/tests/agent/test_session_inbox_controls.py@@ -18,7 +18,7 @@ from nanobot.providers.base import GenerationSettings, LLMResponse, ToolCallRequfrom nanobot.runtime_context import public_history_messagesfrom nanobot.session.automation_turns import AUTOMATION_HISTORY_METAfrom nanobot.session.goal_state import GOAL_STATE_KEY-from nanobot.session.recovery import PENDING_FOLLOWUPS_KEY+from nanobot.session.recovery import PENDING_FOLLOWUPS_KEY, pending_followupsfrom nanobot.triggers.local_session_turns import LOCAL_TRIGGER_META@@ -233,6 +233,66 @@ async def test_cancelled_session_completes_queued_automation_waiters(loop):await asyncio.gather(*tasks, return_exceptions=True)[email protected]("followup_still_queued", [True, False])+async def test_explicit_cancel_discards_webui_followup_recovery_journal(+ loop,+ followup_still_queued,+):+ started = asyncio.Event()++ async def chat(**kwargs):+ started.set()+ await asyncio.Event().wait()++ loop.provider.chat_stream_with_retry = chat+ key = "websocket:test"+ loop._enqueue_session_message(InboundMessage(+ channel="websocket", sender_id="u", chat_id="test", content="first",+ metadata={"webui": True},+ ))+ await asyncio.wait_for(started.wait(), timeout=3)+ loop._enqueue_session_message(InboundMessage(+ channel="websocket", sender_id="u", chat_id="test", content="cancel me",+ metadata={"webui": True},+ ))++ if not followup_still_queued:+ consumed = loop._pending_queues[key].get_nowait()+ assert consumed.content == "cancel me"+ session = loop.sessions.get_or_create(key)+ assert [message.content for message in pending_followups(session)] == ["cancel me"]++ await loop._cancel_active_tasks(key)++ assert pending_followups(loop.sessions.get_or_create(key)) == []+++async def test_shutdown_preserves_webui_followup_recovery_journal(loop):+ started = asyncio.Event()++ async def chat(**kwargs):+ started.set()+ await asyncio.Event().wait()++ loop.provider.chat_stream_with_retry = chat+ key = "websocket:test"+ loop._enqueue_session_message(InboundMessage(+ channel="websocket", sender_id="u", chat_id="test", content="first",+ metadata={"webui": True},+ ))+ await asyncio.wait_for(started.wait(), timeout=3)+ loop._enqueue_session_message(InboundMessage(+ channel="websocket", sender_id="u", chat_id="test", content="recover me",+ metadata={"webui": True},+ ))++ loop.preserve_inflight_turns_on_shutdown()+ await loop.aclose()++ session = loop.sessions.get_or_create(key)+ assert [message.content for message in pending_followups(session)] == ["recover me"]++@pytest.mark.parametrize("action", ["stop", "close"])async def test_cancel_before_worker_runs_completes_automation(loop, action):loop.provider.chat_stream_with_retry = AsyncMock(return_value=LLMResponse(content="unused"))