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): log when a queued turn starts, is cancelled, or gives its slot back (#15475)
Reinier van der Leer2d92cd2d34b4
diff --git a/autogpt_platform/backend/backend/copilot/active_turns.py b/autogpt_platform/backend/backend/copilot/active_turns.pyindex be3aa6dc3c..d056f2f74f 100644--- a/autogpt_platform/backend/backend/copilot/active_turns.py+++ b/autogpt_platform/backend/backend/copilot/active_turns.py@@ -29,6 +29,7 @@ the dispatcher works from both the HTTP server (Prisma directly) andthe copilot_executor process (RPC via DatabaseManager)."""+import loggingfrom contextlib import asynccontextmanagerfrom typing import AsyncIterator@@ -40,6 +41,8 @@ from backend.copilot.model import (from backend.data.db_accessors import chat_dbfrom backend.util.settings import Settings+logger = logging.getLogger(__name__)+# Upper bound on a single Otto turn's wall-clock duration. Re-exported# for callers (e.g. ``backend.blocks.autopilot``) that need a sensible# upper-wait timeout. Stale running sessions older than this are an@@ -132,12 +135,16 @@ async def release_turn_slot(user_id: str, session_id: str) -> None:changed (e.g. a parallel cancel)."""if not user_id:return- await chat_db().update_chat_session_status(+ if await chat_db().update_chat_session_status(session_id=session_id,expect_status=CHAT_STATUS_RUNNING,status=CHAT_STATUS_IDLE,user_id=user_id,- )+ ):+ logger.info(+ f"release_turn_slot: session={session_id} user={user_id} "+ "gave its running slot back"+ )class TurnSlot:diff --git a/autogpt_platform/backend/backend/copilot/active_turns_test.py b/autogpt_platform/backend/backend/copilot/active_turns_test.pyindex c050a48371..09ec6d9bdc 100644--- a/autogpt_platform/backend/backend/copilot/active_turns_test.py+++ b/autogpt_platform/backend/backend/copilot/active_turns_test.py@@ -5,6 +5,7 @@ tests patch ``backend.copilot.active_turns.chat_db`` to return an:class:`unittest.mock.AsyncMock`."""+import loggingfrom unittest.mock import AsyncMock, MagicMock, patchimport pytest@@ -52,6 +53,39 @@ async def test_release_flips_running_to_idle_with_userid_guard() -> None:)[email protected]("flipped", [True, False], ids=["released", "not-running"])+async def test_a_released_slot_is_logged_only_when_it_was_held(+ caplog: pytest.LogCaptureFixture, flipped: bool+) -> None:+ """A turn's end and a parallel cancel can both release; only the one that+ flipped the row says so."""+ db = _mock_db()+ db.update_chat_session_status = AsyncMock(return_value=flipped)+ with (+ patch.object(active_turns, "chat_db", return_value=db),+ caplog.at_level(logging.INFO, logger=active_turns.logger.name),+ ):+ await release_turn_slot("user-1", "session-a")++ released = [+ (r.levelno, r.getMessage())+ for r in caplog.records+ if r.name == active_turns.logger.name+ ]+ assert released == (+ [+ (+ logging.INFO,+ "release_turn_slot: session=session-a user=user-1 "+ "gave its running slot back",+ )+ ]+ if flipped+ else []+ )++@pytest.mark.asyncioasync def test_release_anonymous_user_is_noop() -> None:"""``release_turn_slot`` with empty user_id makes no DB write."""diff --git a/autogpt_platform/backend/backend/copilot/turn_queue.py b/autogpt_platform/backend/backend/copilot/turn_queue.pyindex d501f71106..9bba285d4f 100644--- a/autogpt_platform/backend/backend/copilot/turn_queue.py+++ b/autogpt_platform/backend/backend/copilot/turn_queue.py@@ -338,7 +338,12 @@ async def cancel_queued_turn(*, user_id: str, session_id: str) -> bool:user_id=user_id,):return False- if pending is not None and _is_spawned(pending):+ spawned = pending is not None and _is_spawned(pending)+ logger.info(+ f"cancel_queued_turn: session={session_id} user={user_id} "+ f"cancelled before it started, spawned={spawned}"+ )+ if spawned and pending is not None:child = _child_node(pending.metadata or {})await _note_cancel(db, session_id)await invalidate_session_cache(session_id)@@ -575,6 +580,11 @@ async def _promote_head(user_id: str) -> bool | None:)raise+ logger.info(+ f"dispatch_next_for_user: started queued session={head.session_id} "+ f"user={user_id} turn={turn_id} sub_work={sub_work[head.session_id]}"+ )+if pending.role == "user" and pending.content and not spawned:try:track_user_message(diff --git a/autogpt_platform/backend/backend/copilot/turn_queue_test.py b/autogpt_platform/backend/backend/copilot/turn_queue_test.pyindex 4382c05254..b395d70ee1 100644--- a/autogpt_platform/backend/backend/copilot/turn_queue_test.py+++ b/autogpt_platform/backend/backend/copilot/turn_queue_test.py@@ -8,6 +8,7 @@ for Prisma directly while still exercising the queue's branching."""import asyncio+import loggingimport sysfrom datetime import datetime, timezonefrom unittest.mock import AsyncMock, MagicMock, patch@@ -303,6 +304,88 @@ async def test_cancelling_a_queued_turn_returns_only_a_childs_node(db.add_chat_message.assert_not_awaited()[email protected]("flipped", [True, False], ids=["cancelled", "not-queued"])+async def test_a_queued_turn_cancelled_before_it_started_is_logged(+ caplog: pytest.LogCaptureFixture, flipped: bool+) -> None:+ db = MagicMock()+ db.update_chat_session_status = AsyncMock(return_value=flipped)+ db.get_latest_user_message_in_session = AsyncMock(+ return_value=_pyd_message(metadata={"envelope": _CHILD})+ )+ db.get_next_sequence = AsyncMock(return_value=2)+ db.add_chat_message = AsyncMock()+ with (+ patch.object(turn_queue, "chat_db", return_value=db),+ patch.object(turn_queue, "invalidate_session_cache", new=AsyncMock()),+ patch.object(turn_queue, "release_turn", new=AsyncMock()),+ caplog.at_level(logging.INFO, logger=turn_queue.logger.name),+ ):+ await turn_queue.cancel_queued_turn(user_id="u1", session_id="s1")++ lines = [+ r for r in caplog.records if "cancelled before it started" in r.getMessage()+ ]+ if flipped:+ assert [(r.levelno, r.getMessage()) for r in lines] == [+ (+ logging.INFO,+ "cancel_queued_turn: session=s1 user=u1 cancelled before it "+ "started, spawned=True",+ )+ ]+ else:+ assert lines == []++[email protected]("started", [True, False], ids=["started", "refused"])+async def test_a_queued_sub_session_that_starts_is_logged(+ caplog: pytest.LogCaptureFixture, started: bool+) -> None:+ head = _queued_row("child")+ head.metadata.delegated_by_session_id = "parent"+ head.metadata.llm_auth_provider = "codex"+ db = MagicMock()+ db.update_chat_session_status = AsyncMock(return_value=True)+ db.get_latest_user_message_in_session = AsyncMock(+ return_value=_pyd_message(metadata={"envelope": _CHILD})+ )+ dispatched = AsyncMock(+ side_effect=None if started else TreeRefusal("This tree has closed.")+ )+ with (+ patch.object(+ turn_queue, "list_queued_sessions", new=AsyncMock(side_effect=[[head], []])+ ),+ patch.object(turn_queue, "chat_db", return_value=db),+ patch.object(turn_queue, "has_codex_access", new=AsyncMock(return_value=True)),+ patch.object(+ turn_queue, "claim_queued_session", new=AsyncMock(return_value="admitted")+ ),+ patch.object(turn_queue, "invalidate_session_cache", new=AsyncMock()),+ patch.object(turn_queue, "append_and_save_message", new=AsyncMock()),+ patch.object(turn_queue, "release_turn", new=AsyncMock()),+ patch("backend.copilot.executor.utils.dispatch_turn", new=dispatched),+ caplog.at_level(logging.INFO, logger=turn_queue.logger.name),+ ):+ assert await turn_queue.dispatch_next_for_user("u1") is started++ lines = [r for r in caplog.records if "started queued" in r.getMessage()]+ if started:+ turn_id = dispatched.await_args.kwargs["turn_id"]+ assert [(r.levelno, r.getMessage()) for r in lines] == [+ (+ logging.INFO,+ f"dispatch_next_for_user: started queued session=child user=u1 "+ f"turn={turn_id} sub_work=True",+ )+ ]+ else:+ assert lines == []++@pytest.mark.asyncioasync def test_a_queued_child_keeps_its_tree_apart_from_its_envelope() -> None:"""So a deploy that stops parsing the envelope can still give its node back."""