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(platform/copilot): a burst of approval answers runs in one turn that is never dropped (#15062)
Reinier van der Leere0217702d0f8
diff --git a/.github/scripts/backend-allowed-skips.json b/.github/scripts/backend-allowed-skips.jsonindex 1dc298f4f6..9c470c7358 100644--- a/.github/scripts/backend-allowed-skips.json+++ b/.github/scripts/backend-allowed-skips.json@@ -119,7 +119,6 @@"backend.copilot.stream_drift.drift_causes_test.test_a_dispatch_does_not_take_over_a_running_turns_meta","backend.copilot.stream_drift.drift_causes_test.test_a_reply_that_failed_to_persist_ends_the_turn_in_error","backend.copilot.stream_drift.drift_causes_test.test_a_silent_turn_keeps_publishing_within_the_stream_ttl",- "backend.copilot.stream_drift.drift_causes_test.test_a_turn_the_executor_drops_is_not_left_running","backend.copilot.stream_drift.drift_causes_test.test_a_turn_whose_executor_died_is_not_reported_running","backend.copilot.test_copilot_e2e.test_session_state_persistence","backend.copilot.tools.agent_browser_integration_test.test_chromium_executable_env_is_set",diff --git a/autogpt_platform/backend/backend/api/features/graph_executions/review/model.py b/autogpt_platform/backend/backend/api/features/graph_executions/review/model.pyindex 18aef6abbf..8eea352ff9 100644--- a/autogpt_platform/backend/backend/api/features/graph_executions/review/model.py+++ b/autogpt_platform/backend/backend/api/features/graph_executions/review/model.py@@ -248,6 +248,10 @@ class ReviewRequest(BaseModel):reviews: List[ReviewItem] = Field(description="All reviews with their approval status, data, and messages")+ wait_for_other_cards: bool = Field(+ False,+ description="Set by a chat's own queue, which can continue: no turn starts while other cards in the chat still wait",+ )@model_validator(mode="after")def validate_review_completeness(self):@@ -271,6 +275,10 @@ class ReviewResponse(BaseModel):rejected_count: int = Field(description="Number of reviews successfully rejected")failed_count: int = Field(description="Number of reviews that failed processing")error: str | None = Field(None, description="Error message if operation failed")+ awaiting_other_cards: bool = Field(+ False,+ description="The answers start no chat turn yet: other cards in the chat still wait",+ )def _block_named(name: str | None):diff --git a/autogpt_platform/backend/backend/api/features/graph_executions/review/review_routes_test.py b/autogpt_platform/backend/backend/api/features/graph_executions/review/review_routes_test.pyindex 813b359736..8326eb3957 100644--- a/autogpt_platform/backend/backend/api/features/graph_executions/review/review_routes_test.py+++ b/autogpt_platform/backend/backend/api/features/graph_executions/review/review_routes_test.py@@ -1,6 +1,7 @@import datetimefrom types import SimpleNamespacefrom typing import AsyncGenerator+from unittest.mock import AsyncMock, patchimport httpximport pytest@@ -12,7 +13,16 @@ from pytest_snapshot.plugin import Snapshotfrom backend.api.rest_api import appfrom backend.blocks.github._auth import TEST_CREDENTIALSfrom backend.blocks.github.issues import GithubMakeIssueBlock+from backend.copilot.constants import step_card_id+from backend.copilot.gate import review as review_storefrom backend.copilot.gate.held import HeldCall+from backend.copilot.gate.held_db_test import (+ _hold,+ _new_session,+ _Post,+ _resolved,+ _row,+)from backend.data.execution import (ExecutionContext,ExecutionStatus,@@ -1388,9 +1398,14 @@ async def test_process_review_action_per_review_auto_approve_granularity(@pytest.mark.asyncio(loop_scope="session")@pytest.mark.parametrize(- "graph_exec_id, session_id, woken",- [(None, "s1", "s1"), ("test_graph_exec_456", None, None)],+ "graph_exec_id, session_id, woken, started",+ [+ (None, "s1", "s1", True),+ (None, "s1", "s1", False),+ ("test_graph_exec_456", None, None, False),+ ],)[email protected]("wait", [True, False])async def test_an_answer_on_a_chat_card_wakes_that_chat(client: httpx.AsyncClient,mocker: pytest_mock.MockerFixture,@@ -1399,8 +1414,11 @@ async def test_an_answer_on_a_chat_card_wakes_that_chat(graph_exec_id: str | None,session_id: str | None,woken: str | None,+ started: bool,+ wait: bool,) -> None:- """A held call finishes on the chat's next turn; nothing else starts one."""+ """A held call finishes on the chat's next turn, from the chat's own queue+ once its last card is answered; the page is told when the answer waits."""review = sample_pending_review.model_copy(update={"graph_exec_id": graph_exec_id, "session_id": session_id})@@ -1419,11 +1437,14 @@ async def test_an_answer_on_a_chat_card_wakes_that_chat(},)mocker.patch(f"{routes}.has_pending_reviews_for_graph_exec", return_value=True)- wake = mocker.patch(f"{routes}.wake_for_held_calls")+ wake = mocker.patch(f"{routes}.wake_for_held_calls", return_value=started)response = await client.post("/api/review/action",- json={"reviews": [{"node_exec_id": "test_node_123", "approved": True}]},+ json={+ "reviews": [{"node_exec_id": "test_node_123", "approved": True}],+ "wait_for_other_cards": wait,+ },)assert response.status_code == 200@@ -1431,8 +1452,26 @@ async def test_an_answer_on_a_chat_card_wakes_that_chat(wake.assert_awaited_once()assert wake.await_args.args[:2] == (test_user_id, woken)assert [r.node_exec_id for r in wake.await_args.args[2]] == ["test_node_123"]+ assert wake.await_args.kwargs == {"until_last": wait}+ assert response.json()["awaiting_other_cards"] is not startedelse:wake.assert_not_called()+ assert response.json()["awaiting_other_cards"] is False++[email protected](loop_scope="session")+async def test_continue_wakes_the_chat_without_waiting_for_its_other_cards(+ client: httpx.AsyncClient,+ mocker: pytest_mock.MockerFixture,+ test_user_id: str,+) -> None:+ routes = "backend.api.features.graph_executions.review.routes"+ wake = mocker.patch(f"{routes}.wake_for_held_calls", return_value=True)++ response = await client.post("/api/review/session/s1/continue")++ assert response.status_code == 204+ wake.assert_awaited_once_with(test_user_id, "s1")@pytest.mark.asyncio(loop_scope="session")@@ -1634,3 +1673,99 @@ async def test_a_rejected_irreversible_action_never_runs_when_the_run_resumes(with pytest.raises(BlockExecutionError, match="rejected by reviewer"):await run_node(resumed)create_issue.assert_not_awaited()++[email protected](loop_scope="session")+async def test_a_burst_answered_through_the_route_starts_one_turn_past_a_step_card(+ client: httpx.AsyncClient, setup_test_user: str, test_user_id: str+) -> None:+ """A workflow step's card waiting in the same chat holds no turn back: its+ answer resumes the run, not the chat, and it can leave from the run page."""+ session = await _new_session(test_user_id)+ post = _Post()+ with (+ patch("backend.copilot.gate.is_feature_enabled", AsyncMock(return_value=True)),+ patch("backend.copilot.tools.get_tool", return_value=post),+ ):+ cards = [await _hold(session, test_user_id, text) for text in "abc"]+ step = step_card_id(test_user_id, f"step-{session.session_id}")+ assert await review_store.open_review_row(+ step, test_user_id, session, {}, "Run a workflow step"+ )+ dispatch = AsyncMock()+ with patch("backend.copilot.executor.utils.dispatch_turn", dispatch):+ waits = []+ for card in cards:+ response = await client.post(+ "/api/review/action",+ json={+ "reviews": [{"node_exec_id": card, "approved": True}],+ "wait_for_other_cards": True,+ },+ )+ assert response.status_code == 200+ waits.append(response.json()["awaiting_other_cards"])+ delivered = await _resolved(test_user_id, session)++ assert waits == [True, True, False]+ dispatch.assert_awaited_once()+ assert len(delivered) == 3+ assert sorted(run["text"] for run in post.runs) == ["a", "b", "c"]+ step_row = await _row(step, test_user_id)+ assert step_row is not None and step_row.status == ReviewStatus.WAITING++[email protected](loop_scope="session")+async def test_an_answer_from_outside_the_chat_starts_its_turn_at_once(+ client: httpx.AsyncClient, setup_test_user: str, test_user_id: str+) -> None:+ """/home and the Expert page cannot continue a chat, so an answer there+ never waits for the chat's other cards."""+ session = await _new_session(test_user_id)+ with patch("backend.copilot.gate.is_feature_enabled", AsyncMock(return_value=True)):+ answered, waiting = [await _hold(session, test_user_id, t) for t in "ab"]+ dispatch = AsyncMock()+ with patch("backend.copilot.executor.utils.dispatch_turn", dispatch):+ response = await client.post(+ "/api/review/action",+ json={"reviews": [{"node_exec_id": answered, "approved": True}]},+ )++ assert response.status_code == 200+ assert response.json()["awaiting_other_cards"] is False+ dispatch.assert_awaited_once()+ row = await _row(waiting, test_user_id)+ assert row is not None and row.status == ReviewStatus.WAITING++[email protected](loop_scope="session")+async def test_a_chat_lists_its_answered_cards_until_their_calls_run(+ client: httpx.AsyncClient, setup_test_user: str, test_user_id: str+) -> None:+ """What lets the chat's box show an answer it waits on after a reload."""+ session = await _new_session(test_user_id)+ with (+ patch("backend.copilot.gate.is_feature_enabled", AsyncMock(return_value=True)),+ patch("backend.copilot.tools.get_tool", return_value=_Post()),+ ):+ answered, waiting = [await _hold(session, test_user_id, t) for t in "ab"]+ response = await client.post(+ "/api/review/action",+ json={+ "reviews": [{"node_exec_id": answered, "approved": True}],+ "wait_for_other_cards": True,+ },+ )+ assert response.json()["awaiting_other_cards"] is True++ listed = await client.get(f"/api/review/session/{session.session_id}/answered")+ pending = await client.get(f"/api/review/session/{session.session_id}")+ # The next turn runs the answered call, which takes it off the list.+ await _resolved(test_user_id, session)+ after = await client.get(f"/api/review/session/{session.session_id}/answered")++ assert [(r["node_exec_id"], r["status"]) for r in listed.json()] == [+ (answered, "APPROVED")+ ]+ assert [r["node_exec_id"] for r in pending.json()] == [waiting]+ assert after.json() == []diff --git a/autogpt_platform/backend/backend/api/features/graph_executions/review/routes.py b/autogpt_platform/backend/backend/api/features/graph_executions/review/routes.pyindex 12f2faf4d7..81245438db 100644--- a/autogpt_platform/backend/backend/api/features/graph_executions/review/routes.py+++ b/autogpt_platform/backend/backend/api/features/graph_executions/review/routes.py@@ -8,6 +8,7 @@ from prisma.enums import ReviewStatusfrom backend.copilot.constants import legacy_chat_session_id, parse_node_id_from_exec_idfrom backend.copilot.gate.chat_rules import set_answer_rules as set_chat_rules+from backend.copilot.gate.held import answered_cards as held_answered_cardsfrom backend.copilot.gate.held import subject_keys as held_subject_keysfrom backend.copilot.gate.held import wake as wake_for_held_callsfrom backend.copilot.gate.workflow import (@@ -192,6 +193,20 @@ async def list_pending_reviews_for_chat_session(return await get_pending_reviews_for_chat_session(chat_session_id, user_id)+ "/session/{chat_session_id}/answered",+ summary="Get Answered Reviews Waiting to Run for Chat Session",+ response_model=List[PendingHumanReviewModel],+)+async def list_answered_reviews_for_chat_session(+ chat_session_id: str,+ user_id: str = Security(autogpt_auth_lib.get_user_id),+) -> List[PendingHumanReviewModel]:+ """The chat's answered cards whose calls have not run yet, oldest first, so+ its box still shows them as answered, with Close and continue, after a reload."""+ return await held_answered_cards(user_id, chat_session_id)++@router.post("/action",response_model=ReviewResponse,@@ -388,6 +403,9 @@ async def process_review_action({review.node_exec_id: review.chat_rule_scope for review in request.reviews},)+ # A held call finishes on its own: an answer starts the chat's next turn, or+ # from the chat's own queue, its last answer does.+ awaiting_other_cards = Falseif chat_session_id is not None and updated_reviews:# A workflow step's card answers the step's own row; its run resumes# below, so the card never wakes the chat.@@ -404,11 +422,11 @@ async def process_review_action(await resume_reviewed_execution(user_id, run.graph_exec_id, run.graph_id, run.graph_version)- # A held call finishes on its own: the answer starts the chat's next turn.- await wake_for_held_calls(+ awaiting_other_cards = not await wake_for_held_calls(user_id,chat_session_id,[r for r in updated_reviews.values() if not is_step_card(r)],+ until_last=request.wait_for_other_cards,)if finished.failed:# Each card is kept, so answering it again finishes the job.@@ -444,9 +462,25 @@ async def process_review_action(rejected_count=rejected_count,failed_count=auto_approval_failed_count,error=error_message,+ awaiting_other_cards=awaiting_other_cards,)+ "/session/{chat_session_id}/continue",+ summary="Continue Chat With Answered Reviews",+ status_code=status.HTTP_204_NO_CONTENT,+ dependencies=[Depends(enforce_payment_paywall)],+)+async def continue_with_answered_reviews(+ chat_session_id: str,+ user_id: str = Security(autogpt_auth_lib.get_user_id),+) -> None:+ """Start the chat's turn with the cards answered so far; the rest stay open+ and the turn is told how many still wait."""+ await wake_for_held_calls(user_id, chat_session_id)++async def _running(user_id: str, graph_exec_id: str) -> bool:meta = await get_graph_execution_meta(user_id=user_id, execution_id=graph_exec_id)return meta is not None and meta.status == ExecutionStatus.RUNNINGdiff --git a/autogpt_platform/backend/backend/api/paywall_routes_test.py b/autogpt_platform/backend/backend/api/paywall_routes_test.pyindex fc28044764..a0be895574 100644--- a/autogpt_platform/backend/backend/api/paywall_routes_test.py+++ b/autogpt_platform/backend/backend/api/paywall_routes_test.py@@ -217,6 +217,7 @@ OPEN_WHILE_PAYWALLED = {"GET /api/review/execution/{graph_exec_id}","GET /api/review/pending","GET /api/review/session/{chat_session_id}",+ "GET /api/review/session/{chat_session_id}/answered","GET /api/schedules","GET /api/schedules/followups","GET /api/schedules/followups/paused",diff --git a/autogpt_platform/backend/backend/copilot/after_turn.py b/autogpt_platform/backend/backend/copilot/after_turn.pynew file mode 100644index 0000000000..9b1a4de35d--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/after_turn.py@@ -0,0 +1,42 @@+"""Work a turn leaves for after it has fully finished on its executor.++A turn that starts the chat's next turn from inside itself races its own+executor: the queue can hand the new turn to the pod still finishing this one,+which drops it as a duplicate. So the executor registers every turn it runs+here, work deferred while that turn runs is handed back to the executor's+done-callback (after the turn has left ``active_tasks`` and released its+lock), and work outside a registered turn, as in the API process, runs at once.++Process-local, like ``engine_switch``: a turn's end and its done-callback run+in the same executor process.+"""++import threading+from collections.abc import Awaitable, Callable++Work = Callable[[], Awaitable[object]]++_lock = threading.Lock()+_deferred: dict[str, list[Work]] = {}+++async def run_after_turn(turn_id: str, work: Work) -> None:+ """Run *work* once turn *turn_id* has fully finished here, or now if it+ does not run in this process."""+ with _lock:+ deferred = _deferred.get(turn_id)+ if deferred is not None:+ deferred.append(work)+ return+ await work()+++def turn_started(turn_id: str) -> None:+ with _lock:+ _deferred[turn_id] = []+++def turn_finished(turn_id: str) -> list[Work]:+ """The work *turn_id* deferred, for its done-callback to run."""+ with _lock:+ return _deferred.pop(turn_id, [])diff --git a/autogpt_platform/backend/backend/copilot/baseline/continuation_note_test.py b/autogpt_platform/backend/backend/copilot/baseline/continuation_note_test.pyindex 5ed1e2516b..bdb8e1305f 100644--- a/autogpt_platform/backend/backend/copilot/baseline/continuation_note_test.py+++ b/autogpt_platform/backend/backend/copilot/baseline/continuation_note_test.py@@ -11,7 +11,10 @@ from unittest.mock import AsyncMock, MagicMockimport pytestfrom backend.copilot.baseline import service-from backend.copilot.baseline.service_cancellation_test import baseline_io+from backend.copilot.baseline.service_cancellation_test import (+ _no_answered_cards,+ baseline_io,+)from backend.copilot.continuation import KEEP_GOING_NOTEfrom backend.copilot.model import ChatMessage, ChatSession@@ -70,7 +73,7 @@ async def _sent_messages(stream.close = AsyncMock()provider = AsyncMock(return_value=stream)monkeypatch.setattr(service, "call_provider_stream", provider)- monkeypatch.setattr(service, "resolve_answered", AsyncMock(return_value=[]))+ monkeypatch.setattr(service, "resolve_answered", _no_answered_cards)async with asyncio.timeout(5):async for _ in service.stream_chat_completion_baseline(diff --git a/autogpt_platform/backend/backend/copilot/baseline/service.py b/autogpt_platform/backend/backend/copilot/baseline/service.pyindex 83fac53788..5206b13c96 100644--- a/autogpt_platform/backend/backend/copilot/baseline/service.py+++ b/autogpt_platform/backend/backend/copilot/baseline/service.py@@ -83,6 +83,7 @@ from backend.copilot.pending_message_helpers import (drained_rows_entry,persist_pending_as_user_rows,persist_session_safe,+ persist_unfolded_rows,)from backend.copilot.pending_messages import (PendingMessage,@@ -225,6 +226,16 @@ def _pause_uncounted_box(return task+def _pause_counted_box(sandbox: Any, session_id: str, expert_id: str | None) -> None:+ """Pause the box at a counted turn's end, fire-and-forget; an expert's box+ stays running while another of its turns is active."""+ task = asyncio.create_task(+ pause_sandbox_direct(sandbox, session_id, expert_id=expert_id)+ )+ _background_tasks.add(task)+ task.add_done_callback(_background_tasks.discard)++# Hint appended on the last tool round so the model wraps up with a summary# instead of issuing another tool call that gets cut off cold. The shared# ``tool_call_loop`` drops ``tools`` on the last iteration (see util/tool_call_loop.py),@@ -2391,10 +2402,31 @@ async def stream_chat_completion_baseline(# After the execution context: an approved held call runs here, in this# turn's sandbox and tool bounds, and its result opens the turn.- held_results = await resolve_answered(user_id, session)- if held_results and await persist_pending_as_user_rows(- session, transcript_builder, held_results, log_prefix="[Baseline]"- ):+ unsaved_held: list[PendingMessage] = []+ held_results: list[PendingMessage] = []+ try:+ async for held_status in resolve_answered(+ user_id, session, unsaved_held.append+ ):+ yield held_status+ held_results = [*unsaved_held]+ # The helper appends the rows before its first await, so from here the+ # session carries them.+ unsaved_held.clear()+ folded = bool(held_results) and await persist_pending_as_user_rows(+ session, transcript_builder, held_results, log_prefix="[Baseline]"+ )+ except BaseException:+ # Stopped before the finally below: save what already ran, its approval+ # spent, and end the counted turn on the box.+ await persist_unfolded_rows(session, unsaved_held, log_prefix="[Baseline]")+ if held_results:+ # Cut inside the fold's own save: its rows are on the session only.+ await persist_session_safe(session, "[Baseline]")+ if e2b_sandbox is not None:+ _pause_counted_box(e2b_sandbox, session_id, session.expert_id)+ raise+ if folded:opening_entries.append(drained_rows_entry(held_results))openai_messages.extend(format_pending_as_user_message(pm) for pm in held_results@@ -2818,13 +2850,7 @@ async def stream_chat_completion_baseline(# cleanup below. An expert's box is left running while another of# its turns is still active.if e2b_sandbox is not None:- pause_task = asyncio.create_task(- pause_sandbox_direct(- e2b_sandbox, session_id, expert_id=session.expert_id- )- )- _background_tasks.add(pause_task)- pause_task.add_done_callback(_background_tasks.discard)+ _pause_counted_box(e2b_sandbox, session_id, session.expert_id)# Pending messages are drained atomically at turn start and# between tool rounds, so there's nothing to clear in finally.diff --git a/autogpt_platform/backend/backend/copilot/baseline/service_cancellation_test.py b/autogpt_platform/backend/backend/copilot/baseline/service_cancellation_test.pyindex fe9468b55c..2e1cf3a02b 100644--- a/autogpt_platform/backend/backend/copilot/baseline/service_cancellation_test.py+++ b/autogpt_platform/backend/backend/copilot/baseline/service_cancellation_test.py@@ -1,8 +1,9 @@"""Saved display names survive cancellation and stay out of provider requests."""import asyncio-from collections.abc import Iterator+from collections.abc import AsyncGenerator, Iteratorfrom contextlib import suppress+from typing import Anyfrom unittest.mock import AsyncMock, MagicMockimport pytest@@ -156,7 +157,6 @@ def baseline_io(monkeypatch: pytest.MonkeyPatch) -> Iterator[list[ChatSession]]:for name, value in {"drain_pending_safe": [],"drain_pending_messages": [],- "resolve_answered": [],"resolve_model_route": ResolvedModel(model="anthropic/claude-sonnet-4-6", source="env"),@@ -166,6 +166,7 @@ def baseline_io(monkeypatch: pytest.MonkeyPatch) -> Iterator[list[ChatSession]]:"persist_and_record_usage": None,}.items():monkeypatch.setattr(service, name, AsyncMock(return_value=value))+ monkeypatch.setattr(service, "resolve_answered", _no_answered_cards)monkeypatch.setattr(service, "_get_main_client", MagicMock())monkeypatch.setattr(service, "upsert_chat_session", AsyncMock(side_effect=save))try:@@ -280,3 +281,8 @@ def _tool_call_stream() -> MagicMock:stream.__aiter__.return_value = [chunk]stream.close = AsyncMock()return stream+++async def _no_answered_cards(*_: Any, **__: Any) -> AsyncGenerator[Any, None]:+ return+ yielddiff --git a/autogpt_platform/backend/backend/copilot/billing_e2e/baseline_turns.py b/autogpt_platform/backend/backend/copilot/billing_e2e/baseline_turns.pyindex c3deee5f86..15469f5226 100644--- a/autogpt_platform/backend/backend/copilot/billing_e2e/baseline_turns.py+++ b/autogpt_platform/backend/backend/copilot/billing_e2e/baseline_turns.py@@ -7,6 +7,7 @@ copilot's tools, which answer the provider's tool call with a canned result."""import uuid+from collections.abc import AsyncGeneratorfrom typing import Anyfrom unittest.mock import AsyncMock@@ -56,7 +57,7 @@ class BaselineTurns:"build_skills_update_notice": AsyncMock(return_value=None),"inject_user_context": AsyncMock(return_value=None),"clear_pending_question": AsyncMock(),- "resolve_answered": AsyncMock(return_value=[]),+ "resolve_answered": _no_answered_cards,"build_turn_budget_block": AsyncMock(return_value=""),"is_enabled_for_user": AsyncMock(return_value=False),"is_feature_enabled": AsyncMock(return_value=False),@@ -85,3 +86,8 @@ async def _answer_tool(*, tool_call_id: str, **_: Any) -> StreamToolOutputAvailareturn StreamToolOutputAvailable(toolCallId=tool_call_id, toolName=TOOL_NAME, output="The fact is 42.")+++async def _no_answered_cards(*_: Any, **__: Any) -> AsyncGenerator[Any, None]:+ return+ yielddiff --git a/autogpt_platform/backend/backend/copilot/billing_e2e/sdk_turns.py b/autogpt_platform/backend/backend/copilot/billing_e2e/sdk_turns.pyindex 2954e8bfa0..0069ed5cfc 100644--- a/autogpt_platform/backend/backend/copilot/billing_e2e/sdk_turns.py+++ b/autogpt_platform/backend/backend/copilot/billing_e2e/sdk_turns.py@@ -11,6 +11,7 @@ import base64import jsonimport shutilimport uuid+from collections.abc import AsyncGeneratorfrom contextlib import ExitStackfrom pathlib import Pathfrom typing import Any@@ -152,7 +153,7 @@ class SdkTurns:"gate_auto_continue": AsyncMock(return_value=AutoContinueGate(proceed=True)),- "resolve_answered": AsyncMock(return_value=[]),+ "resolve_answered": _no_answered_cards,"clear_pending_question": AsyncMock(),}@@ -253,3 +254,8 @@ def _codex_credentials() -> OAuth2Credentials:return credentials_from_bundle(bundle).model_copy(update={"id": CODEX_CREDENTIAL_ID})+++async def _no_answered_cards(*_: Any, **__: Any) -> AsyncGenerator[Any, None]:+ return+ yielddiff --git a/autogpt_platform/backend/backend/copilot/bot/card_buttons_db_test.py b/autogpt_platform/backend/backend/copilot/bot/card_buttons_db_test.pyindex f2a7c043df..764704c1a5 100644--- a/autogpt_platform/backend/backend/copilot/bot/card_buttons_db_test.py+++ b/autogpt_platform/backend/backend/copilot/bot/card_buttons_db_test.py@@ -17,7 +17,7 @@ import pytestfrom prisma.enums import ReviewStatusfrom prisma.models import PendingHumanReview, PlatformLink-from backend.copilot import stream_registry+from backend.copilot import stream_registry, woken_turnsfrom backend.copilot.active_turns import acquire_turn_slotfrom backend.copilot.bot import sessions as bot_sessionsfrom backend.copilot.bot.adapters.discord import choice_ui as discord_ui@@ -41,6 +41,7 @@ from backend.copilot.model import (update_session_autopilot_mode,upsert_chat_session,)+from backend.copilot.pending_messages import PendingMessagefrom backend.copilot.response_model import StreamCheckpoint, StreamTextDeltafrom backend.copilot.tools.base import BaseToolfrom backend.copilot.tools.models import ResponseType, ToolResponseBase@@ -331,6 +332,16 @@ def dispatched():yield dispatch[email protected](autouse=True)+def delayed_wake():+ """A wake a channel answer puts off goes no further than the scheduler."""+ client = MagicMock(schedule_held_wake=AsyncMock())+ with patch(+ "backend.copilot.gate.channel.get_scheduler_client", return_value=client+ ):+ yield client.schedule_held_wake++@pytest.fixturedef gate_on():with patch("backend.copilot.gate.is_feature_enabled", AsyncMock(return_value=True)):@@ -466,7 +477,10 @@ async def test_the_owners_approve_runs_the_call_in_the_channels_next_turn(assert ctx.follow is not None and ctx.follow.session_id == session.session_idassert ctx.user_id == linked.owner# That turn folds the answered card in, and the call runs as it was held.- [result] = await held.resolve_answered(test_user_id, session)+ results: list[PendingMessage] = []+ async for _update in held.resolve_answered(test_user_id, session, results.append):+ pass+ [result] = resultsassert post_tool.runs == [{"text": "hello"}]assert result.metadata["held_call"]["outcome"] == "approved"@@ -890,6 +904,68 @@ async def test_cards_answered_mid_reply_are_carried_once_when_that_turn_ends(assert said == ["Posted both."][email protected](loop_scope="session")+async def test_a_burst_of_clicks_starts_one_turn_after_the_last(+ one_linked: _Channel, test_user_id, gate_on, dispatched, delayed_wake+):+ """Each click moves the chat's one delayed wake, which then carries every+ answer so far and tells the turn what still waits."""+ session, first_id, first = await _held_card(one_linked, test_user_id)+ second = await _another_card(one_linked, test_user_id, session)+ await _another_card(one_linked, test_user_id, session, "third")++ for card in (first, second):+ await one_linked.click(card, one_linked.owner)++ assert await _status(first_id, test_user_id) == ReviewStatus.APPROVED+ assert "✅ Post a message · Approved" in one_linked.shown()+ dispatched.assert_not_awaited()+ assert [c.args for c in delayed_wake.await_args_list] == [+ (test_user_id, session.session_id, 3)+ ] * 2++ # What the scheduler runs when the delay is up.+ await held.wake(test_user_id, session.session_id)++ dispatched.assert_awaited_once()+ assert dispatched.await_args.kwargs["message"].endswith(+ "1 more still waits for my answer."+ )+ turn_id = dispatched.await_args.kwargs["turn_id"]+ assert await woken_turns.turn_for(session.session_id, first_id) == turn_id++[email protected](loop_scope="session")+async def test_the_click_on_a_chats_last_card_starts_its_turn_at_once(+ one_linked: _Channel, test_user_id, gate_on, dispatched, delayed_wake+):+ session, first_id, first = await _held_card(one_linked, test_user_id)+ second = await _another_card(one_linked, test_user_id, session)+ await one_linked.click(first, one_linked.owner)+ delayed_wake.assert_awaited_once()++ await one_linked.click(second, one_linked.owner)++ delayed_wake.assert_awaited_once()+ dispatched.assert_awaited_once()+ assert dispatched.await_args.kwargs["message"] == held.WAKE_MESSAGE+ turn_id = dispatched.await_args.kwargs["turn_id"]+ assert await woken_turns.turn_for(session.session_id, first_id) == turn_id++[email protected](loop_scope="session")+async def test_a_click_whose_wake_cannot_be_delayed_starts_it_now(+ one_linked: _Channel, test_user_id, gate_on, dispatched, delayed_wake+):+ session, _, first = await _held_card(one_linked, test_user_id)+ await _another_card(one_linked, test_user_id, session)+ delayed_wake.side_effect = ConnectionError("scheduler down")++ await one_linked.click(first, one_linked.owner)++ dispatched.assert_awaited_once()++async def _bot_following(channel: _Channel, session: ChatSession, executor: _Executor, dispatched) -> _InProcessBot:@@ -906,9 +982,9 @@ async def _bot_following(async def _another_card(- channel: _Channel, user_id: str, session: ChatSession+ channel: _Channel, user_id: str, session: ChatSession, text: str = "again") -> ChannelCard:- decision = await _hold(user_id, session, "again", "call-2")+ decision = await _hold(user_id, session, text, f"call-{text}")card = await cards.open_card(Platform(channel.platform.upper()),channel.server_id,diff --git a/autogpt_platform/backend/backend/copilot/executor/manager.py b/autogpt_platform/backend/backend/copilot/executor/manager.pyindex 754f0859b3..0065a9dab5 100644--- a/autogpt_platform/backend/backend/copilot/executor/manager.py+++ b/autogpt_platform/backend/backend/copilot/executor/manager.py@@ -16,10 +16,11 @@ from pika.adapters.blocking_connection import BlockingChannelfrom pika.exceptions import AMQPChannelError, AMQPConnectionErrorfrom pika.spec import Basic, BasicPropertiesfrom prometheus_client import Gauge, start_http_server+from pydantic import BaseModel, ValidationErrorimport backend.data.llm_registryfrom backend.blocks import get_blocks-from backend.copilot import engine_switch+from backend.copilot import after_turn, engine_switch, stream_registryfrom backend.copilot.executor.utils import schedule_turnfrom backend.data import redis_client as redisfrom backend.data.rabbitmq import SyncRabbitMQ@@ -94,6 +95,7 @@ class CoPilotExecutor(AppProcess):super().__init__()self.pool_size = settings.config.num_copilot_workersself.active_tasks: dict[str, tuple[Future, threading.Event]] = {}+ self._active_turn_ids: dict[str, str] = {}self.executor_id = str(uuid.uuid4())self._executor = None@@ -443,6 +445,11 @@ class CoPilotExecutor(AppProcess):except Exception as e:logger.error(f"Could not parse run message: {e}, body={body}")ack_message(reject=True, requeue=False)+ try:+ ids = _TurnIds.model_validate_json(body)+ except ValidationError:+ return+ _close_rejected_turn(ids.session_id, ids.turn_id)returnsession_id = entry.session_id@@ -453,6 +460,10 @@ class CoPilotExecutor(AppProcess):f"Session {session_id} already running locally, rejecting duplicate")ack_message(reject=True, requeue=False)+ # A redelivery of the running turn is left to it; any other turn+ # would read as running forever.+ if self._active_turn_ids.get(session_id) != entry.turn_id:+ _close_rejected_turn(session_id, entry.turn_id)return# Try to acquire cluster-wide lock@@ -464,6 +475,8 @@ class CoPilotExecutor(AppProcess):)current_owner = cluster_lock.try_acquire()if current_owner != self.executor_id:+ # Only the holder's own turn can meet its lock: a turn's end frees+ # the lock before a later turn of the chat can be admitted.if current_owner is not None:logger.warning(f"Session {session_id} already running on pod {current_owner}"@@ -485,12 +498,15 @@ class CoPilotExecutor(AppProcess):self._task_locks[session_id] = cluster_lockcancel_event = threading.Event()+ after_turn.turn_started(entry.turn_id)future = self.executor.submit(execute_copilot_turn, entry, cancel_event, cluster_lock)+ self._active_turn_ids[session_id] = entry.turn_idself.active_tasks[session_id] = (future, cancel_event)except Exception as e:logger.warning(f"Failed to setup execution for {session_id}: {e}")+ after_turn.turn_finished(entry.turn_id)cluster_lock.release()if session_id in self._task_locks:del self._task_locks[session_id]@@ -520,7 +536,7 @@ class CoPilotExecutor(AppProcess):self._task_locks[session_id].release()del self._task_locks[session_id]self._cleanup_completed_tasks()- _maybe_dispatch_engine_switch(session_id, error_msg)+ _dispatch_after_turn(session_id, entry.turn_id, error_msg)future.add_done_callback(on_run_done)@@ -534,6 +550,7 @@ class CoPilotExecutor(AppProcess):if future.done():completed_tasks.append(session_id)self.active_tasks.pop(session_id, None)+ self._active_turn_ids.pop(session_id, None)logger.info(f"Cleaned up completed session {session_id}")self._update_metrics()@@ -623,19 +640,17 @@ class CoPilotExecutor(AppProcess):return self._run_client-def _maybe_dispatch_engine_switch(session_id: str, error_msg: str | None) -> None:- """Consume a pending engine switch after the turn fully finished.+def _dispatch_after_turn(session_id: str, turn_id: str, error_msg: str | None) -> None:+ """Start what a turn left for after it: an engine-switch continuation, then+ the work it deferred (``after_turn``), such as the held-call wake.- Called from the turn future's done-callback: dispatch the SDK- continuation only NOW — the turn thread has fully finished (fail-close- ran, message acked, cluster lock released, active_tasks cleaned), so- neither the fail-close CAS nor the RMQ same-session duplicate rejection- can kill the new turn.+ Called from the turn future's done-callback: the turn thread has fully+ finished (fail-close ran, message acked, cluster lock released,+ active_tasks cleaned), so neither the fail-close CAS nor the RMQ+ same-session duplicate rejection can kill a turn started here."""switch = engine_switch.pop_switch(session_id)- if switch is None:- return- if error_msg is not None:+ if switch is not None and error_msg is not None:# Turn failed — the persisted history (and thus the derived# building-mode signal) can't be trusted, so no continuation fires.# Not silent: the session is idle and the user's next message@@ -644,62 +659,71 @@ def _maybe_dispatch_engine_switch(session_id: str, error_msg: str | None) -> Nonf"Engine-switch continuation for {session_id} not "f"dispatched — turn ended with error: {error_msg}")+ switch = None+ deferred = after_turn.turn_finished(turn_id)+ if switch is None and not deferred:return- # Dispatch on a dedicated short-lived thread: the async RabbitMQ client- # is @thread_cached and loop-bound, so asyncio.run() on this pool thread- # would reuse a client created under a previous (now-closed) event loop.- # A fresh thread gets a fresh loop AND a fresh thread-cached client, so- # nothing crosses loops.+ # One short-lived thread, one event loop: the async RabbitMQ client is+ # @thread_cached and loop-bound, so a pool thread or a second loop on+ # the same thread would reuse a client bound to a closed loop.threading.Thread(- target=_dispatch_engine_switch_continuation,- args=(session_id, switch),- name=f"engine-switch-{session_id[:8]}",+ target=lambda: asyncio.run(_after_turn(session_id, switch, deferred)),+ name=f"after-turn-{session_id[:8]}",daemon=True,).start()+async def _after_turn(+ session_id: str,+ switch: engine_switch.SwitchRequest | None,+ deferred: list[after_turn.Work],+) -> None:+ # The continuation first: a wake after it finds the chat running and+ # leaves its answered cards to that turn's start.+ if switch is not None:+ await _dispatch_engine_switch_continuation(session_id, switch)+ for work in deferred:+ try:+ await work()+ except Exception as work_err:+ logger.error(f"After-turn work for {session_id} failed: {work_err}")++_SWITCH_DISPATCH_ATTEMPTS = 3-def _dispatch_engine_switch_continuation(+async def _dispatch_engine_switch_continuation(session_id: str, switch: engine_switch.SwitchRequest) -> None:"""Dispatch the SDK continuation turn after an engine switch.- Runs on its own thread with its own event loop so the thread-cached- async queue client is created fresh and never reused across loops.-Best-effort with bounded retry (transient queue/redis blips): if allattempts fail, the session is left idle (not stranded) with auser-visible marker — building mode is derived from persisted messagehistory, so the user's next message still lands on the SDK engine withthe guide in the prefix."""-- async def dispatch() -> None:- from backend.copilot.model import get_chat_session-- session = await get_chat_session(session_id, switch.user_id)- if session is None:- raise RuntimeError("copilot_session_not_found")- await schedule_turn(- session_id=session_id,- user_id=switch.user_id,- turn_id=str(uuid.uuid4()),- message=engine_switch.CONTINUATION_MESSAGE,- is_user_message=False,- # No engine is named here: the processor pins a building-mode- # session to the SDK from its message history, which is what made- # this argument redundant even while it existed.- organization_id=switch.organization_id,- team_id=switch.team_id,- llm_auth_provider=session.metadata.llm_auth_provider,- llm_credential_id=session.metadata.llm_credential_id,- )+ from backend.copilot.model import get_chat_sessionfor attempt in range(1, _SWITCH_DISPATCH_ATTEMPTS + 1):try:- asyncio.run(dispatch())+ session = await get_chat_session(session_id, switch.user_id)+ if session is None:+ raise RuntimeError("copilot_session_not_found")+ await schedule_turn(+ session_id=session_id,+ user_id=switch.user_id,+ turn_id=str(uuid.uuid4()),+ message=engine_switch.CONTINUATION_MESSAGE,+ is_user_message=False,+ # No engine is named here: the processor pins a building-mode+ # session to the SDK from its message history, which is what+ # made this argument redundant even while it existed.+ organization_id=switch.organization_id,+ team_id=switch.team_id,+ llm_auth_provider=session.metadata.llm_auth_provider,+ llm_credential_id=session.metadata.llm_credential_id,+ )logger.info(f"Dispatched engine-switch continuation for {session_id}")returnexcept Exception as switch_err:@@ -708,11 +732,11 @@ def _dispatch_engine_switch_continuation(f"(attempt {attempt}/{_SWITCH_DISPATCH_ATTEMPTS}): {switch_err}")if attempt < _SWITCH_DISPATCH_ATTEMPTS:- time.sleep(attempt)- _persist_switch_failure_marker(session_id)+ await asyncio.sleep(attempt)+ await _persist_switch_failure_marker(session_id)-def _persist_switch_failure_marker(session_id: str) -> None:+async def _persist_switch_failure_marker(session_id: str) -> None:"""Leave a user-visible error in the session when every dispatch attemptfailed — the user was told building continues automatically, so a silentdrop would strand them on that promise. Best-effort: building mode@@ -723,21 +747,49 @@ def _persist_switch_failure_marker(session_id: str) -> None:from backend.copilot.model import ChatMessage, append_and_save_messagetry:- asyncio.run(- append_and_save_message(- session_id,- ChatMessage(- role="assistant",- content=(- f"{COPILOT_ERROR_PREFIX} Could not start the "- "agent-building engine automatically. Building mode "- "is still active — send a message to continue."- ),+ await append_and_save_message(+ session_id,+ ChatMessage(+ role="assistant",+ content=(+ f"{COPILOT_ERROR_PREFIX} Could not start the "+ "agent-building engine automatically. Building mode "+ "is still active — send a message to continue."),- )+ ),)except Exception as marker_err:logger.error(f"Failed to persist engine-switch failure marker for "f"{session_id}: {marker_err}")+++_REJECTED_TURN_MESSAGE = "This reply could not start. Please try again."+++class _TurnIds(BaseModel):+ session_id: str+ turn_id: str+++def _close_rejected_turn(session_id: str, turn_id: str) -> None:+ """Close a turn this executor dropped unrun, so the chat stops reading as+ running it; the turn guard leaves a chat a later turn owns alone."""+ # Without its id there is no guard, and the close would end the running turn.+ if not turn_id:+ return+ threading.Thread(+ target=lambda: asyncio.run(_close_turn(session_id, turn_id)),+ name=f"close-rejected-{session_id[:8]}",+ daemon=True,+ ).start()+++async def _close_turn(session_id: str, turn_id: str) -> None:+ try:+ await stream_registry.mark_session_completed(+ session_id, error_message=_REJECTED_TURN_MESSAGE, turn_id=turn_id+ )+ except Exception as close_err:+ logger.error(f"Could not close rejected turn {turn_id}: {close_err}")diff --git a/autogpt_platform/backend/backend/copilot/executor/manager_test.py b/autogpt_platform/backend/backend/copilot/executor/manager_test.pyindex 515a0aa684..c53d29b931 100644--- a/autogpt_platform/backend/backend/copilot/executor/manager_test.py+++ b/autogpt_platform/backend/backend/copilot/executor/manager_test.py@@ -1,28 +1,26 @@-"""Tests for the copilot manager: the engine-switch dispatch and the cancel consumer.+"""Tests for the copilot manager: what runs after a turn, and the cancel consumer.-The dispatch is the handoff between a finished baseline turn and the-server-initiated SDK continuation turn — the highest-risk link in the-engine-switch flow (see ``backend.copilot.engine_switch``). These tests-pin its retry/give-up contract.+The engine-switch dispatch is the handoff between a finished baseline turn and+the server-initiated SDK continuation turn — the highest-risk link in the+engine-switch flow (see ``backend.copilot.engine_switch``). These tests pin its+retry/give-up contract, and the order it keeps with a turn's deferred work."""import threading-import time-from concurrent.futures import ThreadPoolExecutor-from time import sleep as original_sleepfrom types import SimpleNamespacefrom unittest.mock import AsyncMock, MagicMock, call, patchimport pytest+from backend.copilot import after_turnfrom backend.copilot.engine_switch import CONTINUATION_MESSAGE, SwitchRequestfrom backend.copilot.executor.utils import COPILOT_CANCEL_QUEUE_PREFIXfrom .manager import (_SWITCH_DISPATCH_ATTEMPTS,CoPilotExecutor,+ _dispatch_after_turn,_dispatch_engine_switch_continuation,- _maybe_dispatch_engine_switch,_persist_switch_failure_marker,)@@ -45,13 +43,13 @@ def mock_chat_session():yield mock_get_session-def test_dispatch_succeeds_first_try():+async def test_dispatch_succeeds_first_try():with (patch("backend.copilot.executor.manager.schedule_turn", new_callable=AsyncMock) as mock_schedule,):- _dispatch_engine_switch_continuation("sess-1", _SWITCH)+ await _dispatch_engine_switch_continuation("sess-1", _SWITCH)assert mock_schedule.await_count == 1kwargs = mock_schedule.call_args.kwargs@@ -63,7 +61,7 @@ def test_dispatch_succeeds_first_try():assert "mode" not in kwargs-def test_dispatch_preserves_codex_transport_for_engine_switch(+async def test_dispatch_preserves_codex_transport_for_engine_switch(mock_chat_session,):mock_chat_session.return_value.metadata.llm_auth_provider = "codex"@@ -72,7 +70,7 @@ def test_dispatch_preserves_codex_transport_for_engine_switch("backend.copilot.executor.manager.schedule_turn",new_callable=AsyncMock,) as mock_schedule:- _dispatch_engine_switch_continuation("sess-1", _SWITCH)+ await _dispatch_engine_switch_continuation("sess-1", _SWITCH)kwargs = mock_schedule.call_args.kwargsassert "mode" not in kwargs@@ -82,27 +80,14 @@ def test_dispatch_preserves_codex_transport_for_engine_switch(@pytest.fixturedef mock_dispatch_sleep():- mock_sleep = MagicMock()+ mock_sleep = AsyncMock()with patch(- "backend.copilot.executor.manager.time", SimpleNamespace(sleep=mock_sleep)+ "backend.copilot.executor.manager.asyncio", SimpleNamespace(sleep=mock_sleep)):yield mock_sleep-def test_dispatch_sleep_mock_does_not_affect_other_threads(mock_dispatch_sleep):- def sleep_in_other_thread():- sleeper = time.sleep- sleeper(0)- return sleeper-- with ThreadPoolExecutor(max_workers=1) as executor:- sleeper = executor.submit(sleep_in_other_thread).result(timeout=5)-- assert sleeper is original_sleep- mock_dispatch_sleep.assert_not_called()---def test_dispatch_retries_until_success(mock_dispatch_sleep):+async def test_dispatch_retries_until_success(mock_dispatch_sleep):with (patch("backend.copilot.executor.manager.schedule_turn",@@ -110,13 +95,13 @@ def test_dispatch_retries_until_success(mock_dispatch_sleep):side_effect=[RuntimeError("rmq down"), RuntimeError("rmq down"), None],) as mock_schedule,):- _dispatch_engine_switch_continuation("sess-1", _SWITCH)+ await _dispatch_engine_switch_continuation("sess-1", _SWITCH)assert mock_schedule.await_count == 3- assert mock_dispatch_sleep.call_args_list == [call(1), call(2)]+ assert mock_dispatch_sleep.await_args_list == [call(1), call(2)]-def test_dispatch_gives_up_after_bounded_attempts_with_user_visible_marker(+async def test_dispatch_gives_up_after_bounded_attempts_with_user_visible_marker(mock_dispatch_sleep,):with (@@ -126,33 +111,35 @@ def test_dispatch_gives_up_after_bounded_attempts_with_user_visible_marker(side_effect=RuntimeError("rmq down"),) as mock_schedule,patch(- "backend.copilot.executor.manager._persist_switch_failure_marker"+ "backend.copilot.executor.manager._persist_switch_failure_marker",+ new_callable=AsyncMock,) as mock_marker,):- _dispatch_engine_switch_continuation("sess-1", _SWITCH)+ await _dispatch_engine_switch_continuation("sess-1", _SWITCH)assert mock_schedule.await_count == _SWITCH_DISPATCH_ATTEMPTS- assert mock_dispatch_sleep.call_args_list == [call(1), call(2)]- mock_marker.assert_called_once_with("sess-1")+ assert mock_dispatch_sleep.await_args_list == [call(1), call(2)]+ mock_marker.assert_awaited_once_with("sess-1")-def test_no_failure_marker_on_success():+async def test_no_failure_marker_on_success():with (patch("backend.copilot.executor.manager.schedule_turn", new_callable=AsyncMock),patch(- "backend.copilot.executor.manager._persist_switch_failure_marker"+ "backend.copilot.executor.manager._persist_switch_failure_marker",+ new_callable=AsyncMock,) as mock_marker,):- _dispatch_engine_switch_continuation("sess-1", _SWITCH)+ await _dispatch_engine_switch_continuation("sess-1", _SWITCH)- mock_marker.assert_not_called()+ mock_marker.assert_not_awaited()-def test_failure_marker_persists_error_row():+async def test_failure_marker_persists_error_row():with patch("backend.copilot.model.append_and_save_message", new_callable=AsyncMock) as mock_append:- _persist_switch_failure_marker("sess-1")+ await _persist_switch_failure_marker("sess-1")assert mock_append.await_count == 1session_id, message = mock_append.call_args.args@@ -161,21 +148,31 @@ def test_failure_marker_persists_error_row():assert "Could not start the agent-building engine" in message.content-def test_turn_done_with_pending_switch_dispatches_on_success():+async def test_a_finished_turn_starts_its_continuation_before_its_deferred_work():+ """The wake must find the continuation's turn running and leave its cards to+ it; the other order starts two turns for one chat."""+ order: list[str] = []+ after_turn.turn_started("turn-1")+ await after_turn.run_after_turn("turn-1", _recording(order, "wake"))+ threads = _RecordedThreads()+with (patch("backend.copilot.executor.manager.engine_switch.pop_switch",return_value=_SWITCH,),- patch("backend.copilot.executor.manager.threading.Thread") as mock_thread,+ patch(+ "backend.copilot.executor.manager._dispatch_engine_switch_continuation",+ new=AsyncMock(side_effect=lambda *_: order.append("continuation")),+ ),+ patch("backend.copilot.executor.manager.threading.Thread", new=threads),):- _maybe_dispatch_engine_switch("sess-1", error_msg=None)+ assert order == []+ _dispatch_after_turn("sess-1", "turn-1", error_msg=None)+ threads.join()- mock_thread.assert_called_once()- kwargs = mock_thread.call_args.kwargs- assert kwargs["target"] is _dispatch_engine_switch_continuation- assert kwargs["args"] == ("sess-1", _SWITCH)- mock_thread.return_value.start.assert_called_once()+ assert order == ["continuation", "wake"]+ assert after_turn.turn_finished("turn-1") == []def test_turn_done_with_error_skips_dispatch_but_consumes_switch():@@ -186,13 +183,13 @@ def test_turn_done_with_error_skips_dispatch_but_consumes_switch():) as mock_pop,patch("backend.copilot.executor.manager.threading.Thread") as mock_thread,):- _maybe_dispatch_engine_switch("sess-1", error_msg="boom")+ _dispatch_after_turn("sess-1", "turn-2", error_msg="boom")mock_pop.assert_called_once_with("sess-1")mock_thread.assert_not_called()-def test_turn_done_without_switch_is_noop():+def test_turn_done_without_switch_or_deferred_work_is_noop():with (patch("backend.copilot.executor.manager.engine_switch.pop_switch",@@ -200,7 +197,7 @@ def test_turn_done_without_switch_is_noop():),patch("backend.copilot.executor.manager.threading.Thread") as mock_thread,):- _maybe_dispatch_engine_switch("sess-1", error_msg=None)+ _dispatch_after_turn("sess-1", "turn-3", error_msg=None)mock_thread.assert_not_called()@@ -279,3 +276,29 @@ def test_cancel_consumer_consumes_the_queue_it_just_declared():declared = channel.queue_declare.call_args.kwargs["queue"]assert declared.startswith(COPILOT_CANCEL_QUEUE_PREFIX)assert channel.basic_consume.call_args.kwargs["queue"] == declared+++def _recording(order: list[str], name: str) -> after_turn.Work:+ async def work() -> None:+ order.append(name)++ return work+++class _RecordedThreads:+ """Starts real threads, so ``asyncio.run`` never touches the test's loop,+ and joins them."""++ def __init__(self) -> None:+ self.started: list[threading.Thread] = []+ self._thread = threading.Thread++ def __call__(self, *args, **kwargs) -> threading.Thread:+ thread = self._thread(*args, **kwargs)+ self.started.append(thread)+ return thread++ def join(self) -> None:+ for thread in self.started:+ thread.join(timeout=10)+ assert not thread.is_alive()diff --git a/autogpt_platform/backend/backend/copilot/executor/turn_handoff_test.py b/autogpt_platform/backend/backend/copilot/executor/turn_handoff_test.pynew file mode 100644index 0000000000..e41c14dc25--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/executor/turn_handoff_test.py@@ -0,0 +1,296 @@+"""A chat's next turn, started by the one ending, on one executor end to end.++One real ``CoPilotExecutor`` whose queue hands every turn straight back to it,+which is the worst case and what happened on prod: the ending turn started the+approval follow-up while it still sat in ``active_tasks``, the executor dropped+it as a duplicate, and the chat read as running a turn nobody ran. A script+stands in for the engine; the wake, the dispatch, the executor's intake and+done-callback, and the approved call's run are the real ones. Every coroutine+runs on the test's loop, where the Prisma client lives: the executor's threads+hand theirs over.+"""++import asyncio+import json+import threading+import uuid+from collections.abc import AsyncIterator, Awaitable, Callable+from concurrent.futures import ThreadPoolExecutor+from contextlib import asynccontextmanager+from types import SimpleNamespace+from typing import Any+from unittest.mock import AsyncMock, patch++import pytest+from prisma.enums import ReviewStatus++from backend.copilot import stream_registry+from backend.copilot.active_turns import acquire_turn_slot+from backend.copilot.executor.manager import CoPilotExecutor+from backend.copilot.executor.utils import CoPilotExecutionEntry, dispatch_turn+from backend.copilot.gate import held+from backend.copilot.gate.held_db_test import _answer, _hold, _new_session, _Post+from backend.copilot.model import get_chat_session+from backend.copilot.pending_messages import PendingMessage+from backend.data.db_accessors import chat_db+from backend.data.redis_client import get_redis_async+from backend.data.redis_helpers import as_str++_MANAGER = "backend.copilot.executor.manager"+++def gate_on():+ with patch("backend.copilot.gate.is_feature_enabled", AsyncMock(return_value=True)):+ yield+++def post_tool():+ tool = _Post()+ with patch("backend.copilot.tools.get_tool", return_value=tool):+ yield tool++[email protected](loop_scope="session")+async def test_cards_answered_during_a_turn_run_once_it_has_left_under_its_envelope(+ setup_test_user, test_user_id, gate_on, post_tool+):+ """Not a fresh root for the follow-up: the card runs under the tree of the+ turn that held it, and its result keeps the card's copy for the chain row."""+ session = await _new_session(test_user_id)+ delivered: list[PendingMessage] = []++ async def turn(entry: CoPilotExecutionEntry) -> None:+ chat = await get_chat_session(entry.session_id, entry.user_id)+ assert chat is not None+ async for _status in held.resolve_answered(+ test_user_id, chat, delivered.append+ ):+ pass+ if entry.message == "post it":+ review_id = await _hold(+ chat, test_user_id, "approved mid-turn", envelope=entry.envelope+ )+ await _answer(review_id, ReviewStatus.APPROVED)+ await stream_registry.mark_session_completed(+ entry.session_id, turn_id=entry.turn_id+ )++ async with _Pod.running(turn) as pod:+ await _start_turn(session.session_id, test_user_id, "post it")+ final = await _settled(pod, session.session_id, turns=2)++ assert pod.dropped == [], "the executor dropped the follow-up turn unrun"+ assert [t.message for t in pod.turns] == ["post it", held.WAKE_MESSAGE]+ assert post_tool.runs == [{"text": "approved mid-turn"}]+ assert (final.turn_id, final.status) == (pod.turns[1].turn_id, "completed")+ held_under, woken_under = (t.envelope for t in pod.turns)+ assert held_under is not None and woken_under is not None+ assert woken_under.tree_id == held_under.tree_id+ [result] = delivered+ assert result.metadata["held_call"]["arguments"] == {"text": "approved mid-turn"}++[email protected](loop_scope="session")+async def test_a_turn_dropped_unrun_ends_and_a_redelivered_running_one_does_not(+ setup_test_user, test_user_id+):+ """A second turn can arrive while the first is still closing on this pod."""+ session = await _new_session(test_user_id)+ finish, closing, tail = asyncio.Event(), asyncio.Event(), asyncio.Event()++ async def turn(entry: CoPilotExecutionEntry) -> None:+ await finish.wait()+ await stream_registry.mark_session_completed(+ entry.session_id, turn_id=entry.turn_id+ )+ closing.set()+ await tail.wait()++ async with _Pod.running(turn) as pod:+ first = await _start_turn(session.session_id, test_user_id, "first")+ pod.deliver(pod.bodies[0])+ await asyncio.sleep(1)+ while_running = await stream_registry.get_session(session.session_id)+ finish.set()+ await closing.wait()+ second = await _start_turn(session.session_id, test_user_id, "second")+ chunks = await _chunks_until_finish(second)+ tail.set()++ assert pod.dropped == [first, second]+ assert while_running is not None+ assert (while_running.turn_id, while_running.status) == (first, "running")+ assert [c["type"] for c in chunks] == ["error", "finish"]+ assert await stream_registry.get_active_session(+ session.session_id, test_user_id+ ) == (None, "0-0")+ info = await chat_db().get_chat_session_metadata(session.session_id)+ assert info is not None and info.chat_status == "idle"++[email protected](loop_scope="session")+async def test_a_turn_the_executor_cannot_read_still_ends():+ """A newer pod's message, say: its ids still name the turn to close."""+ session_id, turn_id = str(uuid.uuid4()), str(uuid.uuid4())+ await stream_registry.create_session(session_id, None, "", "", turn_id=turn_id)+ body = CoPilotExecutionEntry(+ session_id=session_id, turn_id=turn_id, user_id=None, message="hi"+ ).model_dump(mode="json") | {"llm_auth_provider": "from-a-newer-pod"}++ async with _Pod.running(_never_runs) as pod:+ pod.deliver(json.dumps(body).encode())+ chunks = await _chunks_until_finish(turn_id)++ assert pod.turns == []+ assert [c["type"] for c in chunks] == ["error", "finish"]+ closed = await stream_registry.get_session(session_id)+ assert closed is not None and closed.status == "failed"++[email protected](loop_scope="session")+async def test_a_message_naming_no_turn_closes_nothing():+ """With no turn id the close has no guard, so it would end the running turn."""+ session_id, running = str(uuid.uuid4()), str(uuid.uuid4())+ await stream_registry.create_session(session_id, None, "", "", turn_id=running)+ body = CoPilotExecutionEntry(+ session_id=session_id, turn_id="", user_id=None, message="hi"+ ).model_dump(mode="json") | {"llm_auth_provider": "from-a-newer-pod"}++ async with _Pod.running(_never_runs) as pod:+ pod.deliver(json.dumps(body).encode())++ assert pod.turns == []+ still = await stream_registry.get_session(session_id)+ assert still is not None and (still.turn_id, still.status) == (running, "running")+++class _Pod:+ """One executor whose queue delivers every turn published back to itself."""++ def __init__(self, script: Callable[[CoPilotExecutionEntry], Awaitable[None]]):+ self.loop = asyncio.get_running_loop()+ self.script = script+ self.bodies: list[bytes] = []+ self.turns: list[CoPilotExecutionEntry] = []+ self.dropped: list[str] = []+ self.executor = CoPilotExecutor()+ self.executor._executor = ThreadPoolExecutor(max_workers=2)+ self.channel = SimpleNamespace(+ is_open=True,+ connection=SimpleNamespace(add_callback_threadsafe=lambda cb: cb()),+ basic_ack=lambda _tag: None,+ basic_nack=self._nack,+ )++ @classmethod+ @asynccontextmanager+ async def running(+ cls, script: Callable[[CoPilotExecutionEntry], Awaitable[None]]+ ) -> AsyncIterator["_Pod"]:+ pod = cls(script)+ # The executor's own loops (after a turn, closing a dropped one) run+ # on this test's loop instead of a fresh one per thread.+ on_loop = SimpleNamespace(+ run=pod._on_loop,+ sleep=asyncio.sleep,+ CancelledError=asyncio.CancelledError,+ )+ with (+ patch(f"{_MANAGER}.execute_copilot_turn", pod._run_turn),+ patch(f"{_MANAGER}.asyncio", on_loop),+ patch(+ "backend.util.clients.get_async_copilot_queue",+ AsyncMock(return_value=pod),+ ),+ ):+ try:+ yield pod+ finally:+ await pod._idle()+ pod.executor._executor.shutdown(wait=False)++ def deliver(self, body: bytes) -> None:+ self.bodies.append(body)+ self.executor._handle_run_message(+ self.channel,+ SimpleNamespace(delivery_tag=len(self.bodies)),+ None,+ body,+ )++ async def publish_message(self, routing_key: str, message: str, exchange: Any):+ self.deliver(message.encode())++ def _run_turn(self, entry: CoPilotExecutionEntry, *_args: Any) -> None:+ self.turns.append(entry)+ asyncio.run_coroutine_threadsafe(self.script(entry), self.loop).result(60)++ def _on_loop(self, coro: Awaitable[None]) -> None:+ asyncio.run_coroutine_threadsafe(coro, self.loop).result(60)++ async def _idle(self) -> None:+ # The executor's own threads finish on this loop, so wait, never join.+ for _ in range(300):+ busy = [+ t+ for t in threading.enumerate()+ if t.name.startswith(("after-turn-", "close-rejected-"))+ ]+ if not busy and not self.executor.active_tasks:+ return+ await asyncio.sleep(0.1)++ def _nack(self, tag: int, requeue: bool) -> None:+ if not requeue:+ ids = json.loads(self.bodies[tag - 1])+ self.dropped.append(ids["turn_id"])+++async def _start_turn(session_id: str, user_id: str, message: str) -> str:+ turn_id = str(uuid.uuid4())+ async with acquire_turn_slot(user_id, session_id) as slot:+ await dispatch_turn(+ slot,+ session_id=session_id,+ user_id=user_id,+ turn_id=turn_id,+ message=message,+ )+ return turn_id+++async def _settled(+ pod: _Pod, session_id: str, turns: int+) -> stream_registry.ActiveSession:+ """The chat once its ``turns``-th turn ended, or once the pod dropped one."""+ meta = None+ for _ in range(300):+ meta = await stream_registry.get_session(session_id)+ if pod.dropped or (+ len(pod.turns) == turns+ and meta is not None+ and meta.turn_id == pod.turns[-1].turn_id+ and meta.status != "running"+ ):+ break+ await asyncio.sleep(0.1)+ assert meta is not None+ return meta+++async def _chunks_until_finish(turn_id: str) -> list[dict[str, Any]]:+ redis = await get_redis_async()+ chunks: list[dict[str, Any]] = []+ for _ in range(300):+ entries = await redis.xrange(stream_registry._get_turn_stream_key(turn_id))+ chunks = [json.loads(as_str(fields["data"]) or "") for _id, fields in entries]+ if chunks and chunks[-1]["type"] == "finish":+ break+ await asyncio.sleep(0.1)+ return chunks+++async def _never_runs(_entry: CoPilotExecutionEntry) -> None:+ raise AssertionError("an unreadable turn must not run")diff --git a/autogpt_platform/backend/backend/copilot/gate/channel.py b/autogpt_platform/backend/backend/copilot/gate/channel.pyindex 58a4ea3f3d..594c5aa9dc 100644--- a/autogpt_platform/backend/backend/copilot/gate/channel.py+++ b/autogpt_platform/backend/backend/copilot/gate/channel.py@@ -15,6 +15,7 @@ from pydantic import BaseModelfrom backend.api.features.graph_executions.review.model import PendingHumanReviewModelfrom backend.copilot.constants import AUTOPILOT_NAMEfrom backend.data.db_accessors import review_db+from backend.util.clients import get_scheduler_clientfrom . import chat_rules, heldfrom .review import payload_headline@@ -38,6 +39,7 @@ _RECEIPTS: dict[Choice, str] = {"reject": "Rejected",}_PASSAGE_CHARS = 500+_CONTINUE_AFTER_SECONDS = 3class CardOption(BaseModel):@@ -114,10 +116,23 @@ async def answer()except Exception:logger.warning(f"Rule from {review_id} not saved", exc_info=True)- await held.wake(user_id, session_id, answered.values())+ if not await held.wake(user_id, session_id, answered.values(), until_last=True):+ await _wake_soon(user_id, session_id)return "answered"+async def _wake_soon(user_id: str, session_id: str) -> None:+ """A channel has no Continue: its answers run a few seconds after the last+ one, or at once on the chat's last card."""+ try:+ await get_scheduler_client().schedule_held_wake(+ user_id, session_id, _CONTINUE_AFTER_SECONDS+ )+ except Exception:+ logger.warning(f"Could not delay the wake of {session_id}", exc_info=True)+ await held.wake(user_id, session_id)++def _offered(payload: dict[str, Any]) -> list[tuple[Choice, str, str]]:"""The web card's choices on this row: (choice, label, receipt)."""if _is_held_read(payload):diff --git a/autogpt_platform/backend/backend/copilot/gate/held.py b/autogpt_platform/backend/backend/copilot/gate/held.pyindex d76e41f289..fc9edb5eb7 100644--- a/autogpt_platform/backend/backend/copilot/gate/held.py+++ b/autogpt_platform/backend/backend/copilot/gate/held.py@@ -6,27 +6,33 @@ the model made it, and :func:`resolve_answered` runs it (or refuses it) at thestart of the chat's next turn, inside the engine, where the turn's sandbox andtool bounds exist. Its result reaches the model as a user row naming theoriginal call. :func:`wake` starts that turn when none is running; a running-turn's end wakes it instead (``stream_registry.mark_session_completed``).+turn's end wakes it instead, once that turn has left its executor+(``stream_registry.mark_session_completed``). An answer on an idle chat waits+for the chat's last card, so a burst of answers starts one turn."""import jsonimport loggingimport uuidfrom datetime import UTC, datetime-from typing import TYPE_CHECKING, Any, Callable, Iterable, Literal+from typing import TYPE_CHECKING, Any, AsyncIterator, Callable, Iterable, Literalfrom prisma.enums import ReviewStatusfrom pydantic import BaseModel, Field, ValidationErrorfrom backend.copilot import woken_turns-from backend.copilot.constants import COPILOT_NODE_EXEC_ID_SEPARATOR+from backend.copilot.constants import (+ COPILOT_NODE_EXEC_ID_SEPARATOR,+ COPILOT_STEP_CARD_PREFIX,+)from backend.copilot.google_data_redaction import (is_google_call,is_google_output,remember_google_output,)-from backend.copilot.model import ChatSession+from backend.copilot.model import CHAT_STATUS_RUNNING, ChatSessionfrom backend.copilot.pending_messages import PendingMessage+from backend.copilot.response_model import StreamStatusfrom backend.copilot.tree import TreeRefusal, TurnEnvelopefrom backend.copilot.trial_cost_context import TrialCostContext, restore_cost_contextfrom backend.data.db_accessors import chat_db, review_db@@ -35,6 +41,7 @@ from backend.util.encryption import JSONCryptorfrom . import chat_rulesfrom . import review as review_store+from .headline import headline_forfrom .policy import PARKABLE, effect_forif TYPE_CHECKING:@@ -79,6 +86,11 @@ class HeldResult(PendingMessage):content: str = Field(min_length=1, max_length=_MAX_RESULT_CHARS)+ @staticmethod+ def carried_by(data: dict[str, Any]) -> bool:+ """Whether a buffered message's data is a held call's result."""+ return _RESULT_KEY in (data.get("metadata") or {})+class HeldCall(BaseModel):review_id: str@@ -155,6 +167,22 @@ async def forget(session_id: str, review_id: str) -> None:async def answered(user_id: str, session_id: str) -> list[HeldCall]:"""Held calls whose card has been answered, oldest first."""held = await _held(session_id)+ return [+ held[review_id] for review_id, _ in await _answered(user_id, session_id, held)+ ]+++async def answered_cards(+ user_id: str, session_id: str+) -> "list[PendingHumanReviewModel]":+ """The answered cards whose held calls have not run yet, oldest first."""+ held = await _held(session_id)+ return [row for _, row in await _answered(user_id, session_id, held)]+++async def _answered(+ user_id: str, session_id: str, held: dict[str, HeldCall]+) -> "list[tuple[str, PendingHumanReviewModel]]":if not held:return []try:@@ -166,50 +194,70 @@ async def answered(user_id: str, session_id: str) -> list[HeldCall]:return []return sorted((- held[review_id]+ (review_id, row)for review_id, row in rows.items()if row.session_id == session_id and row.status != ReviewStatus.WAITING),- key=lambda call: call.held_at,+ key=lambda answered: held[answered[0]].held_at,)async def resolve_answered(user_id: str | None,session: ChatSession,+ deliver: Callable[[PendingMessage], None],cap: Callable[[str], str] = lambda text: text,-) -> list[PendingMessage]:- """Run every answered held call once and return its result as a user row.-- Call only once the turn's execution context is set. ``cap`` is the- engine's own last step on a direct tool result, so a late one reads the- same. The HDEL claims the call, so two turns racing over one card cannot- both run it; the gate's own consume stays the second lock behind it.+) -> AsyncIterator[StreamStatus]:+ """Run every answered held call once and ``deliver`` its result as a user row.++ Yields a status before an approved call runs, which the engine streams:+ nothing else reaches the chat while it does. Call only once the turn's+ execution context is set. ``cap`` is the engine's own last step on a+ direct tool result, so a late one reads the same. The HDEL claims the+ call, so two turns racing over one card cannot both run it; the gate's own+ consume stays the second lock behind it."""if not user_id:- return []- delivered: list[PendingMessage] = []+ returnfor call in await answered(user_id, session.session_id):+ try:+ rows = await review_db().get_reviews_by_node_exec_ids(+ [call.review_id], user_id+ )+ except Exception:+ logger.warning(f"Held call {call.review_id} unreadable", exc_info=True)+ continue+ row = rows.get(call.review_id)+ tool = _due(call, row, user_id)+ # Before the claim: a stream closed at this yield leaves the call held.+ if tool:+ yield StreamStatus(+ message=f"Running the action you approved: {_headline(call, row)}"+ )if not await _claim(session.session_id, call.review_id):continuetry:- delivered.append(await _deliver(user_id, session, call, cap))+ await _resolve(user_id, session, call, row, tool, deliver, cap)except Exception:logger.warning(f"Held call {call.review_id} not delivered", exc_info=True)- delivered.extend(await _recover(user_id, session.session_id, call))- return delivered+ for result in await _recover(user_id, session.session_id, call):+ deliver(result)async def wake(user_id: str,session_id: str,answered_rows: "Iterable[PendingHumanReviewModel]" = (),-) -> None:+ *,+ until_last: bool = False,+) -> bool:"""Start a turn to carry answered cards, if the chat is idle.Best effort: a running turn's end calls this again, and any later turnfolds the results in anyway. ``answered_rows`` are the cards justanswered; one whose held call is gone from Redis is restored from its row.+ ``until_last`` is an answer's wake: on an idle chat it starts nothing while+ another card still waits, and returns False."""# Deferred: the executor utilities import the tool registry.from backend.copilot.active_turns import (@@ -235,7 +283,7 @@ async def wake(await _restore(user_id, session_id, answered_rows)calls = await answered(user_id, session_id)if not calls:- return+ return True# One wake per set of answered cards: a turn that died before its fold# must not be woken again for the same cards, turn after failed turn.wake_id = str(@@ -246,7 +294,18 @@ async def wake()info = await chat_db().get_chat_session_metadata(session_id)if info is None or info.user_id != user_id:- return+ return True+ waiting = await _waiting(user_id, session_id)+ # A running turn's end carries the answers whatever still waits.+ if until_last and waiting and info.chat_status != CHAT_STATUS_RUNNING:+ logger.info(f"Held calls in {session_id} wait for {waiting} more answers")+ return False+ # A turn now would run over the question; the answer's turn runs these+ # cards first.+ if info.metadata.pending_question is not None:+ logger.info(f"Held calls in {session_id} wait for its open question")+ return True+ message = _wake_message(waiting)refusal_id = str(uuid.uuid5(uuid.NAMESPACE_URL, f"{wake_id}:refused"))latest = max(calls, key=lambda c: c.held_at)envelope = latest.envelope@@ -254,10 +313,10 @@ async def wake(# Deriving one from the turn that just ended would run the approved# action under that turn's limits, not the ones it was held under.await post_refusal(session_id, UNRECORDED_WAKE, message_id=refusal_id)- return+ return Trueif latest.cost_context is None or latest.cost_context.user_id != user_id:await post_refusal(session_id, _RESEND, message_id=refusal_id)- return+ return Truepermissions = resolve_session_permissions(info)metadata = {_WAKE_KEY: True}capacity = get_delegated_turn_limit() if wakes_sub_work(info) else None@@ -268,27 +327,27 @@ async def wake() as slot:# Not admitted: a turn is already running, and its end wakes us.if not slot.admitted:- return+ return Trueif (await append_and_save_message(session_id,ChatMessage(id=wake_id,role="user",- content=WAKE_MESSAGE,+ content=message,metadata=metadata,),)is None):- return+ return Trueturn_id = str(uuid.uuid4())await dispatch_turn(slot,session_id=session_id,user_id=user_id,turn_id=turn_id,- message=WAKE_MESSAGE,+ message=message,organization_id=info.organization_id,team_id=info.team_id,llm_auth_provider=info.metadata.llm_auth_provider,@@ -308,7 +367,7 @@ async def wake(user_id=user_id,inflight_cap=get_inflight_turn_limit(),session_id=session_id,- message=WAKE_MESSAGE,+ message=message,message_id=wake_id,message_metadata=metadata,llm_auth_provider=info.metadata.llm_auth_provider,@@ -328,39 +387,75 @@ async def wake(logger.info(f"Held calls in {session_id} wait for the user's next turn")except Exception:logger.warning(f"Could not wake session {session_id}", exc_info=True)+ return True-async def _deliver(- user_id: str, session: ChatSession, call: HeldCall, cap: Callable[[str], str]-) -> PendingMessage:- from backend.copilot.tools import get_tool+def _wake_message(waiting: int) -> str:+ """The wake row, which tells the model how many of its cards are unanswered."""+ if not waiting:+ return WAKE_MESSAGE+ more = "1 more still waits" if waiting == 1 else f"{waiting} more still wait"+ return f"{WAKE_MESSAGE} {more} for my answer."++async def _waiting(user_id: str, session_id: str) -> int:+ """The chat's held calls still waiting for an answer."""+ try:+ rows = await review_db().get_pending_reviews_for_chat_session(+ session_id, user_id+ )+ except Exception:+ # Unreadable counts as none, so an answer still wakes its turn.+ logger.warning(f"Waiting cards in {session_id} unreadable", exc_info=True)+ return 0+ gate_prefix = review_store.node_id_for("")+ # A workflow step's card resumes its run, not this chat, and leaves by the+ # run page or a stopped run, neither of which wakes the chat.+ return sum(+ 1+ for row in rows+ if row.node_exec_id.startswith(gate_prefix)+ and not row.node_exec_id.startswith(COPILOT_STEP_CARD_PREFIX)+ )+++async def _resolve(+ user_id: str,+ session: ChatSession,+ call: HeldCall,+ row: "PendingHumanReviewModel | None",+ tool: "BaseTool | None",+ deliver: Callable[[PendingMessage], None],+ cap: Callable[[str], str],+) -> None:if call.cost_context is None or call.cost_context.user_id != user_id:await review_store.consume(call.review_id, user_id)- return _result_row(call, _RESEND, "closed")- rows = await review_db().get_reviews_by_node_exec_ids([call.review_id], user_id)- card = rows.get(call.review_id)+ deliver(_result_row(call, _RESEND, "closed"))+ returnwith restore_cost_context(user_id, call.cost_context):- outcome, output = await _outcome(- user_id, session, call, get_tool(call.tool_name), card- )- shown = _card_copy(card)+ if tool:+ outcome, output = await _run(user_id, session, call, tool)+ else:+ outcome, output = await _settled(user_id, session, call, row)+ shown = _card_copy(row)google_data = is_google_call(call.args) or is_google_output(output)try:- return _result_row(call, cap(output), outcome, shown, google_data)+ deliver(_result_row(call, cap(output), outcome, shown, google_data))except Exception:# The outcome is known and may be a refusal; only the engine's cut# failed, so deliver it trimmed rather than let recovery guess.logger.warning(f"Could not cap held result {call.review_id}", exc_info=True)- return _result_row(- call, output[: _MAX_RESULT_CHARS // 2], outcome, shown, google_data+ deliver(+ _result_row(+ call, output[: _MAX_RESULT_CHARS // 2], outcome, shown, google_data+ ))async def _recover(user_id: str, session_id: str, call: HeldCall) -> list[PendingMessage]:- """``_outcome`` failed. A card still open goes back for the next turn; a+ """``_resolve`` failed. A card still open goes back for the next turn; aspent approval means the call reached the gate, so it may have run."""try:rows = await review_db().get_reviews_by_node_exec_ids([call.review_id], user_id)@@ -419,18 +514,40 @@ def _card_copy(card: "PendingHumanReviewModel | None") -> dict[str, Any]:return {k: payload[k] for k in ("arguments", "fields", "clipped") if k in payload}-async def _outcome(+def _due(+ call: HeldCall, row: "PendingHumanReviewModel | None", user_id: str+) -> "BaseTool | None":+ """The tool an answered card's approved call runs with; None when it runs+ nothing, which :func:`_resolve` then says."""+ from backend.copilot.tools import get_tool++ from .reads import is_held_read++ if (+ row is None+ or row.status != ReviewStatus.APPROVED+ or is_held_read(call.review_id)+ or _expired(row)+ or call.lost+ or call.cost_context is None+ or call.cost_context.user_id != user_id+ ):+ return None+ return get_tool(call.tool_name)+++async def _settled(user_id: str,session: ChatSession,call: HeldCall,- tool: "BaseTool | None",row: "PendingHumanReviewModel | None",) -> tuple[Outcome, str]:- if row is None or row.status == ReviewStatus.WAITING:- return "closed", "Nothing ran: this card is no longer open."+ """What an answered card delivers when its call does not run."""# Deferred: reads imports this package's __init__, which imports this module.from .reads import answered_read, is_held_read+ if row is None or row.status == ReviewStatus.WAITING:+ return "closed", "Nothing ran: this card is no longer open."if is_held_read(call.review_id):return await answered_read(user_id, row)if row.status == ReviewStatus.REJECTED:@@ -445,19 +562,33 @@ async def _outcome("Nothing ran: the user declined this action. Do not retry it or ""reach the same effect another way.")- approved_at = row.reviewed_at or row.updated_at or row.created_at- if datetime.now(UTC) - approved_at > review_store.APPROVAL_TTL:- await review_store.consume(call.review_id, user_id)+ await review_store.consume(call.review_id, user_id)+ if _expired(row):return "expired", ("Nothing ran: the approval expired an hour after it was given. ""Propose the call again if it is still needed.")if call.lost:- await review_store.consume(call.review_id, user_id)return "closed", _RESEND- if tool is None:- await review_store.consume(call.review_id, user_id)- return "closed", "Nothing ran: this tool no longer exists."+ return "closed", "Nothing ran: this tool no longer exists."+++def _expired(row: "PendingHumanReviewModel") -> bool:+ approved_at = row.reviewed_at or row.updated_at or row.created_at+ return datetime.now(UTC) - approved_at > review_store.APPROVAL_TTL+++def _headline(call: HeldCall, row: "PendingHumanReviewModel | None") -> str:+ """The card's own headline, which the user approved."""+ payload = row.payload if row and isinstance(row.payload, dict) else {}+ if "headline" in payload:+ return review_store.payload_headline(payload)+ return headline_for(call.tool_name, call.args).text+++async def _run(+ user_id: str, session: ChatSession, call: HeldCall, tool: "BaseTool"+) -> tuple[Outcome, str]:# The gate finds the approval for exactly these arguments and spends it.result = await tool.execute(user_id, session, call.tool_call_id, **call.args)# With the flag switched off since, the gate ran it without spending thediff --git a/autogpt_platform/backend/backend/copilot/gate/held_activation_integration_test.py b/autogpt_platform/backend/backend/copilot/gate/held_activation_integration_test.pyindex c813d71244..9afe77c625 100644--- a/autogpt_platform/backend/backend/copilot/gate/held_activation_integration_test.py+++ b/autogpt_platform/backend/backend/copilot/gate/held_activation_integration_test.py@@ -14,7 +14,13 @@ from backend.copilot import rate_limit, turn_queuefrom backend.copilot.active_turns import ConcurrentTurnLimitErrorfrom backend.copilot.executor import utilsfrom backend.copilot.gate import held-from backend.copilot.gate.held_db_test import _answer, _hold, _new_session, _Post+from backend.copilot.gate.held_db_test import (+ _answer,+ _hold,+ _new_session,+ _Post,+ _resolved,+)from backend.copilot.tools.models import ResponseType, ToolResponseBasefrom backend.copilot.trial_cost_context import (TrialCostContext,@@ -95,8 +101,7 @@ async def test_queued_approvals_charge_their_original_allowances(async def dispatch(*args, **kwargs):assert await capture_cost_context(user_id) == wake_originresults = await asyncio.gather(- held.resolve_answered(user_id, session),- held.resolve_answered(user_id, session),+ _resolved(user_id, session), _resolved(user_id, session))assert sum(map(len, results)) == len(review_ids)@@ -106,7 +111,7 @@ async def test_queued_approvals_charge_their_original_allowances(assert expected_trial in originsassert (expected_paid in origins) == include_paid_callassert get_trial_cost_context(user_id) is None- assert await held.resolve_answered(user_id, session) == []+ assert await _resolved(user_id, session) == []await verify_activation_usage(user_id, generation)paid_usage = 24 if include_paid_call else 7assert [int(await redis.get(key) or 0) for key in keys[:4]] == [diff --git a/autogpt_platform/backend/backend/copilot/gate/held_attribution_test.py b/autogpt_platform/backend/backend/copilot/gate/held_attribution_test.pyindex debcade77b..f5ee025243 100644--- a/autogpt_platform/backend/backend/copilot/gate/held_attribution_test.py+++ b/autogpt_platform/backend/backend/copilot/gate/held_attribution_test.py@@ -39,11 +39,13 @@ async def test_mixed_trial_and_paid_deliveries_keep_their_own_origin(monkeypatchorigins.append(get_trial_cost_context("user-1"))return "approved", "done"- monkeypatch.setattr(held, "_outcome", outcome)+ monkeypatch.setattr(held, "_settled", outcome)+ delivered = []with restore_cost_context("user-1", paid):- delivered = await held.resolve_answered(- "user-1", ChatSession.new(user_id="user-1", dry_run=False)- )+ async for _status in held.resolve_answered(+ "user-1", ChatSession.new(user_id="user-1", dry_run=False), delivered.append+ ):+ passassert get_trial_cost_context("user-1") == paidassert len(delivered) == 2assert origins == [trial, paid]@@ -69,13 +71,19 @@ async def test_unknown_held_origin_closes_without_running(monkeypatch, snapshot)lambda: MagicMock(get_reviews_by_node_exec_ids=AsyncMock(return_value={})),)consume = AsyncMock()- outcome = AsyncMock(return_value=("approved", "done"))+ run = AsyncMock(return_value=("approved", "done"))+ settled = AsyncMock(return_value=("approved", "done"))monkeypatch.setattr(held.review_store, "consume", consume)- monkeypatch.setattr(held, "_outcome", outcome)- [result] = await held.resolve_answered(- "user-1", ChatSession.new(user_id="user-1", dry_run=False)- )- outcome.assert_not_awaited()+ monkeypatch.setattr(held, "_run", run)+ monkeypatch.setattr(held, "_settled", settled)+ delivered = []+ async for _status in held.resolve_answered(+ "user-1", ChatSession.new(user_id="user-1", dry_run=False), delivered.append+ ):+ pass+ [result] = delivered+ run.assert_not_awaited()+ settled.assert_not_awaited()consume.assert_awaited_once_with("review-1", "user-1")assert result.metadata["held_call"]["outcome"] == "closed"assert "send the request again" in result.contentdiff --git a/autogpt_platform/backend/backend/copilot/gate/held_db_test.py b/autogpt_platform/backend/backend/copilot/gate/held_db_test.pyindex 176d4d64c9..404d2f08ff 100644--- a/autogpt_platform/backend/backend/copilot/gate/held_db_test.py+++ b/autogpt_platform/backend/backend/copilot/gate/held_db_test.py@@ -29,13 +29,16 @@ from backend.copilot.model import (ChatMessage,ChatSession,append_and_save_message,+ clear_pending_question,create_chat_session,get_chat_session,update_session_autopilot_mode,upsert_chat_session,)from backend.copilot.pending_message_helpers import persist_pending_as_user_rows+from backend.copilot.pending_messages import PendingMessagefrom backend.copilot.permissions import CopilotPermissions+from backend.copilot.tools.ask_question import AskQuestionToolfrom backend.copilot.tools.base import BaseToolfrom backend.copilot.tools.models import ResponseType, ToolResponseBasefrom backend.copilot.tree import TurnEnvelope, get_tree_ledger, root_envelope@@ -135,6 +138,17 @@ async def _row(review_id: str, user_id: str):return rows.get(review_id)+async def _resolved(+ user_id: str, session: ChatSession, **kwargs: Any+) -> list[PendingMessage]:+ delivered: list[PendingMessage] = []+ async for _status in held.resolve_answered(+ user_id, session, delivered.append, **kwargs+ ):+ pass+ return delivered++@pytest.mark.asyncio(loop_scope="session")async def test_two_held_calls_in_one_turn_both_wait(setup_test_user, test_user_id, gate_on@@ -201,7 +215,7 @@ async def test_an_approval_runs_the_call_with_its_stored_arguments(review_id = await _hold(session, test_user_id, text)await _answer(review_id, ReviewStatus.APPROVED)- delivered = await held.resolve_answered(test_user_id, session)+ delivered = await _resolved(test_user_id, session)assert post_tool.runs == [{"text": text}]assert len(delivered) == 1@@ -225,7 +239,7 @@ async def test_the_result_row_carries_the_arguments_as_the_card_showed_them(assert decision.review_idawait _answer(decision.review_id, ReviewStatus.APPROVED)- [delivered] = await held.resolve_answered(test_user_id, session)+ [delivered] = await _resolved(test_user_id, session)assert post_tool.runs == [args]shown = delivered.metadata["held_call"]@@ -337,7 +351,7 @@ async def test_a_call_rebuilt_from_its_card_in_a_sub_session_is_not_woken(assert reloaded is not Nonerefusals = [m for m in reloaded.messages if m.content == UNRECORDED_WAKE]assert len(refusals) == 1- assert len(await held.resolve_answered(test_user_id, reloaded)) == 1+ assert len(await _resolved(test_user_id, reloaded)) == 1@pytest.mark.asyncio(loop_scope="session")@@ -434,7 +448,7 @@ async def test_a_held_calls_arguments_are_never_stored_in_the_clear(assert stored and all(secret not in str(v) for v in stored.values())await _answer(review_id, ReviewStatus.APPROVED)- await held.resolve_answered(test_user_id, session)+ await _resolved(test_user_id, session)assert post_tool.runs == [{"text": f"token {secret}"}]@@ -446,7 +460,7 @@ async def test_an_approval_whose_usage_origin_was_lost_does_not_run_from_the_carreview_id = await _hold(session, test_user_id, "from the card")await _approve_after_losing_the_held_call(session, test_user_id, review_id)- [delivered] = await held.resolve_answered(test_user_id, session)+ [delivered] = await _resolved(test_user_id, session)assert post_tool.runs == []assert "send the request again" in delivered.content@@ -455,6 +469,33 @@ async def test_an_approval_whose_usage_origin_was_lost_does_not_run_from_the_carassert await _row(review_id, test_user_id) is None[email protected](loop_scope="session")+async def test_a_call_held_without_its_usage_origin_is_never_announced_as_running(+ setup_test_user, test_user_id, gate_on, post_tool+):+ """Held before calls carried an origin, it asks for a resend and runs nothing."""+ session = await _new_session(test_user_id)+ review_id = await _hold(session, test_user_id, "held before origins")+ await _answer(review_id, ReviewStatus.APPROVED)+ [call] = await held.answered(test_user_id, session.session_id)+ assert await held.remember(+ session.session_id, call.model_copy(update={"cost_context": None})+ )+ delivered: list[PendingMessage] = []++ heard = [+ update.message+ async for update in held.resolve_answered(+ test_user_id, session, delivered.append+ )+ ]++ assert heard == []+ assert post_tool.runs == []+ [result] = delivered+ assert "send the request again" in result.content++@pytest.mark.asyncio(loop_scope="session")async def test_a_lost_held_call_the_card_cannot_rebuild_asks_for_a_resend(setup_test_user, test_user_id, gate_on, post_tool@@ -464,7 +505,7 @@ async def test_a_lost_held_call_the_card_cannot_rebuild_asks_for_a_resend(review_id = await _hold(session, test_user_id, "clipped " + "x" * 30_000)await _approve_after_losing_the_held_call(session, test_user_id, review_id)- [delivered] = await held.resolve_answered(test_user_id, session)+ [delivered] = await _resolved(test_user_id, session)assert post_tool.runs == []assert "send the request again" in delivered.content@@ -486,7 +527,7 @@ async def test_a_large_late_result_arrives_as_the_direct_result_would(review_id = await _hold(session, test_user_id, text)await _answer(review_id, ReviewStatus.APPROVED)- [delivered] = await held.resolve_answered(test_user_id, session)+ [delivered] = await _resolved(test_user_id, session)assert delivered.content == (f'<held_call_result tool="{_TOOL}" tool_call_id="call-1" '@@ -502,16 +543,36 @@ async def test_a_failure_after_the_claim_keeps_the_call_for_the_next_turn(review_id = await _hold(session, test_user_id, "flaky")await _answer(review_id, ReviewStatus.APPROVED)- with patch.object(held, "_outcome", AsyncMock(side_effect=RuntimeError("db"))):- assert await held.resolve_answered(test_user_id, session) == []+ with patch.object(held, "_run", AsyncMock(side_effect=RuntimeError("db"))):+ assert await _resolved(test_user_id, session) == []assert [c.review_id for c in await held.answered(test_user_id, session.session_id)] == [review_id]- [delivered] = await held.resolve_answered(test_user_id, session)+ [delivered] = await _resolved(test_user_id, session)assert "posted flaky" in delivered.content[email protected](loop_scope="session")+async def test_a_turn_closed_at_the_status_keeps_the_call_for_the_next_turn(+ setup_test_user, test_user_id, gate_on, post_tool+):+ """A stream closed or cancelled while the status goes out runs nothing."""+ session = await _new_session(test_user_id)+ review_id = await _hold(session, test_user_id, "closed")+ await _answer(review_id, ReviewStatus.APPROVED)+ delivered: list[PendingMessage] = []++ turn = held.resolve_answered(test_user_id, session, delivered.append)+ await anext(turn)+ await turn.aclose()++ assert post_tool.runs == [] and delivered == []+ [result] = await _resolved(test_user_id, session)+ assert post_tool.runs == [{"text": "closed"}]+ assert result.metadata["held_call"]["outcome"] == "approved"++@pytest.mark.parametrize("status, expect",[(ReviewStatus.APPROVED, "posted keep"), (ReviewStatus.REJECTED, "declined")],@@ -528,7 +589,7 @@ async def test_a_failed_cap_still_delivers_the_real_outcome(def broken_cap(_text: str) -> str:raise RuntimeError("cap failed")- [delivered] = await held.resolve_answered(test_user_id, session, cap=broken_cap)+ [delivered] = await _resolved(test_user_id, session, cap=broken_cap)assert expect in delivered.contentassert "may have run" not in delivered.content@@ -550,8 +611,8 @@ async def test_a_failure_after_the_approval_was_spent_says_it_may_have_run(await review_store.consume(review_id, user_id)raise RuntimeError("lost after the gate")- with patch.object(held, "_outcome", spend_then_fail):- [delivered] = await held.resolve_answered(test_user_id, session)+ with patch.object(held, "_run", spend_then_fail):+ [delivered] = await _resolved(test_user_id, session)assert "may have run" in delivered.contentassert delivered.metadata["held_call"]["outcome"] == "unknown"@@ -569,7 +630,7 @@ async def test_an_approval_run_with_the_flag_off_is_still_spent(with patch("backend.copilot.gate.is_feature_enabled", AsyncMock(return_value=False)):- await held.resolve_answered(test_user_id, session)+ await _resolved(test_user_id, session)assert post_tool.runs == [{"text": "flag off"}]assert await _row(review_id, test_user_id) is None@@ -587,7 +648,7 @@ async def test_a_row_approved_and_resolved_four_times_at_once_runs_once(with patch.object(held, "answered", AsyncMock(return_value=listed)):results = await asyncio.gather(- *(held.resolve_answered(test_user_id, session) for _ in range(4))+ *(_resolved(test_user_id, session) for _ in range(4)))assert len(post_tool.runs) == 1@@ -608,7 +669,7 @@ async def test_a_stale_approval_delivers_a_refusal_not_a_run(session = await get_chat_session(session.session_id, test_user_id)assert session is not None- delivered = await held.resolve_answered(test_user_id, session)+ delivered = await _resolved(test_user_id, session)assert post_tool.runs == []assert "expired" in delivered[0].content@@ -628,7 +689,7 @@ async def test_a_rejection_never_runs_and_the_tool_asks_from_then_on(with patch("backend.copilot.gate.is_feature_enabled", AsyncMock(return_value=False)):- delivered = await held.resolve_answered(test_user_id, session)+ delivered = await _resolved(test_user_id, session)assert post_tool.runs == []assert "declined" in delivered[0].content@@ -654,6 +715,54 @@ async def test_a_held_call_returns_the_stub_and_no_partial_result(assert post_tool.runs == []+ "status, heard",+ [+ (ReviewStatus.APPROVED, ["Running the action you approved: Post a message"]),+ (ReviewStatus.REJECTED, []),+ ],+)[email protected](loop_scope="session")+async def test_the_chat_hears_an_approved_action_before_it_runs(+ setup_test_user, test_user_id, gate_on, post_tool, status, heard+):+ """Nothing else streams while it runs; a declined card runs nothing to name."""+ session = await _new_session(test_user_id)+ review_id = await _hold(session, test_user_id, "heard first")+ await _answer(review_id, status)+ delivered: list[PendingMessage] = []+ seen = []++ async for update in held.resolve_answered(test_user_id, session, delivered.append):+ seen.append((update.message, list(post_tool.runs), list(delivered)))++ assert seen == [(message, [], []) for message in heard]+ assert len(delivered) == 1++[email protected](loop_scope="session")+async def test_an_approval_whose_tool_is_gone_runs_nothing_and_says_so(+ setup_test_user, test_user_id, gate_on+):+ session = await _new_session(test_user_id)+ review_id = await _hold(session, test_user_id, "gone")+ await _answer(review_id, ReviewStatus.APPROVED)+ delivered: list[PendingMessage] = []++ with patch("backend.copilot.tools.get_tool", return_value=None):+ heard = [+ update+ async for update in held.resolve_answered(+ test_user_id, session, delivered.append+ )+ ]++ assert heard == []+ [result] = delivered+ assert "no longer exists" in result.content+ assert await _row(review_id, test_user_id) is None++@pytest.mark.asyncio(loop_scope="session")async def test_an_answer_on_an_idle_chat_starts_its_turn(setup_test_user, test_user_id, gate_on@@ -672,6 +781,31 @@ async def test_an_answer_on_an_idle_chat_starts_its_turn(assert dispatch.await_args.kwargs["message"] == held.WAKE_MESSAGE[email protected](loop_scope="session")+async def test_an_open_question_keeps_the_answered_cards_for_its_answer(+ setup_test_user, test_user_id, gate_on+):+ """A turn started now would run over the question and swallow its answer."""+ session = await _new_session(test_user_id)+ review_id = await _hold(session, test_user_id, "after the question")+ await _answer(review_id, ReviewStatus.APPROVED)+ await AskQuestionTool().execute(+ test_user_id, session, "call-q", questions=[{"question": "Which repo?"}]+ )+ dispatch = AsyncMock()++ with patch("backend.copilot.executor.utils.dispatch_turn", dispatch):+ await held.wake(test_user_id, session.session_id)+ dispatch.assert_not_awaited()+ [waiting] = await held.answered(test_user_id, session.session_id)+ assert waiting.review_id == review_id++ await clear_pending_question(session)+ await held.wake(test_user_id, session.session_id)++ dispatch.assert_awaited_once()++@pytest.mark.asyncio(loop_scope="session")async def test_the_chain_row_and_the_card_name_the_call_alike(setup_test_user, test_user_id, gate_on, post_tool@@ -705,7 +839,7 @@ async def test_an_approval_turn_is_judged_against_the_users_request(await held.wake(test_user_id, session.session_id)woken = await get_chat_session(session.session_id, test_user_id)assert woken is not None- delivered = await held.resolve_answered(test_user_id, woken)+ delivered = await _resolved(test_user_id, woken)assert await persist_pending_as_user_rows(woken, None, delivered, log_prefix="test")reloaded = await get_chat_session(session.session_id, test_user_id)assert reloaded is not None@@ -718,3 +852,116 @@ async def test_an_approval_turn_is_judged_against_the_users_request(await check_action("bash_exec", {"command": "ls"}, test_user_id, reloaded)assert supervisor.await_args.kwargs["user_messages"] == [request]++[email protected](loop_scope="session")+async def test_answers_on_an_idle_chat_start_one_turn_at_the_last_card(+ setup_test_user, test_user_id, gate_on, post_tool+):+ session = await _new_session(test_user_id)+ cards = [await _hold(session, test_user_id, text) for text in ("a", "b", "c")]+ dispatch = AsyncMock()++ with patch("backend.copilot.executor.utils.dispatch_turn", dispatch):+ for card in cards[:2]:+ await _answer(card, ReviewStatus.APPROVED)+ assert not await held.wake(+ test_user_id, session.session_id, until_last=True+ )+ dispatch.assert_not_awaited()++ await _answer(cards[2], ReviewStatus.APPROVED)+ assert await held.wake(test_user_id, session.session_id, until_last=True)++ dispatch.assert_awaited_once()+ assert dispatch.await_args.kwargs["message"] == held.WAKE_MESSAGE+ delivered = await _resolved(test_user_id, session)+ assert len(delivered) == 3+ assert sorted(run["text"] for run in post_tool.runs) == ["a", "b", "c"]++[email protected](loop_scope="session")+async def test_continue_starts_the_turn_and_leaves_the_rest_waiting(+ setup_test_user, test_user_id, gate_on, post_tool+):+ session = await _new_session(test_user_id)+ answered, waiting = [+ await _hold(session, test_user_id, text) for text in ("now", "later")+ ]+ await _answer(answered, ReviewStatus.APPROVED)+ dispatch = AsyncMock()++ with patch("backend.copilot.executor.utils.dispatch_turn", dispatch):+ assert not await held.wake(test_user_id, session.session_id, until_last=True)+ await held.wake(test_user_id, session.session_id)++ dispatch.assert_awaited_once()+ assert dispatch.await_args.kwargs["message"] == (+ f"{held.WAKE_MESSAGE} 1 more still waits for my answer."+ )+ delivered = await _resolved(test_user_id, session)+ assert [r.metadata["held_call"]["review_id"] for r in delivered] == [answered]+ row = await _row(waiting, test_user_id)+ assert row is not None and row.status == ReviewStatus.WAITING+ assert post_tool.runs == [{"text": "now"}]++[email protected](loop_scope="session")+async def test_an_answer_during_a_running_turn_is_left_to_that_turns_end(+ setup_test_user, test_user_id, gate_on+):+ """The page follows the turn that end starts, so the answer is not held."""+ session = await _new_session(test_user_id)+ answered, _ = [await _hold(session, test_user_id, t) for t in ("mid", "rest")]+ await _answer(answered, ReviewStatus.APPROVED)+ assert await chat_db().update_chat_session_status(+ session_id=session.session_id,+ expect_status=CHAT_STATUS_IDLE,+ status=CHAT_STATUS_RUNNING,+ user_id=test_user_id,+ )+ dispatch = AsyncMock()++ with patch("backend.copilot.executor.utils.dispatch_turn", dispatch):+ try:+ assert await held.wake(test_user_id, session.session_id, until_last=True)+ dispatch.assert_not_awaited()+ finally:+ await chat_db().update_chat_session_status(+ session_id=session.session_id,+ expect_status=CHAT_STATUS_RUNNING,+ status=CHAT_STATUS_IDLE,+ user_id=test_user_id,+ )+ # The running turn's end.+ await held.wake(test_user_id, session.session_id)++ dispatch.assert_awaited_once()+ assert dispatch.await_args.kwargs["message"].endswith(+ "1 more still waits for my answer."+ )++[email protected](loop_scope="session")+async def test_a_wake_lost_after_the_last_answer_starts_on_the_next_one(+ setup_test_user, test_user_id, gate_on+):+ """No timer or marker holds the batch: whatever wakes the same answered+ cards next (a retried answer, a turn's end) still starts their turn."""+ session = await _new_session(test_user_id)+ cards = [await _hold(session, test_user_id, t) for t in ("first", "last")]+ for card in cards:+ await _answer(card, ReviewStatus.APPROVED)+ dispatch = AsyncMock()++ with patch("backend.copilot.executor.utils.dispatch_turn", dispatch):+ with patch.object(+ chat_db(), "get_chat_session_metadata", side_effect=RuntimeError("died")+ ):+ await held.wake(test_user_id, session.session_id, until_last=True)+ dispatch.assert_not_awaited()++ await held.wake(test_user_id, session.session_id, until_last=True)+ await held.wake(test_user_id, session.session_id, until_last=True)++ dispatch.assert_awaited_once()diff --git a/autogpt_platform/backend/backend/copilot/gate/held_engines_test.py b/autogpt_platform/backend/backend/copilot/gate/held_engines_test.pyindex 209f090e2e..c529345453 100644--- a/autogpt_platform/backend/backend/copilot/gate/held_engines_test.py+++ b/autogpt_platform/backend/backend/copilot/gate/held_engines_test.py@@ -4,31 +4,53 @@ One test per engine, driven through the engine's real entry point and stoppedat the fold: removing the fold from one engine turns only that engine's testred. The held call itself runs inside ``resolve_answered``, so each test alsoproves the turn's execution context — the chat whose ceiling an approval-raises included — is set before it runs.+raises included — is set before it runs, and that the status it yields while+the call runs reaches the engine's stream before the result is folded."""+import asyncioimport contextlibimport json-from unittest.mock import AsyncMock, MagicMock, patch+from collections.abc import Iterator+from unittest.mock import AsyncMock, MagicMock, PropertyMock, patchimport pytest+from backend.copilot.baseline import service as baseline_servicefrom backend.copilot.baseline.service import stream_chat_completion_baselinefrom backend.copilot.context import (get_current_envelope,get_execution_context,set_execution_context,)+from backend.copilot.gate.held import HeldCall, _result_rowfrom backend.copilot.model import ChatSessionfrom backend.copilot.model_router import ResolvedModel-from backend.copilot.pending_messages import PendingMessage+from backend.copilot.pending_message_helpers import (+ combine_pending_with_current,+ persist_pending_as_user_rows,+)+from backend.copilot.pending_messages import (+ PendingMessage,+ format_pending_as_user_message,+)+from backend.copilot.response_model import StreamStatusfrom backend.copilot.sdk.expert_tool_gate_test import _make_patches, _make_session+from backend.copilot.sdk.service import (+ _session_messages_to_transcript,+ stream_chat_completion_sdk,+)from backend.copilot.sdk.tool_adapter import cap_late_tool_resultfrom backend.copilot.tree import root_envelope_RESULT = PendingMessage(content='<held_call_result tool="post_to_chat_platform">posted</held_call_result>')+_RUNNING = StreamStatus(message="Running the action you approved: Post a message")+_SECOND = PendingMessage(+ content='<held_call_result tool="send_email">sent</held_call_result>'+)+_RUNNING_SECOND = StreamStatus(message="Running the action you approved: Send an email")class _StopAtFold(Exception):@@ -37,25 +59,25 @@ class _StopAtFold(Exception):@pytest.mark.asyncioasync def test_the_sdk_engine_opens_its_turn_with_the_held_result():- from backend.copilot.sdk.service import stream_chat_completion_sdk-order: list[str] = []folded: list[list[PendingMessage]] = []caps: list[object] = []chats: list[str | None] = []- async def resolve(*_args, **kwargs):+ async def resolve(_user_id, _session, deliver, **kwargs):order.append("resolve")caps.append(kwargs.get("cap"))chats.append(getattr(get_current_envelope(), "spend_session_id", None))- return [_RESULT]+ yield _RUNNING+ deliver(_RESULT)def set_context(*args, **kwargs):order.append("context")set_execution_context(*args, **kwargs)async def persist(_session, _builder, pending, **_kwargs):+ order.append("fold")folded.append(list(pending))raise _StopAtFold@@ -81,7 +103,7 @@ async def test_the_sdk_engine_opens_its_turn_with_the_held_result():))with contextlib.suppress(_StopAtFold):- async for _ in stream_chat_completion_sdk(+ async for chunk in stream_chat_completion_sdk(session_id=session.session_id,message="hello",is_user_message=True,@@ -89,11 +111,13 @@ async def test_the_sdk_engine_opens_its_turn_with_the_held_result():session=session,envelope=envelope,):- pass+ if chunk is _RUNNING:+ order.append("streamed")set_execution_context(None, None)context.assert_called()assert order.index("context") < order.index("resolve")+ assert order.index("resolve") < order.index("streamed") < order.index("fold")assert folded == [[_RESULT]]# A late result is cut by the same rule as a direct MCP tool result.assert caps == [cap_late_tool_result]@@ -108,56 +132,25 @@ async def test_the_baseline_engine_opens_its_turn_with_the_held_result():chats: list[str | None] = []sent: list[list[dict]] = []- async def resolve(*_args, **_kwargs):+ order: list[str] = []++ async def resolve(_user_id, _session, deliver, **_kwargs):seen_context.append(get_execution_context())chats.append(getattr(get_current_envelope(), "spend_session_id", None))- return [_RESULT]+ yield _RUNNING+ deliver(_RESULT)persist = AsyncMock(return_value=True)async def model_loop(*, messages, **_kwargs):+ order.append("model")sent.append(list(messages))raise _StopAtFoldyield- svc = "backend.copilot.baseline.service"- with (- patch(f"{svc}.build_expert_identity_suffix", new=AsyncMock(return_value="")),- patch(f"{svc}.drain_pending_safe", new=AsyncMock(return_value=[])),- patch(- f"{svc}._resolve_baseline_model",- new=AsyncMock(- return_value=ResolvedModel(- model="anthropic/claude-sonnet-4-6", source="env"- )- ),- ),- patch(- f"{svc}.normalize_model_for_transport",- new=MagicMock(side_effect=lambda model, cfg=None: model),- ),- patch(- "backend.copilot.tools.e2b_sandbox.get_or_create_sandbox",- new=AsyncMock(return_value=None),- ),- patch(- f"{svc}._build_system_prompt",- new=AsyncMock(return_value=("system prompt", None)),- ),- patch(f"{svc}.is_enabled_for_user", new=AsyncMock(return_value=False)),- patch(f"{svc}.is_feature_enabled", new=AsyncMock(return_value=False)),- patch(- f"{svc}.build_builder_system_prompt_suffix",- new=AsyncMock(return_value=""),- ),- patch(f"{svc}.extract_context_messages", new=AsyncMock(return_value=[])),- patch(f"{svc}._compress_session_messages", new=AsyncMock(return_value=[])),- patch(f"{svc}.resolve_answered", new=resolve),- patch(f"{svc}.persist_pending_as_user_rows", new=persist),- patch(f"{svc}.tool_call_loop", new=model_loop),- ):+ with _baseline_patches(resolve, persist, model_loop):try:- async for _ in stream_chat_completion_baseline(+ async for chunk in stream_chat_completion_baseline(session_id=session.session_id,message=None,is_user_message=False,@@ -165,13 +158,15 @@ async def test_the_baseline_engine_opens_its_turn_with_the_held_result():session=session,envelope=root_envelope("turn-2", session_id=session.session_id),):- pass+ if chunk is _RUNNING:+ order.append("streamed")except Exception:pass # what the turn does after the model's first call is not under testfinally:set_execution_context(None, None)assert seen_context == [("user-1", session)]+ assert order == ["streamed", "model"]assert chats == [session.session_id]assert len(sent) == 1assert sent[0][-1] == {"role": "user", "content": _RESULT.content}@@ -183,14 +178,6 @@ async def test_the_baseline_engine_opens_its_turn_with_the_held_result():async def test_the_card_copy_on_a_held_result_never_reaches_the_model():"""The result row carries the card's arguments for the chain row, up to thecard's clip; each engine's fold and history replay read the content alone."""- from backend.copilot.gate.held import HeldCall, _result_row- from backend.copilot.pending_message_helpers import (- combine_pending_with_current,- persist_pending_as_user_rows,- )- from backend.copilot.pending_messages import format_pending_as_user_message- from backend.copilot.sdk.service import _session_messages_to_transcript-marker = "card-copy-only-7f3a"call = HeldCall(review_id="r1", tool_name="post_to_chat_platform", tool_call_id="c1", args={}@@ -224,3 +211,190 @@ async def test_the_card_copy_on_a_held_result_never_reaches_the_model():]assert all("posted" in text for text in model_inputs)assert not [text for text in model_inputs if marker in text]+++ "stop, kept",+ [+ ("closed at the next card's status", [_RESULT]),+ ("cancelled before the fold", [_RESULT, _SECOND]),+ ],+)+async def test_a_sdk_turn_that_ends_early_keeps_the_results_of_calls_it_ran(stop, kept):+ """Their approvals are spent, so nothing would run those calls again; a+ card whose status the stream was closed at stays held for the next turn."""++ async def resolve(_user_id, _session, deliver, **_kwargs):+ yield _RUNNING+ deliver(_RESULT)+ yield _RUNNING_SECOND+ deliver(_SECOND)++ patches, _, _ = _make_patches(hire_experts_enabled=False)+ session = _make_session()+ with contextlib.ExitStack() as stack:+ for target, kwargs in patches:+ stack.enter_context(patch(target, **kwargs))+ stack.enter_context(+ patch("backend.copilot.sdk.service.resolve_answered", new=resolve)+ )+ stack.enter_context(+ patch(+ "backend.copilot.sdk.service.drain_pending_safe",+ AsyncMock(side_effect=asyncio.CancelledError),+ )+ )+ stack.enter_context(+ patch(+ "backend.copilot.pending_message_helpers.persist_session_safe",+ _numbered,+ )+ )+ turn = stream_chat_completion_sdk(+ session_id=session.session_id,+ message="hello",+ is_user_message=True,+ user_id="test-user",+ session=session,+ envelope=root_envelope("turn-2", session_id=session.session_id),+ )+ try:+ async for chunk in turn:+ if chunk is _RUNNING_SECOND and stop.startswith("closed"):+ await turn.aclose()+ break+ except asyncio.CancelledError:+ assert stop.startswith("cancelled")+ set_execution_context(None, None)++ assert _saved_results(session) == [r.content for r in kept]+++ "stop, saved",+ [+ # The second card is not claimed yet, so it stays held.+ ("closed at the next card's status", [_RESULT.content]),+ # Cut inside the fold's own save: both rows are on the session only.+ ("cancelled inside the fold's save", [_RESULT.content, _SECOND.content]),+ ],+)+async def test_a_stopped_baseline_turn_keeps_what_ran_and_ends_its_box_turn(+ stop, saved+):+ """Stopped before the try whose finally ends the turn: the results already+ run are saved once, and the counted turn is ended on the box."""+ session = ChatSession.new("user-1", dry_run=False)+ session.title = "already titled"+ sandbox = MagicMock()++ async def resolve(_user_id, _session, deliver, **_kwargs):+ yield _RUNNING+ deliver(_RESULT)+ yield _RUNNING_SECOND+ deliver(_SECOND)++ async def model_loop(**_kwargs):+ raise AssertionError("the turn was stopped before the model")+ yield++ async def cut(_session: ChatSession, _log_prefix: str) -> ChatSession:+ raise asyncio.CancelledError++ fold_save = cut if stop.startswith("cancelled") else _numbered+ svc = "backend.copilot.baseline.service"+ with (+ _baseline_patches(resolve, persist_pending_as_user_rows, model_loop),+ patch.object(+ type(baseline_service.config),+ "active_e2b_api_key",+ new_callable=PropertyMock,+ return_value="e2b-key",+ ),+ patch(f"{svc}.get_or_create_sandbox", AsyncMock(return_value=sandbox)),+ patch(f"{svc}.workspace_volume_mounts", MagicMock(return_value=[])),+ patch(f"{svc}.count_expert_turn", AsyncMock()),+ patch(f"{svc}.pause_sandbox_direct", AsyncMock()) as pause,+ patch(+ "backend.copilot.pending_message_helpers.persist_session_safe",+ fold_save,+ ),+ patch(f"{svc}.persist_session_safe", _numbered),+ ):+ turn = stream_chat_completion_baseline(+ session_id=session.session_id,+ message=None,+ is_user_message=False,+ user_id="user-1",+ session=session,+ envelope=root_envelope("turn-2", session_id=session.session_id),+ )+ try:+ async for chunk in turn:+ if chunk is _RUNNING_SECOND and stop.startswith("closed"):+ await turn.aclose()+ break+ except asyncio.CancelledError:+ assert stop.startswith("cancelled")+ await asyncio.sleep(0)+ set_execution_context(None, None)++ assert _saved_results(session) == saved+ pause.assert_awaited_once_with(sandbox, session.session_id, expert_id=None)+++def _baseline_patches(resolve, persist, model_loop) -> Iterator[None]:+ svc = "backend.copilot.baseline.service"+ with (+ patch(f"{svc}.build_expert_identity_suffix", new=AsyncMock(return_value="")),+ patch(f"{svc}.drain_pending_safe", new=AsyncMock(return_value=[])),+ patch(+ f"{svc}._resolve_baseline_model",+ new=AsyncMock(+ return_value=ResolvedModel(+ model="anthropic/claude-sonnet-4-6", source="env"+ )+ ),+ ),+ patch(+ f"{svc}.normalize_model_for_transport",+ new=MagicMock(side_effect=lambda model, cfg=None: model),+ ),+ patch(+ "backend.copilot.tools.e2b_sandbox.get_or_create_sandbox",+ new=AsyncMock(return_value=None),+ ),+ patch(+ f"{svc}._build_system_prompt",+ new=AsyncMock(return_value=("system prompt", None)),+ ),+ patch(f"{svc}.is_enabled_for_user", new=AsyncMock(return_value=False)),+ patch(f"{svc}.is_feature_enabled", new=AsyncMock(return_value=False)),+ patch(+ f"{svc}.build_builder_system_prompt_suffix",+ new=AsyncMock(return_value=""),+ ),+ patch(f"{svc}.extract_context_messages", new=AsyncMock(return_value=[])),+ patch(f"{svc}._compress_session_messages", new=AsyncMock(return_value=[])),+ patch(f"{svc}.resolve_answered", new=resolve),+ patch(f"{svc}.persist_pending_as_user_rows", new=persist),+ patch(f"{svc}.tool_call_loop", new=model_loop),+ ):+ yield+++async def _numbered(session: ChatSession, _log_prefix: str) -> ChatSession:+ for i, message in enumerate(session.messages):+ message.sequence = i+ return session+++def _saved_results(session: ChatSession) -> list[str]:+ return [+ m.content or ""+ for m in session.messages+ if m.sequence is not None and "<held_call_result" in (m.content or "")+ ]diff --git a/autogpt_platform/backend/backend/copilot/gate/held_google_data_test.py b/autogpt_platform/backend/backend/copilot/gate/held_google_data_test.pyindex 0b5d1fccd1..c4bc6f2c6a 100644--- a/autogpt_platform/backend/backend/copilot/gate/held_google_data_test.py+++ b/autogpt_platform/backend/backend/copilot/gate/held_google_data_test.py@@ -58,10 +58,17 @@ async def _deliver("review_db",lambda: MagicMock(get_reviews_by_node_exec_ids=AsyncMock(return_value={})),)- monkeypatch.setattr(held, "_outcome", AsyncMock(return_value=("approved", output)))- [row] = await held.resolve_answered(- "user-1", ChatSession.new(user_id="user-1", dry_run=False), cap=cap- )+ monkeypatch.setattr(held, "_due", MagicMock(return_value=MagicMock()))+ monkeypatch.setattr(held, "_run", AsyncMock(return_value=("approved", output)))+ delivered: list[PendingMessage] = []+ async for _status in held.resolve_answered(+ "user-1",+ ChatSession.new(user_id="user-1", dry_run=False),+ delivered.append,+ cap=cap,+ ):+ pass+ [row] = deliveredreturn rowdiff --git a/autogpt_platform/backend/backend/copilot/gate/reads_test.py b/autogpt_platform/backend/backend/copilot/gate/reads_test.pyindex 9f8cfd9718..75927ecc9f 100644--- a/autogpt_platform/backend/backend/copilot/gate/reads_test.py+++ b/autogpt_platform/backend/backend/copilot/gate/reads_test.py@@ -27,6 +27,7 @@ from backend.copilot.model import (ChatSession,ChatSessionMetadata,)+from backend.copilot.pending_messages import PendingMessagefrom backend.copilot.response_model import StreamToolOutputAvailablefrom backend.copilot.sdk.tool_adapter import (_consecutive_tool_failures,@@ -67,12 +68,21 @@ class _Rows:self.held.append(call)return True+ async def held_calls(self, session_id):+ return {call.review_id: call for call in self.held}++ async def claim(self, session_id, review_id):+ kept = [call for call in self.held if call.review_id != review_id]+ claimed, self.held = len(kept) < len(self.held), kept+ return claimed+async def get_reviews_by_node_exec_ids(self, ids, user_id):return {i: self.rows[i] for i in ids if i in self.rows}async def open_review_row(self, review_id, user_id, session, payload, instructions):self.rows[review_id] = SimpleNamespace(node_exec_id=review_id,+ session_id=session.session_id,status=ReviewStatus.WAITING,created_at=datetime.now(UTC),updated_at=None,@@ -145,6 +155,8 @@ def rows():patch.object(review_store, "find_review", fake.find_review),patch.object(review_store, "consume", fake.consume),patch.object(held, "remember", fake.remember),+ patch.object(held, "_held", fake.held_calls),+ patch.object(held, "_claim", fake.claim),patch.object(held, "review_db", lambda: fake),patch.object(review_store, "open_review_row", fake.open_review_row),):@@ -400,13 +412,18 @@ async def test_an_approved_held_read_arrives_as_its_late_result_byte_identical(r(call,) = rows.heldassert call.tool_call_id == "call-7"rows.answer(ReviewStatus.APPROVED)+ delivered: list[PendingMessage] = []- outcome, late = await held._outcome(- "user-1", session, call, tool, rows.rows[call.review_id]- )+ with patch("backend.copilot.tools.get_tool", return_value=tool):+ heard = [+ s async for s in held.resolve_answered("user-1", session, delivered.append)+ ]- assert (outcome, late) == ("approved", _plain_output(_MARKER))+ [late] = delivered+ assert late.metadata["held_call"]["outcome"] == "approved"+ assert _late_body(late) == _plain_output(_MARKER)assert tool.runs == 1, "the late result must be the stored bytes, not a refetch"+ assert heard == [], "a released read is not an action the chat hears run"assert rows.rows == {}@@ -420,12 +437,10 @@ async def test_a_rejected_held_read_never_arrives_and_sets_no_chat_rule(rows):set_ask = AsyncMock()with patch.object(held.chat_rules, "set_ask", set_ask):- outcome, late = await held._outcome(- "user-1", session, call, tool, rows.rows[call.review_id]- )+ [late] = await _resolved(session)- assert outcome == "rejected"- assert _MARKER not in late and "declined" in late+ assert late.metadata["held_call"]["outcome"] == "rejected"+ assert _MARKER not in late.content and "declined" in late.contentset_ask.assert_not_awaited()assert tool.runs == 1@@ -468,11 +483,10 @@ async def test_a_large_released_read_arrives_late_exactly_as_a_direct_result(await _call(_Fetch(content), session)cap = str # the baseline passes no cap: execute already capped- (call,) = rows.heldrows.answer(ReviewStatus.APPROVED)- late = await held._deliver("user-1", session, call, cap)+ [late] = await _resolved(session, cap=cap)- body = late.content.split(">\n", 1)[1].rsplit("\n</held_call_result>", 1)[0]+ body = _late_body(late)assert body == directassert len(body) > 30_000@@ -871,6 +885,19 @@ def _no_action_gate():return patch.object(BaseTool, "_gate", AsyncMock(return_value=(None, ALLOW)))+async def _resolved(session: ChatSession, **kwargs: Any) -> list[PendingMessage]:+ delivered: list[PendingMessage] = []+ async for _status in held.resolve_answered(+ "user-1", session, delivered.append, **kwargs+ ):+ pass+ return delivered+++def _late_body(late: PendingMessage) -> str:+ return late.content.split(">\n", 1)[1].rsplit("\n</held_call_result>", 1)[0]++# What a producer declares came from outside AutoGPT is all the judge reads._STORE_VALUE = "1ff065e9-88e8-4358-9d82-8dc91f622ba9"diff --git a/autogpt_platform/backend/backend/copilot/gate/store_db_test.py b/autogpt_platform/backend/backend/copilot/gate/store_db_test.pyindex 24f41c6ddf..e5d5e1eb48 100644--- a/autogpt_platform/backend/backend/copilot/gate/store_db_test.py+++ b/autogpt_platform/backend/backend/copilot/gate/store_db_test.py@@ -15,6 +15,8 @@ from backend.copilot.model import (update_session_autopilot_mode,upsert_chat_session,)+from backend.copilot.pending_messages import PendingMessage+from backend.copilot.trial_cost_context import TrialCostContextfrom backend.data.db_accessors import review_db@@ -127,18 +129,17 @@ async def test_a_held_read_comes_back_from_its_row_byte_identical(tool_name="read_file",tool_call_id="call-1",args=args,+ cost_context=TrialCostContext(user_id=test_user_id, trial_id=None),)await reads._hold(call, test_user_id, session, "src", "passage", output, True)await PendingHumanReview.prisma().update(where={"nodeExecId": call.review_id}, data={"status": ReviewStatus.APPROVED})- stored = await review_db().get_reviews_by_node_exec_ids(- [call.review_id], test_user_id- )- assert await held._outcome(- test_user_id, session, call, None, stored[call.review_id]- ) == (- "approved",- output,- )+ delivered: list[PendingMessage] = []+ async for _status in held.resolve_answered(test_user_id, session, delivered.append):+ pass++ [late] = delivered+ assert late.metadata["held_call"]["outcome"] == "approved"+ assert late.content.split(">\n", 1)[1] == f"{output}\n</held_call_result>"diff --git a/autogpt_platform/backend/backend/copilot/pending_message_helpers.py b/autogpt_platform/backend/backend/copilot/pending_message_helpers.pyindex ce540b56a5..b4700b7a83 100644--- a/autogpt_platform/backend/backend/copilot/pending_message_helpers.py+++ b/autogpt_platform/backend/backend/copilot/pending_message_helpers.py@@ -665,6 +665,28 @@ async def persist_pending_as_user_rows(return True+async def persist_unfolded_rows(+ session: "ChatSession | None",+ pending: list[PendingMessage],+ *,+ log_prefix: str,+) -> None:+ """Save rows a turn took but ended before folding in, such as results of+ approved calls whose approvals it spent. Empties *pending*, so only the+ first of several exits saves, and never raises over the turn's own error."""+ if not pending or session is None:+ return+ saving = list(pending)+ pending.clear()+ try:+ await persist_pending_as_user_rows(session, None, saving, log_prefix=log_prefix)+ except Exception:+ logger.warning(+ f"{log_prefix} Could not save {len(saving)} unfolded row(s)",+ exc_info=True,+ )++def drained_rows_entry(pending: list[PendingMessage],content_of: Callable[[PendingMessage], str] = lambda pm: pm.content,diff --git a/autogpt_platform/backend/backend/copilot/pending_messages.py b/autogpt_platform/backend/backend/copilot/pending_messages.pyindex 1657039e15..25c886a0af 100644--- a/autogpt_platform/backend/backend/copilot/pending_messages.py+++ b/autogpt_platform/backend/backend/copilot/pending_messages.py@@ -392,7 +392,7 @@ async def drain_pending_messages(session_id: str) -> list[PendingMessage]:messages: list[PendingMessage] = []for payload in decoded:try:- messages.append(PendingMessage.model_validate(json.loads(payload)))+ messages.append(_from_buffer(json.loads(payload)))except (json.JSONDecodeError, ValidationError, TypeError, ValueError) as e:logger.warning("pending_messages: dropping malformed entry for %s: %s",@@ -409,6 +409,17 @@ async def drain_pending_messages(session_id: str) -> list[PendingMessage]:return messages+def _from_buffer(data: Any) -> PendingMessage:+ # Deferred: the gate's held result is built on this module's message.+ from backend.copilot.gate.held import HeldResult++ # A held call's result, re-queued by a save that rolled back, keeps the+ # larger cap it was built with; the action behind it has already run.+ if isinstance(data, dict) and HeldResult.carried_by(data):+ return HeldResult.model_validate(data)+ return PendingMessage.model_validate(data)++async def peek_pending_count(session_id: str) -> int:"""Return the current buffer length without consuming it."""redis = await get_redis_async()diff --git a/autogpt_platform/backend/backend/copilot/pending_messages_test.py b/autogpt_platform/backend/backend/copilot/pending_messages_test.pyindex 1536f2384d..f321829eaa 100644--- a/autogpt_platform/backend/backend/copilot/pending_messages_test.py+++ b/autogpt_platform/backend/backend/copilot/pending_messages_test.py@@ -13,6 +13,7 @@ from unittest.mock import AsyncMockimport pytestfrom backend.copilot import pending_messages as pm_module+from backend.copilot.gate.held import HeldResultfrom backend.copilot.pending_messages import (MAX_PENDING_MESSAGES,PendingMessage,@@ -244,6 +245,22 @@ def test_pending_message_metadata_survives_json() -> None:assert PendingMessage.model_validate_json('{"content": "old"}').metadata is None+async def test_a_long_held_result_requeued_into_the_buffer_drains_whole(+ fake_redis: _FakeRedis,+) -> None:+ """A rolled-back save re-queues held results here; one past a typed+ follow-up's 32,000 characters must still drain, as its action already ran."""+ result = HeldResult(content="x" * 40_000, metadata={"held_call": {"r": "1"}})+ await push_pending_message("sess-held", result)++ drained = await drain_pending_messages("sess-held")++ assert len(drained) == 1+ assert isinstance(drained[0], HeldResult)+ assert drained[0].content == result.content++@pytest.mark.asyncioasync def test_push_and_drain_preserves_order(fake_redis: _FakeRedis) -> None:for i in range(3):diff --git a/autogpt_platform/backend/backend/copilot/sdk/service.py b/autogpt_platform/backend/backend/copilot/sdk/service.pyindex 4fce4e1f24..3f809e88d3 100644--- a/autogpt_platform/backend/backend/copilot/sdk/service.py+++ b/autogpt_platform/backend/backend/copilot/sdk/service.py@@ -136,6 +136,7 @@ from ..pending_message_helpers import (pending_texts_from,persist_pending_as_user_rows,persist_session_safe,+ persist_unfolded_rows,)from ..pending_messages import (PendingMessage,@@ -5027,6 +5028,9 @@ async def stream_chat_completion_sdk( # pyright: ignore[reportGeneralTypeIssues# Built with the CLI options below; read after the loop to keep a paused# turn from auto-continuing.governor: TurnGovernor | None = None+ # Results of approved cards that ran before the turn saved them; their+ # approvals are spent, so a turn that ends first must save them itself.+ unsaved_held: list[PendingMessage] = []# Make sure there is no more code between the lock acquisition and try-block.try:@@ -5556,9 +5560,12 @@ async def stream_chat_completion_sdk( # pyright: ignore[reportGeneralTypeIssuesyield StreamStatus(message="Preparing conversation context…")# Answered cards first: their results ride the same fold as pending.- pending_messages = await resolve_answered(- user_id, session, cap=cap_late_tool_result- ) + await drain_pending_safe(session_id, log_prefix)+ async for held_status in resolve_answered(+ user_id, session, unsaved_held.append, cap=cap_late_tool_result+ ):+ yield held_status+ pending_messages: list[PendingMessage] = [*unsaved_held]+ pending_messages += await drain_pending_safe(session_id, log_prefix)if pending_messages:logger.info("%s Draining %d pending message(s) at turn start",@@ -5674,6 +5681,9 @@ async def stream_chat_completion_sdk( # pyright: ignore[reportGeneralTypeIssues# untouched ensures the NEXT turn's drain doesn't double-# combine (re-queued pending + combined-from-this-turn) into# the model's context.+ # The helper appends the rows before its first await, so from here+ # the turn's own save, or its re-queue, carries the held results.+ unsaved_held = []persisted_ok = await persist_pending_as_user_rows(session,None,@@ -6297,8 +6307,10 @@ async def stream_chat_completion_sdk( # pyright: ignore[reportGeneralTypeIssues# and the user sees "Another stream is already active" on every retry.logger.warning("%s GeneratorExit — releasing stream lock", log_prefix)await lock.release()+ await persist_unfolded_rows(session, unsaved_held, log_prefix=log_prefix)raiseexcept BaseException as e:+ await persist_unfolded_rows(session, unsaved_held, log_prefix=log_prefix)# Catch BaseException to handle both Exception and CancelledError# (CancelledError inherits from BaseException in Python 3.8+)if isinstance(e, asyncio.CancelledError):@@ -6369,6 +6381,7 @@ async def stream_chat_completion_sdk( # pyright: ignore[reportGeneralTypeIssuesfinally:turn_error = sys.exception()tool_display_bridge.reset()+ await persist_unfolded_rows(session, unsaved_held, log_prefix=log_prefix)# Pending messages are drained atomically at the start of each# turn (see drain_pending_messages call above), so there's# nothing to clean up here — any message pushed after thatdiff --git a/autogpt_platform/backend/backend/copilot/sdk/transient_retry_test.py b/autogpt_platform/backend/backend/copilot/sdk/transient_retry_test.pyindex e5c34e75ff..017ca2964f 100644--- a/autogpt_platform/backend/backend/copilot/sdk/transient_retry_test.py+++ b/autogpt_platform/backend/backend/copilot/sdk/transient_retry_test.py@@ -19,7 +19,7 @@ import jsonimport osimport randomimport time-from collections.abc import Awaitable, Callable+from collections.abc import AsyncGenerator, Awaitable, Callablefrom datetime import UTC, datetimefrom pathlib import Pathfrom typing import Any@@ -499,12 +499,14 @@ class _Turn:("build_skills_update_notice", None),("build_session_context", ""),("inject_user_context", None),- ("resolve_answered", []),("drain_pending_for_persist", []),):stack.enter_context(patch(f"{_SVC}.{name}", new_callable=AsyncMock, return_value=value))+ stack.enter_context(+ patch(f"{_SVC}.resolve_answered", new=_no_answered_cards)+ )stack.enter_context(patch(f"{_SVC}._compute_transient_backoff", return_value=0))@@ -717,3 +719,8 @@ def _reconcile_tasks() -> list[asyncio.Task]:for task in sdk_service._background_tasksif task.get_loop() is loop and not task.done()]+++async def _no_answered_cards(*_: Any, **__: Any) -> AsyncGenerator[Any, None]:+ return+ yielddiff --git a/autogpt_platform/backend/backend/copilot/sdk/turn_cost_restart_test.py b/autogpt_platform/backend/backend/copilot/sdk/turn_cost_restart_test.pyindex e4d8d4e5d2..24f481d9f7 100644--- a/autogpt_platform/backend/backend/copilot/sdk/turn_cost_restart_test.py+++ b/autogpt_platform/backend/backend/copilot/sdk/turn_cost_restart_test.py@@ -2,8 +2,10 @@import asyncioimport contextlib+from collections.abc import AsyncGeneratorfrom datetime import UTC, datetimefrom pathlib import Path+from typing import Anyfrom unittest.mock import AsyncMock, MagicMock, patchimport pytest@@ -104,9 +106,7 @@ async def test_building_restart_preserves_cost_and_learning_capture(capture = stack.enter_context(patch(f"{_SVC}.capture_chat_turn", new_callable=AsyncMock))- stack.enter_context(- patch(f"{_SVC}.resolve_answered", new_callable=AsyncMock, return_value=[])- )+ stack.enter_context(patch(f"{_SVC}.resolve_answered", new=_no_answered_cards))events = [eventasync for event in stream_chat_completion_sdk(@@ -139,3 +139,8 @@ async def test_building_restart_preserves_cost_and_learning_capture("before restart","after restart",]+++async def _no_answered_cards(*_: Any, **__: Any) -> AsyncGenerator[Any, None]:+ return+ yielddiff --git a/autogpt_platform/backend/backend/copilot/stream_drift/drift_causes_test.py b/autogpt_platform/backend/backend/copilot/stream_drift/drift_causes_test.pyindex f0f7fd89f9..a742f63e5c 100644--- a/autogpt_platform/backend/backend/copilot/stream_drift/drift_causes_test.py+++ b/autogpt_platform/backend/backend/copilot/stream_drift/drift_causes_test.py@@ -199,15 +199,6 @@ async def test_a_silent_turn_keeps_publishing_within_the_stream_ttl(@requires_redis- strict=True,- raises=AssertionError,- reason=(- "Approval wake: the end-of-turn wake dispatches the follow-up turn while "- "the ending turn is still registered, and the executor drops it as a "- "duplicate without closing it, so the chat reads running forever"- ),-)async def test_a_turn_the_executor_drops_is_not_left_running(session_id: str, turn_id: str) -> None:diff --git a/autogpt_platform/backend/backend/copilot/stream_drift/scripted.py b/autogpt_platform/backend/backend/copilot/stream_drift/scripted.pyindex fcc445b62e..47af6c7a05 100644--- a/autogpt_platform/backend/backend/copilot/stream_drift/scripted.py+++ b/autogpt_platform/backend/backend/copilot/stream_drift/scripted.py@@ -4,7 +4,7 @@ import contextlibimport tempfileimport timeimport uuid-from collections.abc import AsyncGenerator+from collections.abc import AsyncGenerator, Callablefrom typing import Anyfrom unittest.mock import AsyncMock, MagicMock, patch@@ -172,6 +172,17 @@ async def sdk_service_turn(connection.__aenter__.return_value = clientreturn connection+ async def answered_cards(+ _user_id: str,+ _session: ChatSession,+ deliver: Callable[[PendingMessage], None],+ **__: Any,+ ) -> AsyncGenerator[StreamBaseResponse, None]:+ for result in held_results or []:+ deliver(result)+ return+ yield+def lock(*_: Any, owner_id: str = "", **__: Any) -> MagicMock:return MagicMock(try_acquire=AsyncMock(return_value=owner_id),@@ -202,7 +213,7 @@ async def sdk_service_turn("drain_pending_safe": AsyncMock(side_effect=[[], queued_after_first] + [[]] * len(queries)),- "resolve_answered": AsyncMock(return_value=held_results or []),+ "resolve_answered": answered_cards,"build_session_context": AsyncMock(return_value=""),"build_skills_context": AsyncMock(return_value=""),"_maybe_prepend_skills_update": AsyncMock(side_effect=lambda *a: a[-1]),diff --git a/autogpt_platform/backend/backend/copilot/stream_registry.py b/autogpt_platform/backend/backend/copilot/stream_registry.pyindex b471afaf40..4d7f417def 100644--- a/autogpt_platform/backend/backend/copilot/stream_registry.py+++ b/autogpt_platform/backend/backend/copilot/stream_registry.py@@ -27,6 +27,7 @@ from pydantic import BaseModel, ValidationErrorfrom redis.exceptions import RedisError, ResponseErrorfrom backend.api.model import CopilotCompletionPayload+from backend.copilot import after_turnfrom backend.copilot.active_turns import release_turn_slotfrom backend.copilot.turn_queue import dispatch_next_for_userfrom backend.data.db_accessors import chat_db@@ -1037,16 +1038,6 @@ async def mark_session_completed(logger.debug(f"Session {session_id} already completed/failed, skipping")return False- # Release the per-user concurrent-turn slot now that this turn is no- # longer in flight. ``meta`` may be an empty dict if the session's- # Redis key expired between the ``hgetall`` above and the CAS that- # just succeeded — read ``user_id`` directly so we don't skip the- # release on an empty-but-not-None payload. ``user_id`` is empty for- # anonymous sessions; ``release_turn_slot`` is a no-op then.- user_id = meta.get("user_id") or ""- if user_id:- await release_turn_slot(user_id, session_id)-# Force-release the executor's cluster lock so the next enqueued turn can# acquire it immediately. The lock holder's on_run_done will also release# (idempotent delete); doing it here unblocks cases where the task hangs@@ -1065,6 +1056,18 @@ async def mark_session_completed(except RedisError as e:logger.warning(f"Failed to release stream lock for session {session_id}: {e}")+ # Release the per-user concurrent-turn slot now that this turn is no+ # longer in flight, and only after its locks: a turn admitted before then+ # meets this one's cluster lock on another pod, which drops it unrun.+ # ``meta`` may be an empty dict if the session's Redis key expired+ # between the ``hgetall`` above and the CAS that just succeeded — read+ # ``user_id`` directly so we don't skip the release on an+ # empty-but-not-None payload. ``user_id`` is empty for anonymous+ # sessions; ``release_turn_slot`` is a no-op then.+ user_id = meta.get("user_id") or ""+ if user_id:+ await release_turn_slot(user_id, session_id)+# Promote the user's oldest queued turn (if any) AFTER the executor# cluster lock and SDK stream lock are cleared — otherwise the# promoted turn races against the just-finished turn's stale locks@@ -1083,11 +1086,14 @@ async def mark_session_completed(# A card answered while this turn ran has nobody to start its turn but us,# an error included; only the user's own Stop leaves it for their next turn.+ # Not before this turn has left its executor, which would drop it unrun.if user_id and error_message != CANCELLED_MESSAGE:# Deferred: the gate reaches back here through pending_messages.from backend.copilot.gate.held import wake as wake_for_held_calls- await wake_for_held_calls(user_id, session_id)+ await after_turn.run_after_turn(+ turn_id, lambda: wake_for_held_calls(user_id, session_id)+ )if error_message and not skip_error_publish:try:diff --git a/autogpt_platform/backend/backend/copilot/stream_registry_test.py b/autogpt_platform/backend/backend/copilot/stream_registry_test.pyindex e5bb23d8f9..eabc0fd2be 100644--- a/autogpt_platform/backend/backend/copilot/stream_registry_test.py+++ b/autogpt_platform/backend/backend/copilot/stream_registry_test.py@@ -907,6 +907,38 @@ class TestCompletionOnRealRedis:assert session is not None and session.status == "running"publish.assert_not_awaited()+ async def test_a_turn_frees_its_locks_before_its_slot(self, session_id):+ """A turn admitted into the freed slot must not meet this turn's lock on+ another pod, which would drop it unrun."""+ await stream_registry.create_session(session_id, "u1", "", "", turn_id="a")+ redis = await redis_client.get_redis_async()+ await redis.set(get_session_lock_key(session_id), "pod-a")+ await redis.set(f"{STREAM_LOCK_PREFIX}{session_id}", "a")+ held_at_release: list[int] = []++ async def release(_user_id: str, sid: str) -> None:+ held_at_release.append(+ await redis.exists(+ get_session_lock_key(sid), f"{STREAM_LOCK_PREFIX}{sid}"+ )+ )++ with (+ patch.object(stream_registry, "release_turn_slot", new=release),+ patch.object(stream_registry, "publish_chunk", new=AsyncMock()),+ patch.object(+ stream_registry.chat_db(),+ "set_turn_duration",+ new=AsyncMock(),+ create=True,+ ),+ patch.object(stream_registry, "dispatch_next_for_user", new=AsyncMock()),+ patch("backend.copilot.gate.held.wake", new=AsyncMock()),+ ):+ await stream_registry.mark_session_completed(session_id, turn_id="a")++ assert held_at_release == [0]+async def test_a_turn_still_completes_itself(self, session_id):await stream_registry.create_session(session_id, None, "", "", turn_id="a")diff --git a/autogpt_platform/backend/backend/copilot/tools/expert_change_gate_test.py b/autogpt_platform/backend/backend/copilot/tools/expert_change_gate_test.pyindex de7fac18b7..9459591c4d 100644--- a/autogpt_platform/backend/backend/copilot/tools/expert_change_gate_test.py+++ b/autogpt_platform/backend/backend/copilot/tools/expert_change_gate_test.py@@ -311,7 +311,10 @@ async def test_the_held_confirm_fixture_is_what_an_approved_hold_delivers(gate):patch(f"{_GATE}.held.review_db", return_value=reviews),patch(f"{_GATE}.review_store.consume", AsyncMock(return_value=True)),):- [row] = await held.resolve_answered(_USER, session)+ rows: list[Any] = []+ async for _status in held.resolve_answered(_USER, session, rows.append):+ pass+ [row] = rowsassert row.metadata["held_call"]["outcome"] == "approved"part = {diff --git a/autogpt_platform/backend/backend/executor/jobstore_test.py b/autogpt_platform/backend/backend/executor/jobstore_test.pyindex e0e41fdb17..6c5f41fb04 100644--- a/autogpt_platform/backend/backend/executor/jobstore_test.py+++ b/autogpt_platform/backend/backend/executor/jobstore_test.py@@ -2,12 +2,15 @@ import loggingimport osimport pickleimport tempfile+import threading+import timefrom contextlib import contextmanagerfrom datetime import datetime, timedelta, timezonefrom enum import Enumfrom unittest.mock import MagicMock, patchimport pytest+from apscheduler.jobstores.memory import MemoryJobStorefrom apscheduler.schedulers.background import BackgroundSchedulerfrom apscheduler.util import datetime_to_utc_timestampfrom sqlalchemy import select@@ -382,6 +385,48 @@ def test_strip_enums_leaves_plain_values_alone():assert _strip_enums(payload) == payload+def test_a_held_wake_is_one_job_per_chat_and_runs_after_its_scheduler_is_lost():+ fd, path = tempfile.mkstemp(suffix=".sqlite")+ os.close(fd)+ url = f"sqlite:///{path}"+ wake = MagicMock()+ ran = threading.Event()+ try:+ lost = _production_like(url)+ lost.start(paused=True)+ scheduler = Scheduler.__new__(Scheduler)+ scheduler.scheduler = lost+ scheduler.schedule_held_wake("user-1", "chat-1", 0.05)+ scheduler.schedule_held_wake("user-1", "chat-1", 0.05)+ assert [job.id for job in lost.get_jobs()] == ["held_wake_chat-1"]+ lost.shutdown(wait=False)+ time.sleep(0.3)++ with (+ patch("backend.copilot.gate.held.wake", wake),+ patch(f"{_SCHEDULER_PATH}.run_async", side_effect=lambda _: ran.set()),+ ):+ restarted = _production_like(url)+ restarted.start()+ try:+ assert ran.wait(5)+ finally:+ restarted.shutdown(wait=False)+ wake.assert_called_once_with("user-1", "chat-1")+ finally:+ os.unlink(path)+++def _production_like(url: str) -> BackgroundScheduler:+ return BackgroundScheduler(+ jobstores={+ Jobstores.EXECUTION.value: ResilientSQLAlchemyJobStore(url=url),+ Jobstores.WEEKLY_NOTIFICATIONS.value: MemoryJobStore(),+ },+ job_defaults={"coalesce": True, "misfire_grace_time": None},+ )++@contextmanagerdef _store(cls=ResilientSQLAlchemyJobStore):fd, path = tempfile.mkstemp(suffix=".sqlite")diff --git a/autogpt_platform/backend/backend/executor/scheduler.py b/autogpt_platform/backend/backend/executor/scheduler.pyindex 0323f53dbe..8944c7d8f9 100644--- a/autogpt_platform/backend/backend/executor/scheduler.py+++ b/autogpt_platform/backend/backend/executor/scheduler.py@@ -384,6 +384,14 @@ async def _execute_graph(**kwargs):)+def execute_held_wake(user_id: str, session_id: str):+ """Start the turn that carries a chat's answered cards."""+ # Deferred: the gate package imports this module (``gate/references.py``).+ from backend.copilot.gate.held import wake++ run_async(wake(user_id, session_id))++def execute_copilot_turn(**kwargs):"""Enqueue a copilot turn on the executor queue and wait for completion.@@ -4672,6 +4680,23 @@ class Scheduler(AppService):)return result.model_dump(mode="json")+ @expose+ def schedule_held_wake(+ self, user_id: str, session_id: str, delay_seconds: float+ ) -> None:+ """Wake the chat's answered cards in *delay_seconds*. Keyed by chat, so+ each later answer moves the one job, and stored, so it runs after a+ restart however late."""+ self.scheduler.add_job(+ execute_held_wake,+ kwargs={"user_id": user_id, "session_id": session_id},+ trigger="date",+ run_date=datetime.now(timezone.utc) + timedelta(seconds=delay_seconds),+ id=f"held_wake_{session_id}",+ jobstore=Jobstores.EXECUTION.value,+ replace_existing=True,+ )+# ---- Fire-and-forget admin triggers (JobStatus-aware) -------------------## The ``schedule_immediate_*`` methods schedule the matching@@ -4870,3 +4895,4 @@ class SchedulerClient(AppServiceClient):schedule_immediate_community_rebuild = endpoint_to_async(Scheduler.schedule_immediate_community_rebuild)+ schedule_held_wake = endpoint_to_async(Scheduler.schedule_held_wake)diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ApprovalQueue/ApprovalQueue.stories.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ApprovalQueue/ApprovalQueue.stories.tsxindex 08b423e86b..dea54d70d4 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ApprovalQueue/ApprovalQueue.stories.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ApprovalQueue/ApprovalQueue.stories.tsx@@ -464,3 +464,35 @@ export const RealSandboxCommand = realStory("Sandbox Command");export const RealGmailNewsletter = realStory("Gmail Newsletter");// A step of a workflow Otto started, parked on its own review: its card in the chat.export const RealWorkflowStep = realStory("Workflow Step");++// One turn's three real cards. Review all opens Home's dialog over them; once+// one is answered, Close and continue starts the turn and folds the box.+export const ReviewAllAndContinue: Story = (() => {+ const cards = realCards();+ const reviews = ["Gmail Send", "Post To X", "Workflow"].map((name, i) => ({+ ...cards.find((c) => c.story === name)!.review,+ created_at: new Date(Date.now() - (9 - i) * 60_000),+ }));+ return {+ args: { ...queueOf(reviews), reviews },+ parameters: {+ msw: {+ handlers: [+ realCardSchemaHandler(cards),+ http.post("*/api/review/action", () =>+ HttpResponse.json({+ approved_count: 1,+ rejected_count: 0,+ failed_count: 0,+ awaiting_other_cards: true,+ }),+ ),+ http.post(+ "*/api/review/session/*/continue",+ () => new HttpResponse(null, { status: 204 }),+ ),+ ],+ },+ },+ };+})();diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ApprovalQueue/ApprovalQueue.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ApprovalQueue/ApprovalQueue.tsxindex ad9101b9e8..e23ace21b8 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ApprovalQueue/ApprovalQueue.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ApprovalQueue/ApprovalQueue.tsx@@ -1,11 +1,14 @@"use client";+import type { HomeExpert } from "@/app/api/__generated__/models/homeExpert";+import type { PendingHumanReviewModel } from "@/app/api/__generated__/models/pendingHumanReviewModel";+import { HeldReviewDialog } from "@/app/(platform)/home/components/NeedsYou/components/HeldReviewDialog";import { cn } from "@/lib/utils";import { ApprovalCard } from "./components/ApprovalCard/ApprovalCard";import { CompactApprovalLine } from "./components/CompactApprovalLine";+import { QueueFooter } from "./components/QueueFooter";import { QueueHeader } from "./components/QueueHeader";import { ReceiptLine } from "./components/ReceiptLine";-import { RejectAllFooter } from "./components/RejectAllFooter";import {type ApprovalItem,approvalCardId,@@ -14,22 +17,39 @@ import {modeLine,} from "./helpers";import { useApprovalQueue } from "./useApprovalQueue";+import { useChatReview } from "./useChatReview";interface Props {// Oldest first, as the gate raised them.items: ApprovalItem[];+ // The same cards as rows, which the review dialog reads; none, no dialog.+ reviews?: PendingHumanReviewModel[];+ // Cards answered before this mount whose calls have not run yet.+ answered?: PendingHumanReviewModel[];// The chat's Expert, or null in a chat with Otto.expertName?: string | null;- onAnswered: () => void;+ expert?: HomeExpert | null;+ onAnswered: (turnFollows: boolean) => void;+ onContinued?: () => void;}// From this many cards up, each collapses to one line.export const COMPACT_FROM = 4;-export function ApprovalQueue({ items, expertName = null, onAnswered }: Props) {- const queue = useApprovalQueue({ items, onAnswered });+export function ApprovalQueue({+ items,+ reviews = [],+ answered,+ expertName = null,+ expert = null,+ onAnswered,+ onContinued,+}: Props) {+ const queue = useApprovalQueue({ items, answered, onAnswered, onContinued });+ const review = useChatReview({ queue, reviews, expert });const { pending, receipts, leaving } = queue;- if (pending.length === 0 && receipts.length === 0) return null;+ const dialogOpen = review.carousel.open;+ if (pending.length === 0 && receipts.length === 0 && !dialogOpen) return null;const compact = pending.length >= COMPACT_FROM;const mode = pending.find((item) => item.mode)?.mode ?? null;@@ -39,6 +59,31 @@ export function ApprovalQueue({ items, expertName = null, onAnswered }: Props) {(item) => queue.statusOf(item.reviewId) !== "idle",);+ const dialog = (+ <HeldReviewDialog+ carousel={review.carousel}+ held={review.held}+ chatLink={false}+ />+ );++ if (queue.collapsed)+ return (+ <section+ aria-label="Waiting for you"+ className="overflow-hidden rounded-xl border border-zinc-200 bg-white"+ >+ <QueueHeader+ count={pending.length}+ mode={mode}+ approveAll={null}+ onReviewAll={null}+ onShow={queue.expand}+ />+ {dialog}+ </section>+ );+return (<sectionaria-label="Waiting for you"@@ -59,6 +104,10 @@ export function ApprovalQueue({ items, expertName = null, onAnswered }: Props) {}: null}+ onReviewAll={+ reviews.length > 0 && pending.length >= 2 ? review.open : null+ }+ onShow={null}/>{showModeLine && pending.length > 0 && (<p className="border-b border-zinc-100 px-4 py-2 text-sm text-zinc-500">@@ -104,16 +153,24 @@ export function ApprovalQueue({ items, expertName = null, onAnswered }: Props) {<ReceiptLine key={receipt.item.reviewId} receipt={receipt} />))}</ul>- {pending.length >= 2 && (- <RejectAllFooter- count={pending.length}- confirming={queue.confirmRejectAll}- busy={anyBusy}- onAsk={() => queue.setConfirmRejectAll(true)}- onCancel={() => queue.setConfirmRejectAll(false)}- onConfirm={() => queue.answer(pending, false)}+ {(pending.length >= 2 || queue.canContinue) && (+ <QueueFooter+ rejectAll={+ pending.length >= 2+ ? {+ count: pending.length,+ confirming: queue.confirmRejectAll,+ onAsk: () => queue.setConfirmRejectAll(true),+ onCancel: () => queue.setConfirmRejectAll(false),+ onConfirm: () => queue.answer(pending, false),+ }+ : null+ }+ onContinue={queue.canContinue ? queue.continueNow : null}+ busy={anyBusy || queue.isContinuing}/>)}+ {dialog}</section>);}diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ApprovalQueue/__tests__/leaving.test.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ApprovalQueue/__tests__/leaving.test.tsxindex ef728ef9a9..ae7de10a4d 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ApprovalQueue/__tests__/leaving.test.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ApprovalQueue/__tests__/leaving.test.tsx@@ -83,6 +83,29 @@ test("a new held call brings the box back without the earlier receipt", async ()expect(screen.queryByText("· Approved")).toBeNull();});+test("an open review dialog keeps its answers past the linger, and the box leaves once it closes", async () => {+ waiting = [folder("a", "Q3 reports"), folder("b", "Invoices")];+ const user = renderQueue();++ await user.click(await screen.findByRole("button", { name: "Review all" }));+ const dialog = await screen.findByRole("dialog");+ await user.click(within(dialog).getByRole("button", { name: "Approve" }));+ await within(dialog).findByRole("heading", {+ name: "Create library folder Invoices",+ });+ await user.click(within(dialog).getByRole("button", { name: "Approve" }));+ await within(dialog).findByText("All 2 reviewed");+ await advance(3000);++ expect(within(dialog).getByText("All 2 reviewed")).toBeDefined();+ expect(box()).not.toBeNull();++ await user.click(within(dialog).getByRole("button", { name: "Done" }));+ await advance(1500);++ expect(box()).toBeNull();+});+function renderQueue() {render(<CopilotChatActionsProvider onSend={vi.fn()} onBackendTurn={vi.fn()}>diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ApprovalQueue/components/QueueFooter.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ApprovalQueue/components/QueueFooter.tsxnew file mode 100644index 0000000000..31fa376979--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ApprovalQueue/components/QueueFooter.tsx@@ -0,0 +1,69 @@+import { Button } from "@/components/atoms/Button/Button";+import { AUTOPILOT_NAME } from "@/components/molecules/AutopilotAvatar/helpers";++interface Props {+ rejectAll: {+ count: number;+ confirming: boolean;+ onAsk: () => void;+ onCancel: () => void;+ onConfirm: () => void;+ } | null;+ // Shown once something is answered and something still waits.+ onContinue: (() => void) | null;+ busy: boolean;+}++export function QueueFooter({ rejectAll, onContinue, busy }: Props) {+ return (+ <div className="flex flex-wrap items-center gap-2 border-t border-zinc-100 bg-zinc-50/60 px-4 py-2 text-sm">+ {rejectAll?.confirming ? (+ <>+ <span role="alert" className="text-zinc-700">+ Reject all {rejectAll.count}? {AUTOPILOT_NAME} will be told none of+ them ran.+ </span>+ <Button+ size="xs"+ variant="primary"+ className="rounded-full"+ disabled={busy}+ onClick={rejectAll.onConfirm}+ >+ Reject all+ </Button>+ <Button+ size="xs"+ variant="ghost"+ className="rounded-full"+ onClick={rejectAll.onCancel}+ >+ Cancel+ </Button>+ </>+ ) : rejectAll ? (+ <Button+ variant="link"+ size="small"+ className="h-auto min-w-0 px-0 py-0 text-sm"+ disabled={busy}+ onClick={rejectAll.onAsk}+ >+ Reject all {rejectAll.count}…+ </Button>+ ) : null}+ {onContinue && !rejectAll?.confirming && (+ <Button+ type="button"+ size="small"+ variant="secondary"+ className="ml-auto min-w-0"+ disabled={busy}+ onClick={onContinue}+ >+ Close and continue+ </Button>+ )}+ </div>+ );+}diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ApprovalQueue/components/QueueHeader.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ApprovalQueue/components/QueueHeader.tsxindex 4378325ae3..e893e715fa 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ApprovalQueue/components/QueueHeader.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ApprovalQueue/components/QueueHeader.tsx@@ -5,11 +5,20 @@ interface Props {count: number;mode: string | null;approveAll: { count: number; busy: boolean; onClick: () => void } | null;+ onReviewAll: (() => void) | null;+ // Collapsed after Close and continue: the header alone, and a way back.+ onShow: (() => void) | null;}-export function QueueHeader({ count, mode, approveAll }: Props) {+export function QueueHeader({+ count,+ mode,+ approveAll,+ onReviewAll,+ onShow,+}: Props) {return (- <div className="flex min-h-11 items-center justify-between gap-3 border-b border-zinc-100 bg-zinc-50/60 px-4 py-2">+ <div className="flex min-h-11 flex-wrap items-center justify-between gap-x-3 gap-y-2 border-b border-zinc-100 bg-zinc-50/60 px-4 py-2"><span className="flex items-baseline gap-2 text-sm font-medium text-zinc-900">Waiting for you<span className="tabular-nums text-zinc-500">{count}</span>@@ -18,16 +27,45 @@ export function QueueHeader({ count, mode, approveAll }: Props) {{modeLabel(mode) && (<span className="text-xs text-zinc-500">{modeLabel(mode)}</span>)}- {approveAll && (+ {onShow ? (<Button+ type="button"size="small"variant="secondary"className="min-w-0"- disabled={approveAll.busy}- onClick={approveAll.onClick}+ aria-label="Show waiting cards"+ aria-expanded={false}+ // Close and continue removed the button that had focus.+ autoFocus+ onClick={onShow}>- {approveAllLabel(approveAll.count)}+ Show</Button>+ ) : (+ <>+ {onReviewAll && (+ <Button+ type="button"+ size="small"+ variant="secondary"+ className="min-w-0"+ onClick={onReviewAll}+ >+ Review all+ </Button>+ )}+ {approveAll && (+ <Button+ size="small"+ variant="secondary"+ className="min-w-0"+ disabled={approveAll.busy}+ onClick={approveAll.onClick}+ >+ {approveAllLabel(approveAll.count)}+ </Button>+ )}+ </>)}</div></div>diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ApprovalQueue/components/RejectAllFooter.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ApprovalQueue/components/RejectAllFooter.tsxdeleted file mode 100644index 033571e13a..0000000000--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ApprovalQueue/components/RejectAllFooter.tsx+++ /dev/null@@ -1,59 +0,0 @@-import { Button } from "@/components/atoms/Button/Button";-import { AUTOPILOT_NAME } from "@/components/molecules/AutopilotAvatar/helpers";--interface Props {- count: number;- confirming: boolean;- busy: boolean;- onAsk: () => void;- onCancel: () => void;- onConfirm: () => void;-}--export function RejectAllFooter({- count,- confirming,- busy,- onAsk,- onCancel,- onConfirm,-}: Props) {- return (- <div className="flex flex-wrap items-center gap-2 border-t border-zinc-100 bg-zinc-50/60 px-4 py-2 text-sm">- {confirming ? (- <>- <span role="alert" className="text-zinc-700">- Reject all {count}? {AUTOPILOT_NAME} will be told none of them ran.- </span>- <Button- size="xs"- variant="primary"- className="rounded-full"- disabled={busy}- onClick={onConfirm}- >- Reject all- </Button>- <Button- size="xs"- variant="ghost"- className="rounded-full"- onClick={onCancel}- >- Cancel- </Button>- </>- ) : (- <Button- variant="link"- size="small"- className="h-auto min-w-0 px-0 py-0 text-sm"- disabled={busy}- onClick={onAsk}- >- Reject all {count}…- </Button>- )}- </div>- );-}diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ApprovalQueue/useApprovalQueue.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ApprovalQueue/useApprovalQueue.tsindex 6005cc2cce..18dd344887 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ApprovalQueue/useApprovalQueue.ts+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ApprovalQueue/useApprovalQueue.ts@@ -1,5 +1,10 @@import { useContext, useEffect, useRef, useState } from "react";+import { usePostV2ContinueChatWithAnsweredReviews } from "@/app/api/__generated__/endpoints/executions/executions";+import type { PendingHumanReviewModel } from "@/app/api/__generated__/models/pendingHumanReviewModel";+import { ReviewStatus } from "@/app/api/__generated__/models/reviewStatus";+import { toast } from "@/components/molecules/Toast/use-toast";import { useProcessReviews } from "@/hooks/useProcessReviews";+import type { HeldReceipt } from "@/app/(platform)/home/components/NeedsYou/useHeldReview";import { HeldOutcomesContext } from "../ChatMessagesContainer/HeldOutcomesContext";import { useHeldAnswersStore } from "./heldAnswersStore";import type { CardStatus } from "./components/ApprovalCard/ApprovalCard";@@ -8,34 +13,72 @@ import {type ChatRule,type RuleScope,isHeldRead,+ toApprovalItem,} from "./helpers";-export interface Receipt {+export interface Receipt extends HeldReceipt {item: ApprovalItem;- text: string;}interface Args {items: ApprovalItem[];- onAnswered: () => void;+ answered?: PendingHumanReviewModel[];+ // Whether the chat's turn starts now; false while other cards still wait.+ onAnswered: (turnFollows: boolean) => void;+ onContinued?: () => void;}// How long the box lingers once nothing waits, then how long it fades.const LINGER_MS = 1000;const FADE_MS = 200;-export function useApprovalQueue({ items, onAnswered }: Args) {+export function useApprovalQueue({+ items,+ answered,+ onAnswered,+ onContinued,+}: Args) {const outcomes = useContext(HeldOutcomesContext);const { processReviews } = useProcessReviews();+ const { mutateAsync: continueChat, isPending: isContinuing } =+ usePostV2ContinueChatWithAnsweredReviews();const recordAnswers = useHeldAnswersStore((state) => state.record);const [statuses, setStatuses] = useState<Record<string, CardStatus>>({});const [failed, setFailed] = useState<string[]>([]);const [receipts, setReceipts] = useState<Receipt[]>([]);const [openId, setOpenId] = useState<string | null>(null);const [confirmRejectAll, setConfirmRejectAll] = useState(false);+ const [continued, setContinued] = useState<Set<string>>(new Set());+ // The cards waiting when the box collapsed; a new one opens it again.+ const [collapsedOver, setCollapsedOver] = useState<Set<string> | null>(null);const [leaving, setLeaving] = useState(false);+ // The review dialog lists these receipts, so the box stays while it is open.+ const [reviewing, setReviewing] = useState(false);const seen = useRef(new Map<string, ApprovalItem>());const answeredHere = useRef(new Set<string>());+ const seeded = useRef(false);++ // Answers given before a reload keep their receipts, and with them Close and continue.+ useEffect(() => {+ if (seeded.current || answered === undefined) return;+ seeded.current = true;+ setReceipts((prev) => [+ ...answered+ .filter(+ (row) => !prev.some((r) => r.item.reviewId === row.node_exec_id),+ )+ .map((row) => {+ const item = toApprovalItem(row);+ const approved = row.status === ReviewStatus.APPROVED;+ return {+ item,+ outcome: approved ? ("approved" as const) : ("rejected" as const),+ text: receiptText(item, approved),+ };+ }),+ ...prev,+ ]);+ }, [answered]);// A card that leaves the poll without an answer from here was answered on Home or another tab.useEffect(() => {@@ -47,7 +90,11 @@ export function useApprovalQueue({ items, onAnswered }: Args) {if (gone.length > 0) {setReceipts((prev) => [...prev,- ...gone.map((item) => ({ item, text: "Answered elsewhere" })),+ ...gone.map((item) => ({+ item,+ outcome: "elsewhere" as const,+ text: "Answered elsewhere",+ })),]);}seen.current = new Map(items.map((item) => [item.reviewId, item]));@@ -69,6 +116,7 @@ export function useApprovalQueue({ items, onAnswered }: Args) {}));ids.forEach((id) => answeredHere.current.add(id));let ok = false;+ let turnFollows = true;try {const res = await processReviews(batch.map((item) => ({@@ -78,8 +126,11 @@ export function useApprovalQueue({ items, onAnswered }: Args) {...(approved && rule && scope ? { chat_rule_scope: scope } : {}),})),batch.map((item) => item.scope),+ // This box can continue, so an answer here waits for the chat's others.+ { waitForOtherCards: true },);ok = res.status === 200 && res.data.failed_count === 0;+ turnFollows = !(res.status === 200 && res.data.awaiting_other_cards);} catch {ok = false;}@@ -89,11 +140,12 @@ export function useApprovalQueue({ items, onAnswered }: Args) {...prev,...batch.map((item) => ({item,+ outcome: approved ? ("approved" as const) : ("rejected" as const),text: receiptText(item, approved),})),]);setConfirmRejectAll(false);- onAnswered();+ onAnswered(turnFollows);} else {ids.forEach((id) => answeredHere.current.delete(id));setFailed((prev) => [...prev, ...ids]);@@ -103,6 +155,27 @@ export function useApprovalQueue({ items, onAnswered }: Args) {ids.forEach((id) => delete next[id]);return next;});+ return ok;+ }++ // Starts the turn with what is answered; the rest stay here, still answerable.+ async function continueNow() {+ const chatSessionId = pending[0]?.scope.session_id;+ if (!chatSessionId) return;+ try {+ const res = await continueChat({ chatSessionId });+ if (res.status !== 204) throw new Error(`status ${res.status}`);+ } catch {+ toast({+ title: "Could not continue",+ description: "Try again, or send a message to continue.",+ variant: "destructive",+ });+ return;+ }+ setContinued(new Set(shown.map((r) => r.item.reviewId)));+ setCollapsedOver(new Set(pending.map((item) => item.reviewId)));+ onContinued?.();}const receiptIds = new Set(receipts.map((r) => r.item.reviewId));@@ -117,6 +190,7 @@ export function useApprovalQueue({ items, onAnswered }: Args) {// Once every card is answered and off the server's list, the box leaves; a// card still listed would come back if its receipt went with it.const settled =+ !reviewing &&pending.length === 0 &&shown.length > 0 &&shown.every((r) => !currentIds.has(r.item.reviewId));@@ -139,6 +213,18 @@ export function useApprovalQueue({ items, onAnswered }: Args) {pending,receipts: shown,leaving,+ // Every answer given here, for the review dialog's sidebar.+ answered: receipts,+ setReviewing,+ canContinue:+ pending.length > 0 && shown.some((r) => !continued.has(r.item.reviewId)),+ isContinuing,+ continueNow,+ collapsed:+ collapsedOver !== null &&+ pending.length > 0 &&+ pending.every((item) => collapsedOver.has(item.reviewId)),+ expand: () => setCollapsedOver(null),statusOf: (id: string) => statuses[id] ?? "idle",hasFailed: (id: string) => failed.includes(id),openId,diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ApprovalQueue/useChatReview.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ApprovalQueue/useChatReview.tsnew file mode 100644index 0000000000..376406c85a--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ApprovalQueue/useChatReview.ts@@ -0,0 +1,89 @@+import { useRef } from "react";+import type { HomeAttentionItem } from "@/app/api/__generated__/models/homeAttentionItem";+import type { HomeExpert } from "@/app/api/__generated__/models/homeExpert";+import type { PendingHumanReviewModel } from "@/app/api/__generated__/models/pendingHumanReviewModel";+import type { AttentionListRow } from "@/app/(platform)/home/components/NeedsYou/useHeldReview";+import { useReviewCarousel } from "@/app/(platform)/home/components/NeedsYou/useReviewCarousel";+import { type ChatRule, type RuleScope, toApprovalItem } from "./helpers";+import type { Receipt, useApprovalQueue } from "./useApprovalQueue";++interface Args {+ queue: ReturnType<typeof useApprovalQueue>;+ // The chat's waiting cards, oldest first.+ reviews: PendingHumanReviewModel[];+ expert: HomeExpert | null;+}++// Home's review dialog over this chat's queue, so the box and the dialog share one set of answers.+export function useChatReview({ queue, reviews, expert }: Args) {+ const seen = useRef(new Map<string, PendingHumanReviewModel>());+ const answeredHere = useRef(0);+ for (const review of reviews) seen.current.set(review.node_exec_id, review);++ const waiting = new Set(reviews.map((review) => review.node_exec_id));+ const receipts = new Map(queue.answered.map((r) => [r.item.reviewId, r]));+ const rows: AttentionListRow[] = [...seen.current.values()]+ .filter((r) => waiting.has(r.node_exec_id) || receipts.has(r.node_exec_id))+ .map((review) => ({+ item: toAttentionItem(review, expert),+ receipt: heldReceipt(receipts.get(review.node_exec_id)),+ }));++ const held = {+ statusOf: queue.statusOf,+ hasFailed: queue.hasFailed,+ async decide(+ batch: HomeAttentionItem[],+ approved: boolean,+ { rule, scope }: { rule?: ChatRule; scope?: RuleScope } = {},+ ) {+ const items = batch.map((item) => toApprovalItem(item.review!));+ if (!(await queue.answer(items, approved, rule, scope))) return [];+ answeredHere.current += batch.length;+ return batch.map((item) => item.id);+ },+ };+ const carousel = useReviewCarousel({ rows, held });++ function open() {+ const first = rows.find((row) => !row.receipt);+ if (!first) return;+ answeredHere.current = 0;+ queue.setReviewing(true);+ carousel.openAt(first.item.id);+ }++ // Closing part-way is Close and continue; the last answer already started the turn.+ function close() {+ carousel.close();+ queue.setReviewing(false);+ if (answeredHere.current > 0 && queue.pending.length > 0)+ void queue.continueNow();+ }++ return { held, carousel: { ...carousel, close }, open };+}++function toAttentionItem(+ review: PendingHumanReviewModel,+ expert: HomeExpert | null,+): HomeAttentionItem {+ const { headline } = toApprovalItem(review);+ return {+ id: review.node_exec_id,+ kind: "approval",+ priority: "normal",+ title: headline.ask,+ headline,+ description: "",+ why_it_matters: "",+ expert,+ created_at: review.created_at,+ review,+ primary_action: { label: "", href: "" },+ };+}++function heldReceipt(receipt: Receipt | undefined) {+ return receipt ? { outcome: receipt.outcome, text: receipt.text } : null;+}diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/ChatMessagesContainer.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/ChatMessagesContainer.tsxindex 796021d293..0ddde2e36c 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/ChatMessagesContainer.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/ChatMessagesContainer.tsx@@ -624,6 +624,7 @@ export function ChatMessagesContainer({pollWhileEmpty={reviewTarget?.kind === "chat"}refetchKey={countHeldCalls(messages)}expertName={expertIdentity?.name ?? null}+ expert={expertIdentity ?? null}/>)}{!readOnly &&diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/__tests__/ChatMessagesContainer.test.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/__tests__/ChatMessagesContainer.test.tsxindex 9ca31a4570..cdc88fd5b0 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/__tests__/ChatMessagesContainer.test.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/__tests__/ChatMessagesContainer.test.tsx@@ -1782,6 +1782,7 @@ describe("ChatMessagesContainer — pending reviews", () => {pollWhileEmpty,refetchKey,expertName: null,+ expert: null,});afterEach(() => {diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/CopilotPendingReviews/CopilotPendingReviews.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/CopilotPendingReviews/CopilotPendingReviews.tsxindex 50f05c9fc4..6c9cff6548 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/CopilotPendingReviews/CopilotPendingReviews.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/CopilotPendingReviews/CopilotPendingReviews.tsx@@ -7,6 +7,7 @@ import {} from "@/components/organisms/PendingReviewsList/PendingReviewsList";import { useCopilotChatActions } from "../CopilotChatActionsProvider/useCopilotChatActions";import { okData } from "@/app/api/helpers";+import type { ExpertIdentity } from "../../useExpertMap";import { ApprovalQueue } from "../ApprovalQueue/ApprovalQueue";import { isGateReview, toApprovalItem } from "../ApprovalQueue/helpers";import { useCopilotPendingReviews } from "./useCopilotPendingReviews";@@ -18,6 +19,8 @@ type Props =pollWhileEmpty?: boolean;refetchKey?: number;expertName?: string | null;+ // Its avatar in the review dialog.+ expert?: ExpertIdentity | null;};/**@@ -29,19 +32,20 @@ export function CopilotPendingReviews(props: Props) {const { onSend, onBackendTurn } = useCopilotChatActions();const graphExecId = "graphExecId" in props ? props.graphExecId : "";const expertName = "expertName" in props ? props.expertName : null;- const { pendingReviews, refetch } = useCopilotPendingReviews(props);+ const expert = ("expert" in props ? props.expert : null) ?? null;+ const { pendingReviews, answeredReviews, refetch } =+ useCopilotPendingReviews(props);- const heldCalls = pendingReviews+ const heldReviews = pendingReviews.filter(isGateReview)- .sort((a, b) => +new Date(a.created_at) - +new Date(b.created_at))- .map(toApprovalItem);+ .sort((a, b) => +new Date(a.created_at) - +new Date(b.created_at));const otherReviews = pendingReviews.filter((r) => !r.node_exec_id.startsWith(COPILOT_GATE_NODE_PREFIX),);- async function handleHeldCallAnswered() {+ async function handleHeldCallAnswered(turnFollows: boolean) {await refetch();- onBackendTurn?.();+ if (turnFollows) onBackendTurn?.();}const handleReviewComplete = useCallback(async () => {@@ -73,9 +77,22 @@ export function CopilotPendingReviews(props: Props) {return (<div className="flex flex-col gap-2 py-2 empty:hidden"><ApprovalQueue- items={heldCalls}+ items={heldReviews.map(toApprovalItem)}+ reviews={heldReviews}+ answered={answeredReviews}expertName={expertName}+ expert={+ expert+ ? {+ id: expert.id,+ name: expert.name,+ role: expert.role ?? "",+ avatar_url: expert.avatarUrl,+ }+ : null+ }onAnswered={handleHeldCallAnswered}+ onContinued={() => onBackendTurn?.()}/>{otherReviews.length > 0 && (<PendingReviewsListdiff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/CopilotPendingReviews/__tests__/CopilotPendingReviews.batch.test.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/CopilotPendingReviews/__tests__/CopilotPendingReviews.batch.test.tsxnew file mode 100644index 0000000000..14abc60163--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/CopilotPendingReviews/__tests__/CopilotPendingReviews.batch.test.tsx@@ -0,0 +1,226 @@+import userEvent from "@testing-library/user-event";+import { http, HttpResponse } from "msw";+import { expect, test, vi } from "vitest";+import type { PendingHumanReviewModel } from "@/app/api/__generated__/models/pendingHumanReviewModel";+import { server } from "@/mocks/mock-server";+import {+ render,+ screen,+ waitFor,+ within,+} from "@/tests/integrations/test-utils";+import { CHAT_SESSION, folder } from "../../ApprovalQueue/__tests__/fixtures";+import { CopilotChatActionsProvider } from "../../CopilotChatActionsProvider/CopilotChatActionsProvider";+import { CopilotPendingReviews } from "../CopilotPendingReviews";++test("answers on an idle chat follow no turn until the last card, which follows one", async () => {+ const chat = serveChat([folder("a", "Alpha"), folder("b", "Beta")]);+ const onBackendTurn = renderChat();+ const box = await screen.findByRole("region", { name: "Waiting for you" });++ await userEvent.click(+ within(box).getAllByRole("button", { name: "Approve" })[0],+ );+ await within(box).findByText("· Approved");+ expect(onBackendTurn).not.toHaveBeenCalled();++ await userEvent.click(within(box).getByRole("button", { name: "Approve" }));++ await waitFor(() => expect(onBackendTurn).toHaveBeenCalledTimes(1));+ expect(chat.answered).toEqual([[id("a")], [id("b")]]);+ expect(chat.waited).toEqual([true, true]);+ expect(chat.continues).toBe(0);+});++test("Close and continue starts the turn with what is answered and folds the box to its header", async () => {+ const chat = serveChat([folder("a", "Alpha"), folder("b", "Beta")]);+ const onBackendTurn = renderChat();+ const box = await screen.findByRole("region", { name: "Waiting for you" });+ expect(+ within(box).queryByRole("button", { name: "Close and continue" }),+ ).toBeNull();++ await userEvent.click(+ within(box).getAllByRole("button", { name: "Approve" })[0],+ );+ await userEvent.click(+ await within(box).findByRole("button", { name: "Close and continue" }),+ );++ await waitFor(() => expect(onBackendTurn).toHaveBeenCalledTimes(1));+ expect(chat.continues).toBe(1);+ const show = await within(box).findByRole("button", {+ name: "Show waiting cards",+ });+ await waitFor(() => expect(document.activeElement).toBe(show));+ expect(within(box).queryByRole("button", { name: "Approve" })).toBeNull();++ await userEvent.click(+ within(box).getByRole("button", { name: "Show waiting cards" }),+ );+ expect(within(box).getByRole("button", { name: "Approve" })).toBeDefined();+ expect(+ within(box).queryByRole("button", { name: "Close and continue" }),+ ).toBeNull();+});++test("an answer waiting on the chat's other cards keeps Close and continue after a reload", async () => {+ const chat = serveChat([folder("a", "Alpha"), folder("b", "Beta")]);+ const first = mountChat();+ const box = await screen.findByRole("region", { name: "Waiting for you" });+ await userEvent.click(+ within(box).getAllByRole("button", { name: "Approve" })[0],+ );+ await within(box).findByRole("button", { name: "Close and continue" });+ first.unmount();++ mountChat();+ const reloaded = await screen.findByRole("region", {+ name: "Waiting for you",+ });+ await within(reloaded).findByText("· Approved");+ await userEvent.click(+ await within(reloaded).findByRole("button", { name: "Close and continue" }),+ );++ await waitFor(() => expect(chat.continues).toBe(1));+ expect(chat.answered).toEqual([[id("a")]]);+});++test("the review dialog's last verdict follows the turn at once, and closing it sends nothing more", async () => {+ const user = userEvent.setup();+ const chat = serveChat([folder("a", "Alpha"), folder("b", "Beta")]);+ const onBackendTurn = renderChat();+ const dialog = await openReviewAll(user);++ await user.click(within(dialog).getByRole("button", { name: "Approve" }));+ await waitFor(() =>+ expect(heading(dialog)).toBe("Create library folder Beta"),+ );+ expect(onBackendTurn).not.toHaveBeenCalled();+ await user.click(within(dialog).getByRole("button", { name: "Approve" }));++ await waitFor(() => expect(onBackendTurn).toHaveBeenCalledTimes(1));+ await user.click(await within(dialog).findByRole("button", { name: "Done" }));+ expect(chat.answered).toEqual([[id("a")], [id("b")]]);+ expect(chat.continues).toBe(0);+});++test("closing the review dialog part-way continues with what it answered", async () => {+ const user = userEvent.setup();+ const chat = serveChat([folder("a", "Alpha"), folder("b", "Beta")]);+ const onBackendTurn = renderChat();+ const dialog = await openReviewAll(user);++ await user.click(within(dialog).getByRole("button", { name: "Approve" }));+ await waitFor(() =>+ expect(heading(dialog)).toBe("Create library folder Beta"),+ );+ await user.keyboard("{Escape}");++ await waitFor(() => expect(chat.continues).toBe(1));+ expect(onBackendTurn).toHaveBeenCalledTimes(1);+ expect(chat.answered).toEqual([[id("a")]]);+});++test("closing the review dialog with nothing answered sends nothing", async () => {+ const user = userEvent.setup();+ const chat = serveChat([folder("a", "Alpha"), folder("b", "Beta")]);+ const onBackendTurn = renderChat();+ await openReviewAll(user);++ await user.keyboard("{Escape}");++ await waitFor(() => expect(screen.queryByRole("dialog")).toBeNull());+ expect(chat.answered).toEqual([]);+ expect(chat.continues).toBe(0);+ expect(onBackendTurn).not.toHaveBeenCalled();+});++// The review endpoints as the server keeps them: an answered card leaves the+// chat's queue, and an answer says when it waits for the others.+function serveChat(reviews: PendingHumanReviewModel[]) {+ const waiting = new Map(reviews.map((r) => [r.node_exec_id, r]));+ // Answered cards whose calls have not run, until a turn runs them.+ const unrun = new Map<string, PendingHumanReviewModel>();+ const chat = {+ answered: [] as string[][],+ waited: [] as (boolean | undefined)[],+ continues: 0,+ };+ server.use(+ http.get(`*/api/review/session/${CHAT_SESSION}`, () =>+ HttpResponse.json([...waiting.values()]),+ ),+ http.post("*/api/review/action", async ({ request }) => {+ const body = (await request.json()) as {+ reviews: { node_exec_id: string; approved: boolean }[];+ wait_for_other_cards?: boolean;+ };+ const ids = body.reviews.map((r) => r.node_exec_id);+ chat.waited.push(body.wait_for_other_cards);+ body.reviews.forEach(({ node_exec_id, approved }) => {+ const row = waiting.get(node_exec_id);+ if (row)+ unrun.set(node_exec_id, {+ ...row,+ status: approved ? "APPROVED" : "REJECTED",+ });+ });+ ids.forEach((reviewId) => waiting.delete(reviewId));+ chat.answered.push(ids);+ return HttpResponse.json({+ approved_count: ids.length,+ rejected_count: 0,+ failed_count: 0,+ awaiting_other_cards: waiting.size > 0,+ });+ }),+ http.get(`*/api/review/session/${CHAT_SESSION}/answered`, () =>+ HttpResponse.json([...unrun.values()]),+ ),+ http.post(`*/api/review/session/${CHAT_SESSION}/continue`, () => {+ chat.continues += 1;+ unrun.clear();+ return new HttpResponse(null, { status: 204 });+ }),+ );+ return chat;+}++function mountChat() {+ return render(+ <CopilotChatActionsProvider onSend={vi.fn()} onBackendTurn={vi.fn()}>+ <CopilotPendingReviews chatSessionId={CHAT_SESSION} />+ </CopilotChatActionsProvider>,+ );+}++function renderChat() {+ const onBackendTurn = vi.fn();+ render(+ <CopilotChatActionsProvider onSend={vi.fn()} onBackendTurn={onBackendTurn}>+ <CopilotPendingReviews chatSessionId={CHAT_SESSION} />+ </CopilotChatActionsProvider>,+ );+ return onBackendTurn;+}++async function openReviewAll(user: ReturnType<typeof userEvent.setup>) {+ await user.click(await screen.findByRole("button", { name: "Review all" }));+ const dialog = await screen.findByRole("dialog");+ await waitFor(() =>+ expect(heading(dialog)).toBe("Create library folder Alpha"),+ );+ return dialog;+}++function heading(dialog: HTMLElement) {+ return within(dialog)+ .getByRole("region", { name: "Held call" })+ .querySelector("h3")?.textContent;+}++function id(reviewId: string) {+ return folder(reviewId, "").node_exec_id;+}diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/CopilotPendingReviews/useCopilotPendingReviews.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/components/CopilotPendingReviews/useCopilotPendingReviews.tsindex 63c0c78f2c..711dd54a0e 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/CopilotPendingReviews/useCopilotPendingReviews.ts+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/CopilotPendingReviews/useCopilotPendingReviews.ts@@ -1,4 +1,5 @@import { useEffect, useState } from "react";+import { useGetV2GetAnsweredReviewsWaitingToRunForChatSession } from "@/app/api/__generated__/endpoints/executions/executions";import { useGetV1GetExecutionDetails } from "@/app/api/__generated__/endpoints/graphs/graphs";import { AgentExecutionStatus } from "@/app/api/__generated__/models/agentExecutionStatus";import { okData } from "@/app/api/helpers";@@ -64,6 +65,17 @@ export function useCopilotPendingReviews({: false,});const { pendingReviews, refetch } = chatSessionId ? forChat : forRun;+ // Fresh on every mount, never cached across one: the box seeds its receipts+ // from the first answer, so a stale list would seed the wrong ones.+ const { data: answeredReviews } =+ useGetV2GetAnsweredReviewsWaitingToRunForChatSession(chatSessionId, {+ query: {+ enabled: !!chatSessionId,+ select: okData,+ gcTime: 0,+ refetchOnWindowFocus: false,+ },+ });useEffect(() => {setHasRows(pendingReviews.length > 0);@@ -77,7 +89,7 @@ export function useCopilotPendingReviews({if (status) refetch();}, [status, refetch]);- return { pendingReviews, refetch };+ return { pendingReviews, answeredReviews, refetch };}function readStatus(raw: unknown) {diff --git a/autogpt_platform/frontend/src/app/(platform)/home/__tests__/held-calls.test.tsx b/autogpt_platform/frontend/src/app/(platform)/home/__tests__/held-calls.test.tsxindex 09912e80a5..1f913f1c1c 100644--- a/autogpt_platform/frontend/src/app/(platform)/home/__tests__/held-calls.test.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/home/__tests__/held-calls.test.tsx@@ -60,6 +60,8 @@ test("a bare held call is approved from its row", async () => {chat_rule: null,},]);+ // Home cannot continue a chat, so its answer never waits for the chat's others.+ expect("wait_for_other_cards" in requests[0]).toBe(false);});// A folder whose colour the headline does not name, and an email that can't be undone.diff --git a/autogpt_platform/frontend/src/app/(platform)/home/components/NeedsYou/components/HeldReviewDialog.tsx b/autogpt_platform/frontend/src/app/(platform)/home/components/NeedsYou/components/HeldReviewDialog.tsxindex dc8e688cf4..4de2f6af6e 100644--- a/autogpt_platform/frontend/src/app/(platform)/home/components/NeedsYou/components/HeldReviewDialog.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/home/components/NeedsYou/components/HeldReviewDialog.tsx@@ -28,11 +28,13 @@ import { ReviewSidebar } from "./ReviewSidebar";interface Props {carousel: ReturnType<typeof useReviewCarousel>;- held: ReturnType<typeof useHeldReview>;+ held: Pick<ReturnType<typeof useHeldReview>, "statusOf" | "hasFailed">;+ // Off inside the chat the calls came from.+ chatLink?: boolean;}// Every held call on the page, one at a time: the chat's card, a sidebar, and ‹ › to move.-export function HeldReviewDialog({ carousel, held }: Props) {+export function HeldReviewDialog({ carousel, held, chatLink = true }: Props) {const { current } = carousel;const approval = current ? toApprovalItem(current.item.review!) : null;@@ -169,12 +171,14 @@ export function HeldReviewDialog({ carousel, held }: Props) {}onReject={() => carousel.decide(current.item, false)}aside={- <Link- href={current.item.primary_action.href}- className="rounded-sm text-sm font-medium text-zinc-800 underline underline-offset-4 hover:text-zinc-950 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-zinc-300"- >- Open chat- </Link>+ chatLink ? (+ <Link+ href={current.item.primary_action.href}+ className="rounded-sm text-sm font-medium text-zinc-800 underline underline-offset-4 hover:text-zinc-950 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-zinc-300"+ >+ Open chat+ </Link>+ ) : null}/></PaneFooter>diff --git a/autogpt_platform/frontend/src/app/(platform)/home/components/NeedsYou/useReviewCarousel.ts b/autogpt_platform/frontend/src/app/(platform)/home/components/NeedsYou/useReviewCarousel.tsindex 171f2b9244..e3460e7cba 100644--- a/autogpt_platform/frontend/src/app/(platform)/home/components/NeedsYou/useReviewCarousel.ts+++ b/autogpt_platform/frontend/src/app/(platform)/home/components/NeedsYou/useReviewCarousel.ts@@ -9,7 +9,7 @@ import type { AttentionListRow, useHeldReview } from "./useHeldReview";interface Args {rows: AttentionListRow[];- held: ReturnType<typeof useHeldReview>;+ held: Pick<ReturnType<typeof useHeldReview>, "decide">;}// One dialog over every held call: the current one is kept by review id across the poll.diff --git a/autogpt_platform/frontend/src/app/api/openapi.json b/autogpt_platform/frontend/src/app/api/openapi.jsonindex 40b26705a4..5c01fbf8eb 100644--- a/autogpt_platform/frontend/src/app/api/openapi.json+++ b/autogpt_platform/frontend/src/app/api/openapi.json@@ -14640,6 +14640,81 @@}}},+ "/api/review/session/{chat_session_id}/answered": {+ "get": {+ "tags": ["v2", "executions", "review", "v2", "executions", "review"],+ "summary": "Get Answered Reviews Waiting to Run for Chat Session",+ "description": "The chat's answered cards whose calls have not run yet, oldest first, so\nits box still shows them as answered, with Close and continue, after a reload.",+ "operationId": "getV2Get answered reviews waiting to run for chat session",+ "security": [{ "HTTPBearerJWT": [] }],+ "parameters": [+ {+ "name": "chat_session_id",+ "in": "path",+ "required": true,+ "schema": { "type": "string", "title": "Chat Session Id" }+ }+ ],+ "responses": {+ "200": {+ "description": "Successful Response",+ "content": {+ "application/json": {+ "schema": {+ "type": "array",+ "items": {+ "$ref": "#/components/schemas/PendingHumanReviewModel"+ },+ "title": "Response Getv2Get Answered Reviews Waiting To Run For Chat Session"+ }+ }+ }+ },+ "401": {+ "$ref": "#/components/responses/HTTP401NotAuthenticatedError"+ },+ "422": {+ "description": "Validation Error",+ "content": {+ "application/json": {+ "schema": { "$ref": "#/components/schemas/HTTPValidationError" }+ }+ }+ }+ }+ }+ },+ "/api/review/session/{chat_session_id}/continue": {+ "post": {+ "tags": ["v2", "executions", "review", "v2", "executions", "review"],+ "summary": "Continue Chat With Answered Reviews",+ "description": "Start the chat's turn with the cards answered so far; the rest stay open\nand the turn is told how many still wait.",+ "operationId": "postV2Continue chat with answered reviews",+ "security": [{ "HTTPBearerJWT": [] }],+ "parameters": [+ {+ "name": "chat_session_id",+ "in": "path",+ "required": true,+ "schema": { "type": "string", "title": "Chat Session Id" }+ }+ ],+ "responses": {+ "204": { "description": "Successful Response" },+ "401": {+ "$ref": "#/components/responses/HTTP401NotAuthenticatedError"+ },+ "422": {+ "description": "Validation Error",+ "content": {+ "application/json": {+ "schema": { "$ref": "#/components/schemas/HTTPValidationError" }+ }+ }+ }+ }+ }+ },"/api/schedules": {"get": {"tags": ["v1", "schedules"],@@ -31437,6 +31512,12 @@"type": "array","title": "Reviews","description": "All reviews with their approval status, data, and messages"+ },+ "wait_for_other_cards": {+ "type": "boolean",+ "title": "Wait For Other Cards",+ "description": "Set by a chat's own queue, which can continue: no turn starts while other cards in the chat still wait",+ "default": false}},"type": "object",@@ -31465,6 +31546,12 @@"anyOf": [{ "type": "string" }, { "type": "null" }],"title": "Error","description": "Error message if operation failed"+ },+ "awaiting_other_cards": {+ "type": "boolean",+ "title": "Awaiting Other Cards",+ "description": "The answers start no chat turn yet: other cards in the chat still wait",+ "default": false}},"type": "object",diff --git a/autogpt_platform/frontend/src/hooks/useProcessReviews.ts b/autogpt_platform/frontend/src/hooks/useProcessReviews.tsindex 749f1b0340..a6aa122ec0 100644--- a/autogpt_platform/frontend/src/hooks/useProcessReviews.ts+++ b/autogpt_platform/frontend/src/hooks/useProcessReviews.ts@@ -19,9 +19,18 @@ export function useProcessReviews({const queryClient = useQueryClient();const { mutateAsync, isPending } = usePostV2ProcessReviewAction();- async function processReviews(items: ReviewItem[], scopes: ReviewScope[]) {+ async function processReviews(+ items: ReviewItem[],+ scopes: ReviewScope[],+ { waitForOtherCards = false }: { waitForOtherCards?: boolean } = {},+ ) {try {- return await mutateAsync({ data: { reviews: items } });+ return await mutateAsync({+ data: {+ reviews: items,+ ...(waitForOtherCards ? { wait_for_other_cards: true } : {}),+ },+ });} finally {// Awaited so callers can keep a row locked until the refetch settles;// firing and forgetting leaves React Query serving the just-acted-ondiff --git a/autogpt_platform/frontend/src/mocks/mock-handlers.ts b/autogpt_platform/frontend/src/mocks/mock-handlers.tsindex 5c8c96ca17..da4e9bc4c7 100644--- a/autogpt_platform/frontend/src/mocks/mock-handlers.ts+++ b/autogpt_platform/frontend/src/mocks/mock-handlers.ts@@ -17,7 +17,10 @@ import { getCreditsMock } from "@/app/api/__generated__/endpoints/credits/creditimport { getDefaultMock } from "@/app/api/__generated__/endpoints/default/default.msw";import { getEmailMock } from "@/app/api/__generated__/endpoints/email/email.msw";import { getGetFeatureFlagsMockHandler } from "@/app/api/__generated__/endpoints/feature-flags/feature-flags.msw";-import { getExecutionsMock } from "@/app/api/__generated__/endpoints/executions/executions.msw";+import {+ getExecutionsMock,+ getGetV2GetAnsweredReviewsWaitingToRunForChatSessionMockHandler,+} from "@/app/api/__generated__/endpoints/executions/executions.msw";import { getExpertsMock } from "@/app/api/__generated__/endpoints/experts/experts.msw";import { getFilesMock } from "@/app/api/__generated__/endpoints/files/files.msw";import { getGraphsMock } from "@/app/api/__generated__/endpoints/graphs/graphs.msw";@@ -72,6 +75,9 @@ export const mockHandlers = [...getCreditsMock(),...getDefaultMock(),...getEmailMock(),+ // No answers waiting to run unless a test sets them up; random rows would+ // seed receipts into every chat's approval box.+ getGetV2GetAnsweredReviewsWaitingToRunForChatSessionMockHandler([]),...getExecutionsMock(),...getExpertsMock(),...getFilesMock(),