AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
fix(backend/copilot-bot): recover deleted chat sessions instead of erroring (#13197)
Bentlyb5ab847840d0
diff --git a/autogpt_platform/backend/backend/platform_linking/chat.py b/autogpt_platform/backend/backend/platform_linking/chat.pyindex 0b88cdbaf6..c1de9ca880 100644--- a/autogpt_platform/backend/backend/platform_linking/chat.py+++ b/autogpt_platform/backend/backend/platform_linking/chat.py@@ -50,18 +50,20 @@ async def start_chat_turn(request: BotChatRequest) -> ChatTurnHandle:"""owner_user_id = await resolve_chat_owner(request)+ session = Nonesession_id = request.session_idif session_id:session = await get_chat_session(session_id, owner_user_id)- if not session:- raise NotFoundError("Session not found.")- else:+ if session is None:+ # The bot caches session IDs across turns and a cached session can+ # be deleted from the web app in between — fall back to a fresh one+ # so the conversation self-heals instead of erroring every turn.session = await create_chat_session(owner_user_id,dry_run=False,source_platform=request.platform.value.lower(),)- session_id = session.session_id+ session_id = session.session_id# Persist the user message before enqueueing, mirroring the REST chat# endpoint — otherwise the executor runs against empty history.diff --git a/autogpt_platform/backend/backend/platform_linking/chat_test.py b/autogpt_platform/backend/backend/platform_linking/chat_test.pyindex 067ff42fa9..15a01fd884 100644--- a/autogpt_platform/backend/backend/platform_linking/chat_test.py+++ b/autogpt_platform/backend/backend/platform_linking/chat_test.py@@ -111,9 +111,13 @@ class TestStartChatTurn:mock_enqueue.assert_awaited_once()@pytest.mark.asyncio- async def test_existing_session_id_wrong_user_raises_not_found(self):+ async def test_stale_session_id_falls_back_to_fresh_session(self):+ # The bot caches session IDs across turns; a cached session can be+ # deleted from the web app in between. start_chat_turn must recover+ # by creating a fresh session instead of raising.db_mock = MagicMock()db_mock.find_user_link_owner = AsyncMock(return_value="owner-1")+ session = MagicMock(session_id="sess-fresh")with (patch(@@ -124,6 +128,24 @@ class TestStartChatTurn:"backend.platform_linking.chat.get_chat_session",new=AsyncMock(return_value=None),),+ patch(+ "backend.platform_linking.chat.create_chat_session",+ new=AsyncMock(return_value=session),+ ) as mock_create,+ patch(+ "backend.platform_linking.chat.append_and_save_message",+ new=AsyncMock(return_value=MagicMock()),+ ),+ patch(+ "backend.platform_linking.chat.stream_registry"+ ) as mock_stream_registry,+ patch(+ "backend.platform_linking.chat.enqueue_copilot_turn",+ new=AsyncMock(),+ ),):- with pytest.raises(NotFoundError):- await start_chat_turn(_request(session_id="someone-elses"))+ mock_stream_registry.create_session = AsyncMock()+ handle = await start_chat_turn(_request(session_id="deleted-session"))++ assert handle.session_id == "sess-fresh"+ mock_create.assert_awaited_once()