Skip to content

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.

feat(platform): AutoPilot carries on while a held call awaits review: a queue of cards per chat, late results on approval (#14832)

Reinier van der Leerd0c8aae6d651
diff --git a/autogpt_platform/backend/backend/api/features/chat/routes.py b/autogpt_platform/backend/backend/api/features/chat/routes.py
index 979a052cee..3ba0da4d3f 100644
--- a/autogpt_platform/backend/backend/api/features/chat/routes.py
+++ b/autogpt_platform/backend/backend/api/features/chat/routes.py
@@ -1636,7 +1636,7 @@ async def cancel_session_task(
# the "assistant encountered an error" banner over their own cancel.
await stream_registry.mark_session_completed(
session_id,
- error_message="Operation cancelled",
+ error_message=stream_registry.CANCELLED_MESSAGE,
skip_error_publish=True,
)
# Status is now force-flipped out of "running"; re-clear to drop any
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.py
index 2e9f2b6906..c3365d8a24 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
@@ -1370,3 +1370,52 @@ async def test_process_review_action_per_review_auto_approve_granularity(
assert "node_def_node_1_auto" in node_ids_with_auto_approval
assert "node_def_node_3_auto" in node_ids_with_auto_approval
assert "node_def_node_2_manual" not in node_ids_with_auto_approval
+
+
[email protected](loop_scope="session")
+ "graph_exec_id, session_id, woken",
+ [(None, "s1", "s1"), ("test_graph_exec_456", None, None)],
+)
+async def test_an_answer_on_a_chat_card_wakes_that_chat(
+ client: httpx.AsyncClient,
+ mocker: pytest_mock.MockerFixture,
+ sample_pending_review: PendingHumanReviewModel,
+ test_user_id: str,
+ graph_exec_id: str | None,
+ session_id: str | None,
+ woken: str | None,
+) -> None:
+ """A held call finishes on the chat's next turn; nothing else starts one."""
+ review = sample_pending_review.model_copy(
+ update={"graph_exec_id": graph_exec_id, "session_id": session_id}
+ )
+ routes = "backend.api.features.graph_executions.review.routes"
+ mocker.patch(
+ f"{routes}.get_reviews_by_node_exec_ids",
+ return_value={"test_node_123": review},
+ )
+ meta = mocker.Mock()
+ meta.status = ExecutionStatus.REVIEW
+ mocker.patch(f"{routes}.get_graph_execution_meta", return_value=meta)
+ mocker.patch(
+ f"{routes}.process_all_reviews_for_execution",
+ return_value={
+ "test_node_123": review.model_copy(update={"status": ReviewStatus.APPROVED})
+ },
+ )
+ mocker.patch(f"{routes}.has_pending_reviews_for_graph_exec", return_value=True)
+ wake = mocker.patch(f"{routes}.wake_for_held_calls")
+
+ response = await client.post(
+ "/api/review/action",
+ json={"reviews": [{"node_exec_id": "test_node_123", "approved": True}]},
+ )
+
+ assert response.status_code == 200
+ if woken:
+ 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"]
+ else:
+ wake.assert_not_called()
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.py
index 84a3d711ea..ae3fd8a51a 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
@@ -7,6 +7,7 @@ from fastapi import APIRouter, HTTPException, Query, Security, status
from prisma.enums import ReviewStatus
from backend.copilot.constants import legacy_chat_session_id, parse_node_id_from_exec_id
+from backend.copilot.gate.held import wake as wake_for_held_calls
from backend.data.execution import (
ExecutionContext,
ExecutionStatus,
@@ -220,7 +221,7 @@ async def process_review_action(
detail="All reviews in a single request must belong to the same execution.",
)
- graph_exec_id, _ = next(iter(scopes))
+ graph_exec_id, chat_session_id = next(iter(scopes))
# Validate execution status for graph executions; a chat has none
if graph_exec_id is not None:
@@ -344,6 +345,10 @@ async def process_review_action(
if review.status == ReviewStatus.REJECTED
)
+ # A held call finishes on its own: the answer starts the chat's next turn.
+ if chat_session_id is not None and updated_reviews:
+ await wake_for_held_calls(user_id, chat_session_id, updated_reviews.values())
+
# Resume graph execution only for real graph executions; a chat is resumed
# by the LLM calling resume_capability with the review_id
if graph_exec_id is not None and updated_reviews:
diff --git a/autogpt_platform/backend/backend/copilot/baseline/service.py b/autogpt_platform/backend/backend/copilot/baseline/service.py
index 7a90547991..4c7930c582 100644
--- a/autogpt_platform/backend/backend/copilot/baseline/service.py
+++ b/autogpt_platform/backend/backend/copilot/baseline/service.py
@@ -51,6 +51,7 @@ from backend.copilot.context import get_workspace_manager, set_execution_context
from backend.copilot.expert_context import build_expert_identity_suffix
from backend.copilot.expert_kickoff import is_expert_kickoff_turn
from backend.copilot.gate import active_mode
+from backend.copilot.gate.held import resolve_answered
from backend.copilot.graphiti.config import is_enabled_for_user
from backend.copilot.graphiti.context import fetch_warm_context
from backend.copilot.graphiti.ingest import enqueue_conversation_turn
@@ -2333,6 +2334,16 @@ async def stream_chat_completion_baseline(
# From here the finally below always runs, so the turn can be counted.
await count_expert_turn(session_id, session.expert_id)
+ # 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]"
+ ):
+ openai_messages.extend(
+ format_pending_as_user_message(pm) for pm in held_results
+ )
+
# Propagate user/session context to Langfuse so all LLM calls within
# this request are grouped under a single trace with proper attribution.
_trace_ctx: Any = None
diff --git a/autogpt_platform/backend/backend/copilot/executor/processor.py b/autogpt_platform/backend/backend/copilot/executor/processor.py
index ce27730a95..cbfe6c942b 100644
--- a/autogpt_platform/backend/backend/copilot/executor/processor.py
+++ b/autogpt_platform/backend/backend/copilot/executor/processor.py
@@ -782,7 +782,7 @@ class CoPilotProcessor:
# Handle all exceptions (including CancelledError) with appropriate logging
if isinstance(e, asyncio.CancelledError):
log.info("Turn cancelled")
- error_msg = "Operation cancelled"
+ error_msg = stream_registry.CANCELLED_MESSAGE
else:
error_msg = str(e) or type(e).__name__
log.error(f"Turn failed: {error_msg}")
@@ -790,7 +790,7 @@ class CoPilotProcessor:
finally:
# If no exception but user cancelled, still mark as cancelled
if not error_msg and cancel.is_set():
- error_msg = "Operation cancelled"
+ error_msg = stream_registry.CANCELLED_MESSAGE
try:
if credential_lease is not None:
try:
diff --git a/autogpt_platform/backend/backend/copilot/gate/__init__.py b/autogpt_platform/backend/backend/copilot/gate/__init__.py
index 445c6e24c2..449a6464c3 100644
--- a/autogpt_platform/backend/backend/copilot/gate/__init__.py
+++ b/autogpt_platform/backend/backend/copilot/gate/__init__.py
@@ -20,7 +20,7 @@ from pydantic import BaseModel, ConfigDict
from backend.copilot.model import ChatSession
from backend.util.feature_flag import Flag, is_feature_enabled
-from . import chat_rules
+from . import chat_rules, held
from . import review as review_store
from .classifier import classify
from .policy import (
@@ -34,10 +34,7 @@ from .policy import (
logger = logging.getLogger(__name__)
-_ALREADY_WAITING = (
- "Another action in this chat is already waiting for the user's approval. "
- "Stop and let them answer that one first; do not retry or try another way."
-)
+_ALREADY_HELD = "This exact call is already waiting for the user's approval."
_CONSUMED = (
"This approval was already used by an identical call that ran. "
"Do not retry; tell the user what ran."
@@ -64,7 +61,6 @@ class Decision(BaseModel):
allowed: bool
reason: str = ""
review_id: str | None = None
- already_waiting: bool = False
ALLOW = Decision(allowed=True)
@@ -94,6 +90,7 @@ async def check_action(
args: dict[str, Any],
user_id: str | None,
session: ChatSession,
+ tool_call_id: str = "",
) -> Decision:
if not await gate_active(user_id, session):
return ALLOW
@@ -116,6 +113,10 @@ async def check_action(
await review_store.consume(review_id, user_id)
await chat_rules.set_ask(session_id, tool_name)
return Decision(allowed=False, reason=_REJECTED)
+ if status == ReviewStatus.WAITING:
+ # The first call's card and stored call stand; re-storing would
+ # re-point the late result at the retry's tool call id.
+ return Decision(allowed=False, reason=_ALREADY_HELD, review_id=review_id)
mode = resolve_mode(session)
verdict = verdict_for(mode, tool_name)
@@ -133,27 +134,39 @@ async def check_action(
)
if allowed:
return ALLOW
- return await _park(review_id, user_id, session, tool_name, args, reason)
+ call = held.HeldCall(
+ review_id=review_id, tool_name=tool_name, tool_call_id=tool_call_id, args=args
+ )
+ return await _park(call, user_id, session, reason)
async def _park(
- review_id: str,
- user_id: str,
- session: ChatSession,
- tool_name: str,
- args: dict[str, Any],
- reason: str,
+ call: held.HeldCall, user_id: str, session: ChatSession, reason: str
) -> Decision:
- waiting = await review_store.has_open_review(user_id, session.session_id)
- if waiting is None:
+ """Cards queue per chat: the call is kept so its answer can finish it."""
+ if not await held.remember(session.session_id, call):
return Decision(allowed=False, reason=_UNRECORDABLE)
- if waiting:
- return Decision(allowed=False, reason=_ALREADY_WAITING, already_waiting=True)
if not await review_store.open_review(
- review_id, user_id, session, tool_name, args, reason
+ call.review_id, user_id, session, call.tool_name, call.args, reason
):
+ await held.forget(session.session_id, call.review_id)
return Decision(allowed=False, reason=_UNRECORDABLE)
- return Decision(allowed=False, reason=reason, review_id=review_id)
+ return Decision(allowed=False, reason=reason, review_id=call.review_id)
+
+
+def refusal_message(reason: str, review_id: str | None) -> str:
+ """What the model reads instead of a result."""
+ if review_id is None:
+ return (
+ f"Nothing ran. {reason} Tell the user what you wanted to do and why, "
+ "then stop. Do not retry or reach the same effect another way."
+ )
+ return (
+ f"Held for the user's approval (review {review_id}); nothing has run "
+ f"yet. {reason} If they approve, the result arrives later as a "
+ "<held_call_result> naming this call. Carry on with whatever does not "
+ "depend on it. Do not retry it or reach the same effect another way."
+ )
def _last_user_message(session: ChatSession) -> str:
@@ -163,4 +176,11 @@ def _last_user_message(session: ChatSession) -> str:
return ""
-__all__ = ["Decision", "active_mode", "check_action", "gate_active", "resolve_mode"]
+__all__ = [
+ "Decision",
+ "active_mode",
+ "check_action",
+ "gate_active",
+ "refusal_message",
+ "resolve_mode",
+]
diff --git a/autogpt_platform/backend/backend/copilot/gate/gate_test.py b/autogpt_platform/backend/backend/copilot/gate/gate_test.py
index 34e8bb05b1..11dfb63a01 100644
--- a/autogpt_platform/backend/backend/copilot/gate/gate_test.py
+++ b/autogpt_platform/backend/backend/copilot/gate/gate_test.py
@@ -6,13 +6,12 @@ it should break these.
"""
from datetime import UTC, datetime
-from unittest.mock import AsyncMock, MagicMock, patch
+from unittest.mock import AsyncMock, patch
import pytest
from prisma.enums import ReviewStatus
from backend.copilot.gate import active_mode, chat_rules, check_action, gate_active
-from backend.copilot.gate import review as review_store
from backend.copilot.model import (
AutopilotMode,
ChatMessage,
@@ -24,7 +23,6 @@ from backend.copilot.model import (
_GATE = "backend.copilot.gate"
# The fixtures stub the rule lookup; the outage tests need the real one.
_REAL_ASK_REASON = chat_rules.ask_reason
-_REAL_HAS_OPEN_REVIEW = review_store.has_open_review
_MODES: tuple[AutopilotMode, ...] = ("ask_first", "auto", "unsupervised")
@@ -51,10 +49,10 @@ def gate_on():
@pytest.fixture
def clean_session_state():
- """No prior approval, nothing rejected in this chat, nothing waiting."""
+ """No prior approval and nothing rejected in this chat."""
with (
patch(f"{_GATE}.review_store.find_decision", AsyncMock(return_value=None)),
- patch(f"{_GATE}.review_store.has_open_review", AsyncMock(return_value=False)),
+ patch(f"{_GATE}.held.remember", AsyncMock(return_value=True)),
patch(f"{_GATE}.review_store.open_review", AsyncMock(return_value=True)),
patch(f"{_GATE}.chat_rules.ask_reason", AsyncMock(return_value=None)),
patch(f"{_GATE}.chat_rules.set_ask", AsyncMock()),
@@ -186,7 +184,6 @@ async def test_a_lost_consume_race_does_not_execute(gate_on, clean_session_state
):
decision = await check_action("bash_exec", {"command": "ls"}, "u", _session())
assert not decision.allowed
- assert not decision.already_waiting
async def test_a_rejection_makes_the_tool_ask_for_the_rest_of_the_chat(
@@ -244,38 +241,17 @@ async def test_calls_that_always_run_never_query_the_review_store(
find.assert_not_awaited()
-async def test_only_one_action_waits_at_a_time(gate_on, clean_session_state):
+async def test_a_call_that_cannot_be_kept_is_not_parked(gate_on, clean_session_state):
+ """A card whose call is lost could be approved and then run nothing."""
open_review = AsyncMock(return_value=True)
with (
- patch(f"{_GATE}.review_store.has_open_review", AsyncMock(return_value=True)),
+ patch(f"{_GATE}.held.remember", AsyncMock(return_value=False)),
patch(f"{_GATE}.review_store.open_review", open_review),
):
decision = await check_action(
"post_to_chat_platform", {"text": "hi"}, "u", _session()
)
assert not decision.allowed
- assert decision.already_waiting
- open_review.assert_not_awaited()
-
-
[email protected]("mode", _MODES)
-async def test_an_unreadable_review_queue_refuses_without_opening_a_second_card(
- gate_on, clean_session_state, mode
-):
- reviews = MagicMock()
- reviews.get_pending_reviews_for_execution = AsyncMock(
- side_effect=ConnectionError("db down")
- )
- open_review = AsyncMock(return_value=True)
- with (
- patch(f"{_GATE}.review_store.has_open_review", _REAL_HAS_OPEN_REVIEW),
- patch(f"{_GATE}.review_store.review_db", MagicMock(return_value=reviews)),
- patch(f"{_GATE}.review_store.open_review", open_review),
- patch(f"{_GATE}.chat_rules.ask_reason", AsyncMock(return_value="declined")),
- ):
- decision = await check_action("delete_folder", {"id": "f"}, "u", _session(mode))
- assert not decision.allowed
- assert not decision.already_waiting
assert decision.review_id is None
open_review.assert_not_awaited()
diff --git a/autogpt_platform/backend/backend/copilot/gate/held.py b/autogpt_platform/backend/backend/copilot/gate/held.py
new file mode 100644
index 0000000000..047ffe9e61
--- /dev/null
+++ b/autogpt_platform/backend/backend/copilot/gate/held.py
@@ -0,0 +1,400 @@
+"""Held calls: what a parked call was, so its card's answer can finish it later.
+
+A held call returns to the model at once, so the answer arrives after the turn
+that made the call — often after it ended. The call is kept here exactly as
+the model made it, and :func:`resolve_answered` runs it (or refuses it) at the
+start of the chat's next turn, inside the engine, where the turn's sandbox and
+tool bounds exist. Its result reaches the model as a user row naming the
+original call. :func:`wake` starts that turn when none is running; a running
+turn's end wakes it instead (``stream_registry.mark_session_completed``).
+"""
+
+import json
+import logging
+import uuid
+from datetime import UTC, datetime
+from typing import TYPE_CHECKING, Any, Callable, Iterable
+
+from prisma.enums import ReviewStatus
+from pydantic import BaseModel, Field, ValidationError
+
+from backend.copilot.constants import COPILOT_NODE_EXEC_ID_SEPARATOR
+from backend.copilot.model import ChatSession
+from backend.copilot.pending_messages import PendingMessage
+from backend.data.db_accessors import chat_db, review_db
+from backend.data.redis_client import get_redis_async
+from backend.util.encryption import JSONCryptor
+
+from . import chat_rules
+from . import review as review_store
+
+if TYPE_CHECKING:
+ from backend.api.features.graph_executions.review.model import (
+ PendingHumanReviewModel,
+ )
+ from backend.copilot.tools.base import BaseTool
+
+logger = logging.getLogger(__name__)
+
+# Outlives any card a user will still answer; the approval's own hour runs
+# from the click, not from here.
+_TTL_SECONDS = 30 * 24 * 60 * 60
+_KEY = "copilot:gate:held:"
+# Above the largest result either engine hands the model (the baseline's
+# 100,000-character output cap plus the wrapper), so nothing is cut here.
+_MAX_RESULT_CHARS = 120_000
+
+WAKE_MESSAGE = "I answered an action that was waiting for my approval."
+_RESEND = (
+ "Nothing ran: the approved action's details were lost before it could run. "
+ "Tell the user, and ask them to send the request again if it is still needed."
+)
+
+
+class HeldResult(PendingMessage):
+ """A late result is capped by the engine, as a direct one is, not by the
+ 32,000-character limit on a typed follow-up."""
+
+ content: str = Field(min_length=1, max_length=_MAX_RESULT_CHARS)
+
+
+class HeldCall(BaseModel):
+ review_id: str
+ tool_name: str
+ tool_call_id: str
+ args: dict[str, Any]
+ held_at: datetime = Field(default_factory=lambda: datetime.now(UTC))
+ # Rebuilt from a card whose stored copy of the arguments no longer binds.
+ lost: bool = False
+
+
+async def remember(session_id: str, call: HeldCall) -> bool:
+ """False means the call cannot be finished on approval, so it must not park."""
+ try:
+ redis = await get_redis_async()
+ async with redis.pipeline(transaction=True) as pipe:
+ # Encrypted: the arguments can hold a credential, and must stay exact
+ # to match the approval, so they cannot be redacted like the card's.
+ pipe.hset(
+ _key(session_id),
+ call.review_id,
+ JSONCryptor().encrypt(call.model_dump(mode="json")),
+ )
+ pipe.expire(_key(session_id), _TTL_SECONDS)
+ await pipe.execute()
+ return True
+ except Exception:
+ logger.warning(
+ f"Gate could not store held call {call.review_id}", exc_info=True
+ )
+ return False
+
+
+async def forget(session_id: str, review_id: str) -> None:
+ """Drop a call whose card was never opened; nothing can answer it."""
+ await _claim(session_id, review_id)
+
+
+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)
+ if not held:
+ return []
+ try:
+ rows = await review_db().get_reviews_by_node_exec_ids(list(held), user_id)
+ except Exception:
+ logger.warning(
+ f"Gate could not read held calls for session {session_id}", exc_info=True
+ )
+ return []
+ return sorted(
+ (
+ held[review_id]
+ for review_id, row in rows.items()
+ if row.session_id == session_id and row.status != ReviewStatus.WAITING
+ ),
+ key=lambda call: call.held_at,
+ )
+
+
+async def resolve_answered(
+ user_id: str | None,
+ session: ChatSession,
+ 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.
+ """
+ if not user_id:
+ return []
+ delivered: list[PendingMessage] = []
+ for call in await answered(user_id, session.session_id):
+ if not await _claim(session.session_id, call.review_id):
+ continue
+ try:
+ delivered.append(await _deliver(user_id, session, call, 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
+
+
+async def wake(
+ user_id: str,
+ session_id: str,
+ answered_rows: "Iterable[PendingHumanReviewModel]" = (),
+) -> None:
+ """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 turn
+ folds the results in anyway. ``answered_rows`` are the cards just
+ answered; one whose held call is gone from Redis is restored from its row.
+ """
+ # Deferred: the executor utilities import the tool registry.
+ from backend.copilot.active_turns import (
+ ConcurrentTurnLimitError,
+ acquire_turn_slot,
+ get_inflight_turn_limit,
+ )
+ from backend.copilot.executor.utils import dispatch_turn
+ from backend.copilot.model import ChatMessage, append_and_save_message
+ from backend.copilot.session_permissions import resolve_session_permissions
+ from backend.copilot.turn_queue import InflightCapExceeded, try_enqueue_turn
+
+ try:
+ await _restore(user_id, session_id, answered_rows)
+ calls = await answered(user_id, session_id)
+ if not calls:
+ return
+ # 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(
+ uuid.uuid5(
+ uuid.NAMESPACE_URL,
+ f"{session_id}:" + ",".join(sorted(c.review_id for c in calls)),
+ )
+ )
+ info = await chat_db().get_chat_session_metadata(session_id)
+ if info is None or info.user_id != user_id:
+ return
+ permissions = resolve_session_permissions(info)
+ metadata = {"held_calls_answered": True}
+ try:
+ async with acquire_turn_slot(user_id, session_id) as slot:
+ # Not admitted: a turn is already running, and its end wakes us.
+ if not slot.admitted:
+ return
+ if (
+ await append_and_save_message(
+ session_id,
+ ChatMessage(
+ id=wake_id,
+ role="user",
+ content=WAKE_MESSAGE,
+ metadata=metadata,
+ ),
+ )
+ is None
+ ):
+ return
+ await dispatch_turn(
+ slot,
+ session_id=session_id,
+ user_id=user_id,
+ turn_id=str(uuid.uuid4()),
+ message=WAKE_MESSAGE,
+ organization_id=info.organization_id,
+ team_id=info.team_id,
+ llm_auth_provider=info.metadata.llm_auth_provider,
+ llm_credential_id=info.metadata.llm_credential_id,
+ permissions=permissions,
+ message_metadata=metadata,
+ )
+ except ConcurrentTurnLimitError:
+ await try_enqueue_turn(
+ user_id=user_id,
+ inflight_cap=get_inflight_turn_limit(),
+ session_id=session_id,
+ message=WAKE_MESSAGE,
+ message_id=wake_id,
+ message_metadata=metadata,
+ llm_auth_provider=info.metadata.llm_auth_provider,
+ llm_credential_id=info.metadata.llm_credential_id,
+ permissions=(
+ permissions.model_dump(exclude_none=True) if permissions else None
+ ),
+ )
+ except InflightCapExceeded:
+ 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)
+
+
+async def _deliver(
+ user_id: str, session: ChatSession, call: HeldCall, cap: Callable[[str], str]
+) -> PendingMessage:
+ from backend.copilot.tools import get_tool
+
+ output = await _outcome(user_id, session, call, get_tool(call.tool_name))
+ try:
+ return _result_row(call, cap(output))
+ 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])
+
+
+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
+ spent 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)
+ spent = call.review_id not in rows
+ except Exception:
+ spent = False
+ if not spent:
+ await remember(session_id, call)
+ return []
+ return [
+ _result_row(
+ call,
+ "The approved action may have run, but its result was lost before it "
+ "reached you. Tell the user, and check the outcome before relying "
+ "on it.",
+ )
+ ]
+
+
+def _result_row(call: HeldCall, output: str) -> PendingMessage:
+ return HeldResult(
+ content=(
+ f'<held_call_result tool="{call.tool_name}" '
+ f'tool_call_id="{call.tool_call_id}" review_id="{call.review_id}">\n'
+ f"{output}\n</held_call_result>"
+ ),
+ metadata={
+ "held_call": {
+ "review_id": call.review_id,
+ "tool_name": call.tool_name,
+ "tool_call_id": call.tool_call_id,
+ }
+ },
+ )
+
+
+async def _outcome(
+ user_id: str, session: ChatSession, call: HeldCall, tool: "BaseTool | None"
+) -> str:
+ rows = await review_db().get_reviews_by_node_exec_ids([call.review_id], user_id)
+ row = rows.get(call.review_id)
+ if row is None or row.status == ReviewStatus.WAITING:
+ return "Nothing ran: this card is no longer open."
+ if row.status == ReviewStatus.REJECTED:
+ await review_store.consume(call.review_id, user_id)
+ await chat_rules.set_ask(session.session_id, call.tool_name)
+ return (
+ "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)
+ return (
+ "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 _RESEND
+ if tool is None:
+ await review_store.consume(call.review_id, user_id)
+ return "Nothing ran: this tool no longer exists."
+ # 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 the
+ # approval; spend it here so no later identical call rides on it.
+ await review_store.consume(call.review_id, user_id)
+ if isinstance(result.output, str):
+ return result.output
+ return json.dumps(result.output, default=str)
+
+
+async def _restore(
+ user_id: str, session_id: str, rows: "Iterable[PendingHumanReviewModel]"
+) -> None:
+ gate_prefix = review_store.node_id_for("")
+ rows = [r for r in rows if r.node_exec_id.startswith(gate_prefix)]
+ if not rows:
+ return
+ held = await _held(session_id)
+ for row in rows:
+ if row.node_exec_id not in held:
+ logger.warning(f"Held call {row.node_exec_id} restored from its card")
+ await remember(session_id, _from_row(user_id, session_id, row))
+
+
+def _from_row(
+ user_id: str, session_id: str, row: "PendingHumanReviewModel"
+) -> HeldCall:
+ """The card stores a redacted, clipped copy; the review id is a hash of the
+ exact arguments, so only a copy that hashes back to it may run."""
+ tool_name = row.node_exec_id.removeprefix(review_store.node_id_for("")).rsplit(
+ COPILOT_NODE_EXEC_ID_SEPARATOR, 1
+ )[0]
+ payload = row.payload if isinstance(row.payload, dict) else {}
+ args = payload.get("arguments")
+ if not isinstance(args, dict):
+ args = {}
+ intact = (
+ review_store.review_id_for(session_id, user_id, tool_name, args)
+ == row.node_exec_id
+ )
+ return HeldCall(
+ review_id=row.node_exec_id,
+ tool_name=tool_name,
+ tool_call_id="",
+ args=args if intact else {},
+ held_at=row.created_at,
+ lost=not intact,
+ )
+
+
+async def _held(session_id: str) -> dict[str, HeldCall]:
+ try:
+ redis = await get_redis_async()
+ raw = await redis.hgetall(_key(session_id))
+ except Exception:
+ logger.warning(
+ f"Gate could not list held calls for session {session_id}", exc_info=True
+ )
+ return {}
+ held: dict[str, HeldCall] = {}
+ for key, value in raw.items():
+ review_id = key.decode() if isinstance(key, bytes) else key
+ stored = value.decode() if isinstance(value, bytes) else value
+ try:
+ held[review_id] = HeldCall.model_validate(JSONCryptor().decrypt(stored))
+ except ValidationError:
+ # Unreadable is the same as gone: the answer restores it from its card.
+ logger.warning(f"Gate could not read held call {review_id}")
+ return held
+
+
+async def _claim(session_id: str, review_id: str) -> bool:
+ try:
+ redis = await get_redis_async()
+ return await redis.hdel(_key(session_id), review_id) == 1
+ except Exception:
+ logger.warning(f"Gate could not claim held call {review_id}", exc_info=True)
+ return False
+
+
+def _key(session_id: str) -> str:
+ return f"{_KEY}{session_id}"
diff --git a/autogpt_platform/backend/backend/copilot/gate/held_db_test.py b/autogpt_platform/backend/backend/copilot/gate/held_db_test.py
new file mode 100644
index 0000000000..90890cd221
--- /dev/null
+++ b/autogpt_platform/backend/backend/copilot/gate/held_db_test.py
@@ -0,0 +1,444 @@
+"""Held calls against Postgres and Redis: the queue, and what an answer runs.
+
+The call under test is a stub under ``post_to_chat_platform``'s name, so the
+gate treats it as the outward action it is (Auto asks) while the test sees
+exactly what the approval ran it with.
+"""
+
+import asyncio
+import json
+from datetime import UTC, datetime, timedelta
+from typing import Any
+from unittest.mock import AsyncMock, patch
+
+import pytest
+from prisma.enums import ReviewStatus
+from prisma.models import PendingHumanReview
+
+from backend.copilot.gate import chat_rules, check_action, held
+from backend.copilot.gate import review as review_store
+from backend.copilot.model import (
+ AutopilotMode,
+ ChatMessage,
+ ChatSession,
+ append_and_save_message,
+ get_chat_session,
+ update_session_autopilot_mode,
+ upsert_chat_session,
+)
+from backend.copilot.tools.base import BaseTool
+from backend.copilot.tools.models import ResponseType, ToolResponseBase
+from backend.data.db_accessors import review_db
+from backend.data.redis_client import get_redis_async
+
+_TOOL = "post_to_chat_platform"
+
+
+class _Post(BaseTool):
+ def __init__(self) -> None:
+ self.runs: list[dict[str, Any]] = []
+
+ @property
+ def name(self) -> str:
+ return _TOOL
+
+ @property
+ def description(self) -> str:
+ return "posts"
+
+ @property
+ def parameters(self) -> dict:
+ return {"type": "object", "properties": {}}
+
+ async def _execute(self, user_id, session, **kwargs) -> ToolResponseBase:
+ self.runs.append(kwargs)
+ return ToolResponseBase(
+ type=ResponseType.ERROR, message=f"posted {kwargs.get('text')}"
+ )
+
+
+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
+
+
+async def _new_session(user_id: str, mode: AutopilotMode = "auto") -> ChatSession:
+ session = await upsert_chat_session(ChatSession.new(user_id=user_id, dry_run=False))
+ await update_session_autopilot_mode(session.session_id, user_id, mode)
+ reloaded = await get_chat_session(session.session_id, user_id)
+ assert reloaded is not None
+ return reloaded
+
+
+async def _hold(session: ChatSession, user_id: str, text: str) -> str:
+ decision = await check_action(
+ _TOOL, {"text": text}, user_id, session, tool_call_id="call-1"
+ )
+ assert not decision.allowed and decision.review_id
+ return decision.review_id
+
+
+async def _answer(review_id: str, status: ReviewStatus, age=timedelta(0)) -> None:
+ await PendingHumanReview.prisma().update(
+ where={"nodeExecId": review_id},
+ data={"status": status, "reviewedAt": datetime.now(UTC) - age},
+ )
+
+
+async def _row(review_id: str, user_id: str):
+ rows = await review_db().get_reviews_by_node_exec_ids([review_id], user_id)
+ return rows.get(review_id)
+
+
[email protected](loop_scope="session")
+async def test_two_held_calls_in_one_turn_both_wait(
+ setup_test_user, test_user_id, gate_on
+):
+ session = await _new_session(test_user_id)
+
+ first = await _hold(session, test_user_id, "one")
+ second = await _hold(session, test_user_id, "two")
+
+ waiting = await review_db().get_pending_reviews_for_chat_session(
+ session.session_id, test_user_id
+ )
+ assert [r.node_exec_id for r in waiting] == [first, second]
+
+
[email protected](loop_scope="session")
+async def test_a_retry_of_a_waiting_call_keeps_the_first_call(
+ setup_test_user, test_user_id, gate_on
+):
+ session = await _new_session(test_user_id)
+ review_id = await _hold(session, test_user_id, "again")
+
+ retry = await check_action(
+ _TOOL, {"text": "again"}, test_user_id, session, tool_call_id="call-2"
+ )
+ await _answer(review_id, ReviewStatus.APPROVED)
+
+ assert not retry.allowed and retry.review_id == review_id
+ [call] = await held.answered(test_user_id, session.session_id)
+ assert call.tool_call_id == "call-1"
+
+
[email protected](loop_scope="session")
+async def test_a_failed_wake_is_not_repeated_for_the_same_cards(
+ setup_test_user, test_user_id, gate_on
+):
+ """Even after an error reply, which lets an identical user row through."""
+ session = await _new_session(test_user_id)
+ review_id = await _hold(session, test_user_id, "wake once")
+ await _answer(review_id, ReviewStatus.APPROVED)
+ dispatch = AsyncMock(side_effect=RuntimeError("queue down"))
+
+ with patch("backend.copilot.executor.utils.dispatch_turn", dispatch):
+ await held.wake(test_user_id, session.session_id)
+ await append_and_save_message(
+ session.session_id, ChatMessage(role="assistant", content="error")
+ )
+ await held.wake(test_user_id, session.session_id)
+
+ reloaded = await get_chat_session(session.session_id, test_user_id)
+ assert reloaded is not None
+ wakes = [m for m in reloaded.messages if m.content == held.WAKE_MESSAGE]
+ assert len(wakes) == 1
+ assert dispatch.await_count == 1
+
+
[email protected](loop_scope="session")
+async def test_an_approval_runs_the_call_with_its_stored_arguments(
+ setup_test_user, test_user_id, gate_on, post_tool
+):
+ """Long enough that the card shows a clipped copy: the run gets the whole."""
+ session = await _new_session(test_user_id)
+ text = "hello team " + "x" * 5_000
+ review_id = await _hold(session, test_user_id, text)
+ await _answer(review_id, ReviewStatus.APPROVED)
+
+ delivered = await held.resolve_answered(test_user_id, session)
+
+ assert post_tool.runs == [{"text": text}]
+ assert len(delivered) == 1
+ assert "posted hello team" in delivered[0].content
+ assert 'tool_call_id="call-1"' in delivered[0].content
+ assert await _row(review_id, test_user_id) is None
+
+
+async def _approve_after_losing_the_held_call(
+ session: ChatSession, user_id: str, review_id: str
+) -> None:
+ """Redis lost the entry (eviction, flush) while the card sat in Postgres."""
+ assert await held._claim(session.session_id, review_id)
+ await _answer(review_id, ReviewStatus.APPROVED)
+ rows = await review_db().get_reviews_by_node_exec_ids([review_id], user_id)
+ with patch("backend.copilot.executor.utils.dispatch_turn", AsyncMock()):
+ await held.wake(user_id, session.session_id, rows.values())
+
+
[email protected](loop_scope="session")
+async def test_a_call_whose_card_could_not_open_leaves_nothing_held(
+ setup_test_user, test_user_id, gate_on
+):
+ session = await _new_session(test_user_id)
+
+ with patch.object(review_store, "open_review", AsyncMock(return_value=False)):
+ decision = await check_action(
+ _TOOL, {"text": "no card"}, test_user_id, session, tool_call_id="c1"
+ )
+
+ assert not decision.allowed and decision.review_id is None
+ redis = await get_redis_async()
+ assert await redis.hgetall(held._key(session.session_id)) == {}
+
+
[email protected](loop_scope="session")
+async def test_a_held_calls_arguments_are_never_stored_in_the_clear(
+ setup_test_user, test_user_id, gate_on, post_tool
+):
+ """They can hold a credential, and live in Redis until the card is answered."""
+ session = await _new_session(test_user_id)
+ secret = "sk-live-not-for-redis-0123456789"
+ review_id = await _hold(session, test_user_id, f"token {secret}")
+
+ redis = await get_redis_async()
+ stored = await redis.hgetall(held._key(session.session_id))
+ 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)
+ assert post_tool.runs == [{"text": f"token {secret}"}]
+
+
[email protected](loop_scope="session")
+async def test_an_approval_whose_held_call_was_lost_runs_from_the_card(
+ setup_test_user, test_user_id, gate_on, post_tool
+):
+ session = await _new_session(test_user_id)
+ review_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)
+
+ assert post_tool.runs == [{"text": "from the card"}]
+ assert "posted from the card" in delivered.content
+ assert await _row(review_id, test_user_id) is None
+
+
[email protected](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
+):
+ """The card holds a clipped copy, which must never run in the call's place."""
+ session = await _new_session(test_user_id)
+ review_id = await _hold(session, test_user_id, "clipped " + "x" * 5_000)
+ await _approve_after_losing_the_held_call(session, test_user_id, review_id)
+
+ [delivered] = await held.resolve_answered(test_user_id, session)
+
+ assert post_tool.runs == []
+ assert "send the request again" in delivered.content
+ assert await _row(review_id, test_user_id) is None
+
+
[email protected](loop_scope="session")
+async def test_a_large_late_result_arrives_as_the_direct_result_would(
+ setup_test_user, test_user_id, gate_on, post_tool
+):
+ """Longer than a typed follow-up may be: the engine caps it, nothing else."""
+ session = await _new_session(test_user_id)
+ text = "y" * 50_000
+ with patch(
+ "backend.copilot.gate.is_feature_enabled", AsyncMock(return_value=False)
+ ):
+ direct = await post_tool.execute(test_user_id, session, "call-1", text=text)
+ review_id = await _hold(session, test_user_id, text)
+ await _answer(review_id, ReviewStatus.APPROVED)
+
+ [delivered] = await held.resolve_answered(test_user_id, session)
+
+ assert delivered.content == (
+ f'<held_call_result tool="{_TOOL}" tool_call_id="call-1" '
+ f'review_id="{review_id}">\n{direct.output}\n</held_call_result>'
+ )
+
+
[email protected](loop_scope="session")
+async def test_a_failure_after_the_claim_keeps_the_call_for_the_next_turn(
+ setup_test_user, test_user_id, gate_on, post_tool
+):
+ session = await _new_session(test_user_id)
+ 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) == []
+ 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)
+ assert "posted flaky" in delivered.content
+
+
+ "status, expect",
+ [(ReviewStatus.APPROVED, "posted keep"), (ReviewStatus.REJECTED, "declined")],
+)
[email protected](loop_scope="session")
+async def test_a_failed_cap_still_delivers_the_real_outcome(
+ setup_test_user, test_user_id, gate_on, post_tool, status, expect
+):
+ """A refusal stays a refusal, and a run is reported with its result."""
+ session = await _new_session(test_user_id)
+ review_id = await _hold(session, test_user_id, "keep")
+ await _answer(review_id, status)
+
+ def broken_cap(_text: str) -> str:
+ raise RuntimeError("cap failed")
+
+ [delivered] = await held.resolve_answered(test_user_id, session, cap=broken_cap)
+
+ assert expect in delivered.content
+ assert "may have run" not in delivered.content
+ assert post_tool.runs == (
+ [{"text": "keep"}] if status == ReviewStatus.APPROVED else []
+ )
+
+
[email protected](loop_scope="session")
+async def test_a_failure_after_the_approval_was_spent_says_it_may_have_run(
+ setup_test_user, test_user_id, gate_on, post_tool
+):
+ """Re-storing it would report "no longer open" for a call that reached the gate."""
+ session = await _new_session(test_user_id)
+ review_id = await _hold(session, test_user_id, "spent")
+ await _answer(review_id, ReviewStatus.APPROVED)
+
+ async def spend_then_fail(user_id, *_args):
+ 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)
+
+ assert "may have run" in delivered.content
+ assert await held.answered(test_user_id, session.session_id) == []
+
+
[email protected](loop_scope="session")
+async def test_an_approval_run_with_the_flag_off_is_still_spent(
+ setup_test_user, test_user_id, gate_on, post_tool
+):
+ session = await _new_session(test_user_id)
+ review_id = await _hold(session, test_user_id, "flag off")
+ await _answer(review_id, ReviewStatus.APPROVED)
+
+ with patch(
+ "backend.copilot.gate.is_feature_enabled", AsyncMock(return_value=False)
+ ):
+ await held.resolve_answered(test_user_id, session)
+
+ assert post_tool.runs == [{"text": "flag off"}]
+ assert await _row(review_id, test_user_id) is None
+
+
[email protected](loop_scope="session")
+async def test_a_row_approved_and_resolved_four_times_at_once_runs_once(
+ setup_test_user, test_user_id, gate_on, post_tool
+):
+ """Four turns that all listed the answered card before any of them ran it."""
+ session = await _new_session(test_user_id)
+ review_id = await _hold(session, test_user_id, "once")
+ await _answer(review_id, ReviewStatus.APPROVED)
+ listed = await held.answered(test_user_id, session.session_id)
+
+ with patch.object(held, "answered", AsyncMock(return_value=listed)):
+ results = await asyncio.gather(
+ *(held.resolve_answered(test_user_id, session) for _ in range(4))
+ )
+
+ assert len(post_tool.runs) == 1
+ assert sum(len(r) for r in results) == 1
+
+
[email protected](loop_scope="session")
+async def test_a_stale_approval_delivers_a_refusal_not_a_run(
+ setup_test_user, test_user_id, gate_on, post_tool
+):
+ """Unsupervised by the time it resolves: a stale approval must still not run."""
+ session = await _new_session(test_user_id)
+ review_id = await _hold(session, test_user_id, "late")
+ await _answer(review_id, ReviewStatus.APPROVED, age=timedelta(hours=2))
+ await update_session_autopilot_mode(
+ session.session_id, test_user_id, "unsupervised"
+ )
+ 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)
+
+ assert post_tool.runs == []
+ assert "expired" in delivered[0].content
+ assert await _row(review_id, test_user_id) is None
+
+
[email protected](loop_scope="session")
+async def test_a_rejection_never_runs_and_the_tool_asks_from_then_on(
+ setup_test_user, test_user_id, gate_on, post_tool
+):
+ """Even with the flag switched off since: the gate would then allow anything."""
+ session = await _new_session(test_user_id)
+ review_id = await _hold(session, test_user_id, "no")
+ await _answer(review_id, ReviewStatus.REJECTED)
+
+ with patch(
+ "backend.copilot.gate.is_feature_enabled", AsyncMock(return_value=False)
+ ):
+ delivered = await held.resolve_answered(test_user_id, session)
+
+ assert post_tool.runs == []
+ assert "declined" in delivered[0].content
+ assert await chat_rules.ask_reason(session.session_id, _TOOL) == chat_rules.DECLINED
+
+
[email protected](loop_scope="session")
+async def test_a_held_call_returns_the_stub_and_no_partial_result(
+ setup_test_user, test_user_id, gate_on, post_tool
+):
+ """What a dependent call could read while its input is held: no result at all."""
+ session = await _new_session(test_user_id)
+
+ output = await post_tool.execute(test_user_id, session, "call-draft", text="draft")
+
+ body = json.loads(str(output.output))
+ assert body["type"] == "approval_required"
+ assert body["review_id"]
+ assert "posted" not in str(output.output)
+ assert post_tool.runs == []
+
+
[email protected](loop_scope="session")
+async def test_an_answer_on_an_idle_chat_starts_its_turn(
+ setup_test_user, test_user_id, gate_on
+):
+ session = await _new_session(test_user_id)
+ review_id = await _hold(session, test_user_id, "wake")
+ dispatch = AsyncMock()
+ with patch("backend.copilot.executor.utils.dispatch_turn", dispatch):
+ await held.wake(test_user_id, session.session_id)
+ dispatch.assert_not_awaited()
+
+ await _answer(review_id, ReviewStatus.APPROVED)
+ await held.wake(test_user_id, session.session_id)
+
+ dispatch.assert_awaited_once()
+ assert dispatch.await_args.kwargs["message"] == held.WAKE_MESSAGE
diff --git a/autogpt_platform/backend/backend/copilot/gate/held_engines_test.py b/autogpt_platform/backend/backend/copilot/gate/held_engines_test.py
new file mode 100644
index 0000000000..eaa49c3be4
--- /dev/null
+++ b/autogpt_platform/backend/backend/copilot/gate/held_engines_test.py
@@ -0,0 +1,158 @@
+"""Each engine folds answered held calls into the turn it is starting.
+
+One test per engine, driven through the engine's real entry point and stopped
+at the fold: removing the fold from one engine turns only that engine's test
+red. The held call itself runs inside ``resolve_answered``, so each test also
+proves the turn's execution context is set before it runs.
+"""
+
+import contextlib
+from unittest.mock import AsyncMock, MagicMock, patch
+
+import pytest
+
+from backend.copilot.baseline.service import stream_chat_completion_baseline
+from backend.copilot.context import get_execution_context, set_execution_context
+from backend.copilot.model import ChatSession
+from backend.copilot.model_router import ResolvedModel
+from backend.copilot.pending_messages import PendingMessage
+from backend.copilot.sdk.expert_tool_gate_test import _make_patches, _make_session
+from backend.copilot.sdk.tool_adapter import cap_late_tool_result
+
+_RESULT = PendingMessage(
+ content='<held_call_result tool="post_to_chat_platform">posted</held_call_result>'
+)
+
+
+class _StopAtFold(Exception):
+ pass
+
+
+async 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] = []
+
+ async def resolve(*_args, **kwargs):
+ order.append("resolve")
+ caps.append(kwargs.get("cap"))
+ return [_RESULT]
+
+ async def persist(_session, _builder, pending, **_kwargs):
+ folded.append(list(pending))
+ raise _StopAtFold
+
+ 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))
+ context = stack.enter_context(
+ patch(
+ "backend.copilot.sdk.service.set_execution_context",
+ side_effect=lambda *a, **k: order.append("context"),
+ )
+ )
+ stack.enter_context(
+ patch("backend.copilot.sdk.service.resolve_answered", new=resolve)
+ )
+ stack.enter_context(
+ patch(
+ "backend.copilot.sdk.service.persist_pending_as_user_rows",
+ new=persist,
+ )
+ )
+ with contextlib.suppress(_StopAtFold):
+ async for _ in stream_chat_completion_sdk(
+ session_id=session.session_id,
+ message="hello",
+ is_user_message=True,
+ user_id="test-user",
+ session=session,
+ ):
+ pass
+
+ context.assert_called()
+ assert order.index("context") < order.index("resolve")
+ assert folded == [[_RESULT]]
+ # A late result is cut by the same rule as a direct MCP tool result.
+ assert caps == [cap_late_tool_result]
+
+
+async def test_the_baseline_engine_opens_its_turn_with_the_held_result():
+ session = ChatSession.new("user-1", dry_run=False)
+ session.title = "already titled"
+ seen_context: list[tuple[str | None, ChatSession | None]] = []
+ sent: list[list[dict]] = []
+
+ async def resolve(*_args, **_kwargs):
+ seen_context.append(get_execution_context())
+ return [_RESULT]
+
+ persist = AsyncMock(return_value=True)
+
+ async def model_loop(*, messages, **_kwargs):
+ sent.append(list(messages))
+ raise _StopAtFold
+ yield
+
+ 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),
+ ):
+ try:
+ async for _ in stream_chat_completion_baseline(
+ session_id=session.session_id,
+ message=None,
+ is_user_message=False,
+ user_id="user-1",
+ session=session,
+ ):
+ pass
+ except Exception:
+ pass # what the turn does after the model's first call is not under test
+ finally:
+ set_execution_context(None, None)
+
+ assert seen_context == [("user-1", session)]
+ assert len(sent) == 1
+ assert sent[0][-1] == {"role": "user", "content": _RESULT.content}
+ # The uploaded transcript must carry it too, or the next turn loses it.
+ assert persist.await_args.args[1] is not None
diff --git a/autogpt_platform/backend/backend/copilot/gate/mcp_seam.py b/autogpt_platform/backend/backend/copilot/gate/mcp_seam.py
index a05b5f30b9..354ba04e9c 100644
--- a/autogpt_platform/backend/backend/copilot/gate/mcp_seam.py
+++ b/autogpt_platform/backend/backend/copilot/gate/mcp_seam.py
@@ -15,7 +15,7 @@ from typing import Any
from backend.copilot.model import ChatSession
-from . import check_action
+from . import check_action, refusal_message
logger = logging.getLogger(__name__)
@@ -54,11 +54,7 @@ def _error(
"type": "approval_required",
"tool_name": tool_name,
"reason": reason,
- "message": (
- f"Nothing ran. {reason} Tell the user exactly what you wanted to "
- "do and why, then stop. Do not retry, do not work around it, and "
- "do not use another tool for the same effect."
- ),
+ "message": refusal_message(reason, review_id),
"session_id": session.session_id,
}
if review_id:
diff --git a/autogpt_platform/backend/backend/copilot/gate/review.py b/autogpt_platform/backend/backend/copilot/gate/review.py
index fbd9fb9594..acc5e57f6b 100644
--- a/autogpt_platform/backend/backend/copilot/gate/review.py
+++ b/autogpt_platform/backend/backend/copilot/gate/review.py
@@ -32,9 +32,9 @@ logger = logging.getLogger(__name__)
# tool handler runs, so an argument can arrive holding a whole file.
_MAX_ARG_CHARS = 4_000
-# An approval the model never came back for must not run the same call days
-# later; the normal retry follows the click within seconds.
-_APPROVAL_TTL = timedelta(hours=1)
+# An approval must not run a call long after the user gave it; the answered
+# card's turn normally runs it within seconds.
+APPROVAL_TTL = timedelta(hours=1)
def node_id_for(tool_name: str) -> str:
@@ -109,7 +109,7 @@ async def find_decision(
approved_at = review.reviewed_at or review.updated_at or review.created_at
if (
review.status == ReviewStatus.APPROVED
- and datetime.now(UTC) - approved_at > _APPROVAL_TTL
+ and datetime.now(UTC) - approved_at > APPROVAL_TTL
):
await consume(review_id, user_id)
return None
@@ -130,30 +130,6 @@ async def consume(review_id: str, user_id: str) -> bool:
return False
-async def has_open_review(user_id: str, session_id: str) -> bool | None:
- """Whether this session is already waiting on the user; None when unreadable.
-
- The gate parks at most one action at a time. ``PendingReviewsList`` has a
- single Approve button that submits every row in the list, and collapses any
- group of more than one by default — so a queue of five would be approved by
- one click on a card that displayed one of them. Capping the queue also
- keeps one turn from firing five "Needs You" alerts.
- """
- try:
- pending = await review_db().get_pending_reviews_for_chat_session(
- session_id, user_id
- )
- except Exception:
- logger.warning(
- f"Gate could not list pending reviews for session {session_id}",
- exc_info=True,
- )
- return None
- return any(
- r.node_exec_id.startswith(f"{COPILOT_NODE_PREFIX}gate-") for r in pending
- )
-
-
async def open_review(
review_id: str,
user_id: str,
diff --git a/autogpt_platform/backend/backend/copilot/gate/store_db_test.py b/autogpt_platform/backend/backend/copilot/gate/store_db_test.py
index e2033e38f8..e635d0f23e 100644
--- a/autogpt_platform/backend/backend/copilot/gate/store_db_test.py
+++ b/autogpt_platform/backend/backend/copilot/gate/store_db_test.py
@@ -14,6 +14,7 @@ from backend.copilot.model import (
update_session_autopilot_mode,
upsert_chat_session,
)
+from backend.data.db_accessors import review_db
@pytest.mark.asyncio(loop_scope="session")
@@ -32,7 +33,10 @@ async def test_a_parked_call_is_the_chats_review_and_its_approval_is_found(
review_id, test_user_id, session, "bash_exec", args, "needs you"
)
- assert await review_store.has_open_review(test_user_id, session.session_id)
+ queue = await review_db().get_pending_reviews_for_chat_session(
+ session.session_id, test_user_id
+ )
+ assert [r.node_exec_id for r in queue] == [review_id]
await PendingHumanReview.prisma().update(
where={"nodeExecId": review_id}, data={"status": ReviewStatus.APPROVED}
)
diff --git a/autogpt_platform/backend/backend/copilot/prompting.py b/autogpt_platform/backend/backend/copilot/prompting.py
index 26e0ca3e31..1f7a4c0e82 100644
--- a/autogpt_platform/backend/backend/copilot/prompting.py
+++ b/autogpt_platform/backend/backend/copilot/prompting.py
@@ -452,14 +452,13 @@ VOICE_TURN_PREFIX = (
# Environment-specific supplement templates
_APPROVAL_RULES = """
-When a tool returns `approval_required`, nothing happened. Tell the user
-plainly what you wanted to do and why it needs them, then stop and wait. Do
-not retry the call, do not adjust the arguments and try again, and do not
-reach for a different tool to achieve the same effect — the gate treats that
-as the same action, and the user will simply be asked again.
-
-Only one action can wait for approval at a time. If you are told one is
-already waiting, stop and let the user answer it.
+When a tool returns `approval_required` with a review id, the call is held
+for the user and nothing has run. Do not retry it, adjust its arguments, or
+reach the same effect with another tool. Carry on with everything that does
+not depend on it; a call that needs its result waits. When nothing is left
+that does not, tell the user what is waiting on them and stop. If they
+approve, its result reaches you later in a `<held_call_result>` naming the
+call; pick up from there.
"""
_MODE_SUPPLEMENTS = {
diff --git a/autogpt_platform/backend/backend/copilot/sdk/service.py b/autogpt_platform/backend/backend/copilot/sdk/service.py
index 34927ba963..768d240828 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/service.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/service.py
@@ -68,6 +68,7 @@ from backend.integrations.codex.transport import CodexCredentialLease
from backend.integrations.credential_lease import CredentialLease
from backend.util.exceptions import NotFoundError
from backend.copilot.gate import active_mode
+from backend.copilot.gate.held import resolve_answered
from backend.util.feature_flag import Flag, is_feature_enabled
from backend.util.prompt import (
DEFAULT_COMPRESSION_RESERVE,
@@ -212,6 +213,7 @@ from .openrouter_cost import record_turn_cost_from_openrouter
from .response_adapter import SDKResponseAdapter
from .security_hooks import create_security_hooks
from .tool_adapter import (
+ cap_late_tool_result,
MCP_TOOL_PREFIX,
create_copilot_mcp_server,
get_copilot_tool_names,
@@ -5285,7 +5287,10 @@ async def stream_chat_completion_sdk( # pyright: ignore[reportGeneralTypeIssues
# SDK client spawns.
yield StreamStatus(message="Preparing conversation context…")
- pending_messages = await drain_pending_safe(session_id, log_prefix)
+ # 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)
if pending_messages:
logger.info(
"%s Draining %d pending message(s) at turn start",
diff --git a/autogpt_platform/backend/backend/copilot/sdk/tool_adapter.py b/autogpt_platform/backend/backend/copilot/sdk/tool_adapter.py
index fd94679e10..9f1c6b1aba 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/tool_adapter.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/tool_adapter.py
@@ -682,6 +682,12 @@ _READ_TOOL_SCHEMA = {
# ---------------------------------------------------------------------------
+def cap_late_tool_result(text: str) -> str:
+ """A late tool result, cut exactly as the MCP wrapper cuts a direct one."""
+ result = {"content": [{"type": "text", "text": text}], "isError": False}
+ return _text_from_mcp_result(truncate(result, _MCP_MAX_CHARS))
+
+
def _text_from_mcp_result(result: dict[str, Any]) -> str:
"""Extract concatenated text from an MCP response's content blocks."""
content = result.get("content", [])
diff --git a/autogpt_platform/backend/backend/copilot/sdk/tool_adapter_test.py b/autogpt_platform/backend/backend/copilot/sdk/tool_adapter_test.py
index aca809609e..401fe8b6a6 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/tool_adapter_test.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/tool_adapter_test.py
@@ -24,6 +24,7 @@ from .tool_adapter import (
_make_truncating_wrapper,
_strip_llm_fields,
_text_from_mcp_result,
+ cap_late_tool_result,
create_copilot_mcp_server,
create_tool_handler,
get_sdk_disallowed_tools,
@@ -1533,3 +1534,23 @@ def test_set_execution_context_carries_hidden_tools():
finally:
set_execution_context(None, session)
assert get_current_hidden_tools() == frozenset()
+
+
+class TestLateToolResultCap:
+ """A held call's late result must read as the direct result would have."""
+
+ @pytest.mark.asyncio
+ async def test_a_late_result_is_cut_exactly_as_the_wrapper_cuts_a_direct_one(
+ self,
+ ):
+ text = "x" * (_MCP_MAX_CHARS + 20_000)
+
+ async def handler(_args):
+ return {"content": [{"type": "text", "text": text}], "isError": False}
+
+ _init_ctx(_make_test_session())
+ wrapper = _make_truncating_wrapper(handler, "read_workspace_file")
+ direct = _text_from_mcp_result(await wrapper({}))
+
+ assert len(direct) < len(text)
+ assert cap_late_tool_result(text) == direct
diff --git a/autogpt_platform/backend/backend/copilot/stream_registry.py b/autogpt_platform/backend/backend/copilot/stream_registry.py
index 410813c705..71442f2248 100644
--- a/autogpt_platform/backend/backend/copilot/stream_registry.py
+++ b/autogpt_platform/backend/backend/copilot/stream_registry.py
@@ -68,6 +68,9 @@ from .response_model import (
)
logger = logging.getLogger(__name__)
+
+# The error a turn ends with when the user stopped it.
+CANCELLED_MESSAGE = "Operation cancelled"
config = ChatConfig()
_notification_bus = AsyncRedisNotificationEventBus()
@@ -973,6 +976,14 @@ async def mark_session_completed(
exc,
)
+ # 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.
+ 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)
+
if error_message and not skip_error_publish:
try:
await publish_chunk(turn_id, StreamError(errorText=error_message))
diff --git a/autogpt_platform/backend/backend/copilot/stream_registry_test.py b/autogpt_platform/backend/backend/copilot/stream_registry_test.py
index c374b97471..50d2abd033 100644
--- a/autogpt_platform/backend/backend/copilot/stream_registry_test.py
+++ b/autogpt_platform/backend/backend/copilot/stream_registry_test.py
@@ -794,3 +794,49 @@ async def test_get_active_session_falls_back_when_the_stream_is_empty():
_session, last_id = await stream_registry.get_active_session("sess-1")
assert last_id == "0-0"
+
+
+ "error_message, wakes",
+ [
+ (None, True),
+ ("model provider timed out", True),
+ (stream_registry.CANCELLED_MESSAGE, False),
+ ],
+)
+async def test_a_finished_turn_wakes_the_chat_for_cards_answered_during_it(
+ error_message: str | None, wakes: bool
+):
+ """Nothing else starts that turn, so a failed one wakes it too; the
+ user's own Stop leaves it for their next turn."""
+ fake_redis = _FakeRedis({"status": "running", "turn_id": "turn-1", "user_id": "u1"})
+ wake = AsyncMock()
+
+ with (
+ patch.object(
+ stream_registry, "get_redis_async", new=AsyncMock(return_value=fake_redis)
+ ),
+ patch.object(
+ stream_registry, "hash_compare_and_set", new=AsyncMock(return_value=True)
+ ),
+ 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, "release_turn_slot", new=AsyncMock()),
+ patch(
+ "backend.copilot.stream_registry.dispatch_next_for_user", new=AsyncMock()
+ ),
+ patch("backend.copilot.gate.held.wake", new=wake),
+ ):
+ await stream_registry.mark_session_completed(
+ "sess-1", error_message=error_message
+ )
+
+ assert wake.await_count == (1 if wakes else 0)
+ if wakes:
+ wake.assert_awaited_once_with("u1", "sess-1")
diff --git a/autogpt_platform/backend/backend/copilot/tools/base.py b/autogpt_platform/backend/backend/copilot/tools/base.py
index 0b5e4d255a..3c6ffc5cd7 100644
--- a/autogpt_platform/backend/backend/copilot/tools/base.py
+++ b/autogpt_platform/backend/backend/copilot/tools/base.py
@@ -502,7 +502,9 @@ class BaseTool:
from backend.copilot.gate import check_action
try:
- decision = await check_action(self.name, kwargs, user_id, session)
+ decision = await check_action(
+ self.name, kwargs, user_id, session, tool_call_id
+ )
except Exception:
logger.warning(f"Action gate failed for {self.name}", exc_info=True)
return self._refusal(
@@ -525,16 +527,13 @@ class BaseTool:
reason: str,
review_id: str | None = None,
) -> StreamToolOutputAvailable:
+ from backend.copilot.gate import refusal_message
+
return StreamToolOutputAvailable(
toolCallId=tool_call_id,
toolName=self.name,
output=ApprovalRequiredResponse(
- message=(
- f"Nothing ran. {reason} Tell the user exactly what you "
- "wanted to do and why, then stop. Do not retry, do not "
- "work around it, and do not use another tool for the "
- "same effect."
- ),
+ message=refusal_message(reason, review_id),
session_id=session.session_id,
tool_name=self.name,
reason=reason,
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/CopilotChatHost.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/CopilotChatHost.tsx
index 1325f0eeb3..4b3b371a21 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/CopilotChatHost.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/CopilotChatHost.tsx
@@ -62,6 +62,7 @@ export function CopilotChatHost({
isResolvingExpertIdentity,
isAdoptingExpertSession,
isKickoffStarting,
+ followBackendTurn,
} = useCopilotPage();
return (
@@ -110,6 +111,7 @@ export function CopilotChatHost({
isResolvingExpertIdentity={isResolvingExpertIdentity}
isAdoptingExpertSession={isAdoptingExpertSession}
isKickoffStarting={isKickoffStarting}
+ onBackendTurn={followBackendTurn}
hasFloatingControls={hasFloatingControls}
/>
</div>
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/useFollowBackendTurn.test.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/useFollowBackendTurn.test.ts
new file mode 100644
index 0000000000..5504270df8
--- /dev/null
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/useFollowBackendTurn.test.ts
@@ -0,0 +1,36 @@
+import { act, renderHook } from "@testing-library/react";
+import { expect, test, vi } from "vitest";
+import { useFollowBackendTurn } from "../useFollowBackendTurn";
+
+function setup(status: string) {
+ const hasResumedRef = { current: true };
+ const refetchSession = vi.fn(async () => ({}));
+ const hook = renderHook(
+ ({ status }) =>
+ useFollowBackendTurn({ status, refetchSession, hasResumedRef }),
+ { initialProps: { status } },
+ );
+ return { hook, hasResumedRef, refetchSession };
+}
+
+test("an idle chat re-arms the resume and refetches at once", () => {
+ const { hook, hasResumedRef, refetchSession } = setup("ready");
+
+ act(() => hook.result.current.followBackendTurn());
+
+ expect(hasResumedRef.current).toBe(false);
+ expect(refetchSession).toHaveBeenCalledTimes(1);
+});
+
+test("a running turn is followed only once it ends", () => {
+ const { hook, hasResumedRef, refetchSession } = setup("streaming");
+
+ act(() => hook.result.current.followBackendTurn());
+ expect(refetchSession).not.toHaveBeenCalled();
+ expect(hasResumedRef.current).toBe(true);
+
+ hook.rerender({ status: "ready" });
+
+ expect(refetchSession).toHaveBeenCalledTimes(1);
+ expect(hasResumedRef.current).toBe(false);
+});
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatContainer/ChatContainer.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatContainer/ChatContainer.tsx
index dcdae0a13d..9ceec0dfe6 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatContainer/ChatContainer.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatContainer/ChatContainer.tsx
@@ -43,6 +43,7 @@ import { useAreWorkspaceFileCardsOpen } from "../../useAreWorkspaceFileCardsOpen
import type { SentFrom } from "../../sentFrom";
import type { AutopilotMode } from "../../autopilotModeStore";
import { AutopilotModeSelector } from "../ChatInput/components/AutopilotModeSelector/AutopilotModeSelector";
+import { isHeldCallRow } from "../ChatMessagesContainer/heldCallRows";
import {
getKickoffAttemptToken,
getKickoffExpertId,
@@ -113,12 +114,15 @@ export interface ChatContainerProps {
isAdoptingExpertSession?: boolean;
/** True until a newly hired expert's first kickoff has been handed off. */
isKickoffStarting?: boolean;
+ /** Follow a turn the server started, e.g. after an approval card is answered. */
+ onBackendTurn?: () => void;
/** The layout floats its sidebar/files controls over the chat's top-left
* corner on small viewports; the thread header clears them. */
hasFloatingControls?: boolean;
}
const NO_OP_SEND = () => undefined;
+const CONTINUE_AFTER_HELD_CALL = "Continue from where you left off.";
export const ChatContainer = ({
messages,
@@ -154,6 +158,7 @@ export const ChatContainer = ({
isResolvingExpertIdentity,
isAdoptingExpertSession,
isKickoffStarting,
+ onBackendTurn,
hasFloatingControls,
}: ChatContainerProps) => {
const isTaskBarEnabled = useGetFlag(Flag.TASK_PROGRESS_BAR);
@@ -268,7 +273,14 @@ export const ChatContainer = ({
// Retry: re-send the last user message (used by ErrorCard on transient errors).
const handleRetry = useCallback(() => {
- const lastUserMsg = [...messages].reverse().find((m) => m.role === "user");
+ const lastRow = [...messages].reverse().find((m) => m.role === "user");
+ // A turn an answered card started failed after the call ran: resuming it
+ // must not re-send the request that led to the call.
+ if (lastRow && isHeldCallRow(lastRow)) {
+ guardedOnSend(CONTINUE_AFTER_HELD_CALL);
+ return;
+ }
+ const lastUserMsg = lastRow;
const lastText = lastUserMsg?.parts
.filter(
(p): p is Extract<typeof p, { type: "text" }> => p.type === "text",
@@ -300,7 +312,10 @@ export const ChatContainer = ({
}, [guardedOnSend, messages]);
return (
- <CopilotChatActionsProvider onSend={guardedOnSend}>
+ <CopilotChatActionsProvider
+ onSend={guardedOnSend}
+ onBackendTurn={onBackendTurn}
+ >
<PendingAnswerContexts messages={messages}>
<LayoutGroup id="copilot-2-chat-layout">
<div className="flex h-full min-h-0 w-full flex-col px-2 lg:px-0">
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatContainer/__tests__/ChatContainer.test.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatContainer/__tests__/ChatContainer.test.tsx
index d5fe5e2443..49014c073e 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatContainer/__tests__/ChatContainer.test.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatContainer/__tests__/ChatContainer.test.tsx
@@ -295,6 +295,39 @@ describe("ChatContainer", () => {
);
});
+ it("resumes a failed turn an answered card started instead of re-sending a request", () => {
+ const onSend = vi.fn();
+ render(
+ <ChatContainer
+ {...baseProps}
+ onSend={onSend}
+ messages={[
+ {
+ id: "ask",
+ role: "user",
+ parts: [{ type: "text", text: "make two folders" }],
+ },
+ {
+ id: "result",
+ role: "user",
+ parts: [
+ {
+ type: "text",
+ text: "<held_call_result>made</held_call_result>",
+ },
+ ],
+ metadata: { held_call: { review_id: "r1" } },
+ },
+ ]}
+ />,
+ );
+
+ fireEvent.click(screen.getByRole("button", { name: "Retry message" }));
+
+ expect(onSend).toHaveBeenCalledTimes(1);
+ expect(onSend.mock.calls[0][0]).toBe("Continue from where you left off.");
+ });
+
it("does not render the shared-chat notice for unshared chats", async () => {
render(<ChatContainer {...baseProps} />);
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.tsx
index 78ceb2d9a8..f5fe89ad4e 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
@@ -21,6 +21,8 @@ import { useElapsedTimer } from "../JobStatsBar/useElapsedTimer";
import { CopilotPendingReviews } from "../CopilotPendingReviews/CopilotPendingReviews";
import type { TurnStatsMap } from "../../helpers/convertChatSessionToUiMessages";
import { hideKickoffMessages } from "../../expertKickoff";
+import { Text } from "@/components/atoms/Text/Text";
+import { countHeldCalls, getHeldCallRowKind } from "./heldCallRows";
import {
extractReviewTarget,
getLastCompactionCallId,
@@ -561,6 +563,20 @@ export function ChatMessagesContainer({
);
}
+ const heldCallRow = getHeldCallRowKind(message.metadata);
+ if (heldCallRow === "result") return null;
+ if (heldCallRow === "answered") {
+ return (
+ <Text
+ key={message.id}
+ variant="small"
+ className="py-1 text-center text-zinc-500"
+ >
+ Approval answered
+ </Text>
+ );
+ }
+
const isLastAssistant =
rowIndex === renderRows.length - 1 &&
message.role === "assistant";
@@ -826,8 +842,12 @@ export function ChatMessagesContainer({
graphId={reviewTarget.graphId}
/>
)}
- {!readOnly && reviewTarget?.kind === "chat" && sessionID && (
- <CopilotPendingReviews chatSessionId={sessionID} />
+ {!readOnly && sessionID && (
+ <CopilotPendingReviews
+ chatSessionId={sessionID}
+ pollWhileEmpty={reviewTarget?.kind === "chat"}
+ refetchKey={countHeldCalls(messages)}
+ />
)}
{!readOnly &&
queuedMessages?.map((msg, idx) => (
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.tsx
index 6822ef4ea6..68961838b1 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
@@ -1452,6 +1452,41 @@ describe("ChatMessagesContainer — mid-turn follow-up", () => {
});
});
+describe("ChatMessagesContainer — held call rows", () => {
+ it("never shows the server's wake row or a late result as the user's words", () => {
+ const messages: Message[] = [
+ {
+ id: "sess-123-seq-5",
+ role: "user" as const,
+ parts: [
+ {
+ type: "text" as const,
+ text: "I answered an action that was waiting for my approval.",
+ },
+ ],
+ metadata: { held_calls_answered: true },
+ },
+ {
+ id: "sess-123-seq-6",
+ role: "user" as const,
+ parts: [
+ {
+ type: "text" as const,
+ text: '<held_call_result tool="create_folder">folder_created Q3</held_call_result>',
+ },
+ ],
+ metadata: { held_call: { review_id: "r1" } },
+ },
+ ];
+
+ render(<ChatMessagesContainer {...baseProps} messages={messages} />);
+
+ expect(screen.queryByText(/I answered an action/)).toBeNull();
+ expect(screen.queryByText(/folder_created/)).toBeNull();
+ expect(screen.getByText("Approval answered")).toBeDefined();
+ });
+});
+
describe("ChatMessagesContainer — pending reviews", () => {
function withToolOutput(output: object): Message[] {
return [
@@ -1471,11 +1506,18 @@ describe("ChatMessagesContainer — pending reviews", () => {
];
}
- function mountedProps() {
- const el = screen.queryByTestId("pending-reviews");
- return el ? JSON.parse(el.getAttribute("data-props") ?? "{}") : null;
+ function mounted() {
+ return screen
+ .queryAllByTestId("pending-reviews")
+ .map((el) => JSON.parse(el.getAttribute("data-props") ?? "{}"));
}
+ const chatList = (pollWhileEmpty: boolean, refetchKey: number) => ({
+ chatSessionId: "sess-123",
+ pollWhileEmpty,
+ refetchKey,
+ });
+
afterEach(() => {
cleanup();
});
@@ -1494,7 +1536,7 @@ describe("ChatMessagesContainer — pending reviews", () => {
/>,
);
- expect(mountedProps()).toEqual({ chatSessionId: "sess-123" });
+ expect(mounted()).toEqual([chatList(true, 0)]);
});
it("finds a chat review stored before it had a session id of its own", () => {
@@ -1509,7 +1551,7 @@ describe("ChatMessagesContainer — pending reviews", () => {
/>,
);
- expect(mountedProps()).toEqual({ chatSessionId: "sess-123" });
+ expect(mounted()).toEqual([chatList(true, 0)]);
});
it("finds an agent run's reviews by its graph execution", () => {
@@ -1520,7 +1562,42 @@ describe("ChatMessagesContainer — pending reviews", () => {
/>,
);
- expect(mountedProps()).toEqual({ graphExecId: "exec-9" });
+ expect(mounted()).toEqual([{ graphExecId: "exec-9" }, chatList(false, 0)]);
+ });
+
+ it("keeps the chat's held cards loaded while a newer run is the review target", () => {
+ render(
+ <ChatMessagesContainer
+ {...baseProps}
+ messages={[
+ ...withToolOutput({ type: "approval_required", review_id: "r1" }),
+ ...withToolOutput({ execution_id: "exec-9", status: "RUNNING" }),
+ ]}
+ />,
+ );
+
+ // One held call on screen: fetched, but polled only if a card comes back.
+ expect(mounted()).toEqual([{ graphExecId: "exec-9" }, chatList(false, 1)]);
+ });
+
+ it("refetches the chat's list at once for a new held call while it is the target", () => {
+ render(
+ <ChatMessagesContainer
+ {...baseProps}
+ messages={withToolOutput({
+ type: "approval_required",
+ review_id: "r1",
+ })}
+ />,
+ );
+
+ expect(mounted()).toEqual([chatList(true, 1)]);
+ });
+
+ it("still finds held cards whose call has paged out of the loaded history", () => {
+ render(<ChatMessagesContainer {...baseProps} messages={[]} />);
+
+ expect(mounted()).toEqual([chatList(false, 0)]);
});
it("mounts nothing in a read-only transcript", () => {
@@ -1532,6 +1609,6 @@ describe("ChatMessagesContainer — pending reviews", () => {
/>,
);
- expect(mountedProps()).toBeNull();
+ expect(mounted()).toEqual([]);
});
});
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/heldCallRows.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/heldCallRows.ts
new file mode 100644
index 0000000000..57807e3efb
--- /dev/null
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/heldCallRows.ts
@@ -0,0 +1,48 @@
+import type { UIDataTypes, UIMessage, UITools } from "ai";
+
+// Rows the server writes for a held call: the one that starts the turn when a
+// card is answered, and the call's late result, which the reply narrates.
+export function getHeldCallRowKind(
+ metadata: unknown,
+): "answered" | "result" | null {
+ if (!metadata || typeof metadata !== "object") return null;
+ if ("held_call" in metadata) return "result";
+ if ("held_calls_answered" in metadata) return "answered";
+ return null;
+}
+
+export function isHeldCallRow(message: { metadata?: unknown }): boolean {
+ return getHeldCallRowKind(message.metadata) !== null;
+}
+
+// How many calls on screen were held. A new one means a new card to fetch;
+// an old one says nothing about whether its card is still open.
+export function countHeldCalls(
+ messages: UIMessage<unknown, UIDataTypes, UITools>[],
+): number {
+ return messages.reduce(
+ (count, message) =>
+ count +
+ message.parts.filter(
+ (part) => "output" in part && isHeldOutput(part.output),
+ ).length,
+ 0,
+ );
+}
+
+function isHeldOutput(output: unknown): boolean {
+ let value = output;
+ if (typeof value === "string") {
+ try {
+ value = JSON.parse(value);
+ } catch {
+ return false;
+ }
+ }
+ return (
+ !!value &&
+ typeof value === "object" &&
+ (value as { type?: unknown }).type === "approval_required" &&
+ typeof (value as { review_id?: unknown }).review_id === "string"
+ );
+}
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMinimap/__tests__/ChatMinimap.test.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMinimap/__tests__/ChatMinimap.test.tsx
index 2c8b34cd30..796c74b256 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMinimap/__tests__/ChatMinimap.test.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMinimap/__tests__/ChatMinimap.test.tsx
@@ -77,6 +77,21 @@ describe("minimap helpers", () => {
expect(entries[0].title).toBe("Your message");
});
+ it("never ticks a held call's rows as the user's words", () => {
+ const entries = toMinimapEntries([
+ textMessage("t1", "user", "make two folders"),
+ {
+ ...textMessage("t2", "user", "I answered an action"),
+ metadata: { held_calls_answered: true },
+ } as UIMessage,
+ {
+ ...textMessage("t3", "user", "<held_call_result>"),
+ metadata: { held_call: { review_id: "r1" } },
+ } as UIMessage,
+ ]);
+ expect(entries.map((e) => e.id)).toEqual(["t1"]);
+ });
+
it("truncates long titles with an ellipsis", () => {
const entries = toMinimapEntries([
textMessage("t1", "user", "x".repeat(100)),
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMinimap/helpers.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMinimap/helpers.ts
index 0c4b3160f8..a0850684ff 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMinimap/helpers.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMinimap/helpers.ts
@@ -1,4 +1,5 @@
import type { UIDataTypes, UIMessage, UITools } from "ai";
+import { isHeldCallRow } from "../ChatMessagesContainer/heldCallRows";
export interface MinimapEntry {
id: string;
@@ -16,7 +17,7 @@ export function toMinimapEntries(
messages: UIMessage<unknown, UIDataTypes, UITools>[],
): MinimapEntry[] {
return messages
- .filter((message) => message.role === "user")
+ .filter((message) => message.role === "user" && !isHeldCallRow(message))
.map((message) => {
const [title, body] = splitPreview(messageText(message));
return {
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/CopilotChatActionsProvider/CopilotChatActionsProvider.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/CopilotChatActionsProvider/CopilotChatActionsProvider.tsx
index e84b795730..165a7843a8 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/CopilotChatActionsProvider/CopilotChatActionsProvider.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/CopilotChatActionsProvider/CopilotChatActionsProvider.tsx
@@ -7,6 +7,7 @@ import {
interface Props {
onSend: (message: string) => void | Promise<void>;
+ onBackendTurn?: () => void;
/** Defaults to "copilot" — the standalone page. */
chatSurface?: CopilotChatSurface;
getExecutionShareToken?: (executionId: string) => string | null | undefined;
@@ -15,13 +16,14 @@ interface Props {
export function CopilotChatActionsProvider({
onSend,
+ onBackendTurn,
chatSurface = "copilot",
getExecutionShareToken,
children,
}: Props) {
return (
<CopilotChatActionsContext.Provider
- value={{ onSend, chatSurface, getExecutionShareToken }}
+ value={{ onSend, onBackendTurn, chatSurface, getExecutionShareToken }}
>
{children}
</CopilotChatActionsContext.Provider>
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/CopilotChatActionsProvider/useCopilotChatActions.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/components/CopilotChatActionsProvider/useCopilotChatActions.ts
index e36a47d5c2..987d07ee5c 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/CopilotChatActionsProvider/useCopilotChatActions.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/CopilotChatActionsProvider/useCopilotChatActions.ts
@@ -20,6 +20,8 @@ export type CopilotChatSurface = "copilot" | "builder" | "share";
interface CopilotChatActions {
onSend: (message: string) => void | Promise<void>;
+ /** Follow a turn the server started, e.g. after an approval card is answered. */
+ onBackendTurn?: () => void;
chatSurface: CopilotChatSurface;
/**
* Optional: maps execution_id → public share_token for the executions
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.tsx
index 8112a7a626..59985b60ff 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
@@ -1,30 +1,48 @@
"use client";
import { useCallback } from "react";
-import { PendingReviewsList } from "@/components/organisms/PendingReviewsList/PendingReviewsList";
+import {
+ COPILOT_GATE_NODE_PREFIX,
+ PendingReviewsList,
+} from "@/components/organisms/PendingReviewsList/PendingReviewsList";
import { useCopilotChatActions } from "../CopilotChatActionsProvider/useCopilotChatActions";
import { okData } from "@/app/api/helpers";
import { useCopilotPendingReviews } from "./useCopilotPendingReviews";
type Props =
| { graphExecId: string; graphId?: string }
- | { chatSessionId: string };
+ | { chatSessionId: string; pollWhileEmpty?: boolean; refetchKey?: number };
/**
- * Renders a single consolidated PendingReviewsList for the chat's own reviews
- * (run_capability, MCP, spend approval) or for an agent run the chat started
- * — mirrors the non-copilot review page behavior.
+ * Renders the chat's pending reviews, or those of an agent run it started:
+ * each held AutoPilot call as its own card, oldest first, and every block,
+ * MCP or run review in one consolidated list.
*/
export function CopilotPendingReviews(props: Props) {
- const { onSend } = useCopilotChatActions();
+ const { onSend, onBackendTurn } = useCopilotChatActions();
const graphExecId = "graphExecId" in props ? props.graphExecId : "";
const { pendingReviews, refetch } = useCopilotPendingReviews(props);
+ // A held call's own card runs it: one Approve per call, never one for all.
+ const heldCalls = pendingReviews.filter((r) =>
+ r.node_exec_id.startsWith(COPILOT_GATE_NODE_PREFIX),
+ );
+ const otherReviews = pendingReviews.filter(
+ (r) => !r.node_exec_id.startsWith(COPILOT_GATE_NODE_PREFIX),
+ );
+
+ async function handleHeldCallAnswered() {
+ await refetch();
+ onBackendTurn?.();
+ }
+
const handleReviewComplete = useCallback(async () => {
// Brief delay for the server to propagate the approval
await new Promise((resolve) => setTimeout(resolve, 500));
const result = await refetch();
- const remaining = okData(result.data) || [];
+ const remaining = (okData(result.data) || []).filter(
+ (r) => !r.node_exec_id.startsWith(COPILOT_GATE_NODE_PREFIX),
+ );
if (remaining.length > 0) return;
@@ -36,14 +54,10 @@ export function CopilotPendingReviews(props: Props) {
`Use view_agent_output with execution_id="${graphExecId}" to check the result.`,
);
} else {
- // Gate approvals are consumed by re-issuing the original tool call, not
- // by resume_capability — that is only for capability reviews.
onSend(
`All pending reviews have been processed. ` +
- `For an approved block or MCP review, call resume_capability with the ` +
- `corresponding review_id. For any other approved action, retry the ` +
- `tool call you were blocked on, with the same arguments. ` +
- `For rejected reviews, do not retry — tell me what you could not do.`,
+ `For any approved reviews, call resume_capability with the corresponding review_id to execute them. ` +
+ `For rejected reviews, no further action is needed.`,
);
}
}, [refetch, onSend, graphExecId]);
@@ -51,11 +65,20 @@ export function CopilotPendingReviews(props: Props) {
if (pendingReviews.length === 0) return null;
return (
- <div className="py-2">
- <PendingReviewsList
- reviews={pendingReviews}
- onReviewComplete={handleReviewComplete}
- />
+ <div className="flex flex-col gap-2 py-2">
+ {heldCalls.map((review) => (
+ <PendingReviewsList
+ key={review.node_exec_id}
+ reviews={[review]}
+ onReviewComplete={handleHeldCallAnswered}
+ />
+ ))}
+ {otherReviews.length > 0 && (
+ <PendingReviewsList
+ reviews={otherReviews}
+ onReviewComplete={handleReviewComplete}
+ />
+ )}
</div>
);
}
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/CopilotPendingReviews/__tests__/CopilotPendingReviews.held-calls.test.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/CopilotPendingReviews/__tests__/CopilotPendingReviews.held-calls.test.tsx
new file mode 100644
index 0000000000..6cf661f33b
--- /dev/null
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/CopilotPendingReviews/__tests__/CopilotPendingReviews.held-calls.test.tsx
@@ -0,0 +1,72 @@
+import userEvent from "@testing-library/user-event";
+import { http, HttpResponse } from "msw";
+import { expect, test, vi } from "vitest";
+import { getPostV2ProcessReviewActionMockHandler200 } from "@/app/api/__generated__/endpoints/executions/executions.msw";
+import type { PendingHumanReviewModel } from "@/app/api/__generated__/models/pendingHumanReviewModel";
+import type { ReviewRequest } from "@/app/api/__generated__/models/reviewRequest";
+import { server } from "@/mocks/mock-server";
+import { render, screen, waitFor } from "@/tests/integrations/test-utils";
+import { CopilotChatActionsProvider } from "../../CopilotChatActionsProvider/CopilotChatActionsProvider";
+import { CopilotPendingReviews } from "../CopilotPendingReviews";
+
+const CHAT = "s1";
+const NODE = "copilot-node-gate-post_to_chat_platform";
+
+function heldCall(id: string, text: string): PendingHumanReviewModel {
+ return {
+ node_exec_id: `${NODE}:${id}`,
+ node_id: NODE,
+ user_id: "u-1",
+ session_id: CHAT,
+ graph_exec_id: null,
+ graph_id: null,
+ graph_version: null,
+ payload: { tool: "post_to_chat_platform", arguments: { text } },
+ instructions: `Post to chat platform — ${text}`,
+ editable: false,
+ status: "WAITING",
+ created_at: new Date(),
+ };
+}
+
+function serveQueue(reviews: PendingHumanReviewModel[]) {
+ const sent: ReviewRequest[] = [];
+ server.use(
+ http.get(`*/api/review/session/${CHAT}`, () => HttpResponse.json(reviews)),
+ getPostV2ProcessReviewActionMockHandler200(async (info) => {
+ sent.push((await info.request.json()) as ReviewRequest);
+ return { approved_count: 1, rejected_count: 0, failed_count: 0 };
+ }),
+ );
+ return sent;
+}
+
+test("each held call is its own card, oldest first, and answering one follows the server's turn", async () => {
+ const sent = serveQueue([
+ heldCall("a", "first post"),
+ heldCall("b", "second post"),
+ ]);
+ const onSend = vi.fn();
+ const onBackendTurn = vi.fn();
+
+ render(
+ <CopilotChatActionsProvider onSend={onSend} onBackendTurn={onBackendTurn}>
+ <CopilotPendingReviews chatSessionId={CHAT} />
+ </CopilotChatActionsProvider>,
+ );
+
+ const approves = await screen.findAllByRole("button", { name: "Approve" });
+ expect(approves).toHaveLength(2);
+ const [first] = screen.getAllByText(/first post/);
+ const [second] = screen.getAllByText(/second post/);
+ expect(
+ first.compareDocumentPosition(second) & Node.DOCUMENT_POSITION_FOLLOWING,
+ ).toBeTruthy();
+
+ await userEvent.click(approves[0]);
+
+ await waitFor(() => expect(onBackendTurn).toHaveBeenCalledTimes(1));
+ expect(sent).toHaveLength(1);
+ expect(sent[0].reviews.map((r) => r.node_exec_id)).toEqual([`${NODE}:a`]);
+ expect(onSend).not.toHaveBeenCalled();
+});
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/CopilotPendingReviews/__tests__/useCopilotPendingReviews.test.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/CopilotPendingReviews/__tests__/useCopilotPendingReviews.test.tsx
index a6c6cb5b18..f62490861e 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/CopilotPendingReviews/__tests__/useCopilotPendingReviews.test.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/CopilotPendingReviews/__tests__/useCopilotPendingReviews.test.tsx
@@ -130,4 +130,60 @@ describe("useCopilotPendingReviews", () => {
expect(calls.reviews).toBeGreaterThan(settled);
}, 10_000);
+
+ test.each([
+ { rows: [], polls: false },
+ { rows: [{ node_exec_id: "copilot-node-gate-x:1" }], polls: true },
+ ])(
+ "a chat with nothing held on screen polls its cards only once it has one ($rows.length)",
+ async ({ rows, polls }) => {
+ let requests = 0;
+ server.use(
+ http.get("*/api/review/session/s1", () => {
+ requests++;
+ return HttpResponse.json(rows);
+ }),
+ );
+ renderHook(
+ () =>
+ useCopilotPendingReviews({
+ chatSessionId: "s1",
+ pollWhileEmpty: false,
+ }),
+ { wrapper },
+ );
+ await waitFor(() => expect(requests).toBeGreaterThan(0));
+
+ await sleep(4500);
+
+ expect(requests > 1).toBe(polls);
+ },
+ 15_000,
+ );
+
+ test("a new held call on screen fetches the chat's cards again", async () => {
+ let requests = 0;
+ server.use(
+ http.get("*/api/review/session/s1", () => {
+ requests++;
+ return HttpResponse.json([]);
+ }),
+ );
+ const { rerender } = renderHook(
+ ({ key }) =>
+ useCopilotPendingReviews({
+ chatSessionId: "s1",
+ pollWhileEmpty: false,
+ refetchKey: key,
+ }),
+ { wrapper, initialProps: { key: 0 } },
+ );
+ await waitFor(() => expect(requests).toBeGreaterThan(0));
+ await sleep(300);
+ const settled = requests;
+
+ rerender({ key: 1 });
+
+ await waitFor(() => expect(requests).toBeGreaterThan(settled));
+ }, 10_000);
});
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.ts
index cd678e2f99..63c0c78f2c 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,4 @@
-import { useEffect } from "react";
+import { useEffect, useState } from "react";
import { useGetV1GetExecutionDetails } from "@/app/api/__generated__/endpoints/graphs/graphs";
import { AgentExecutionStatus } from "@/app/api/__generated__/models/agentExecutionStatus";
import { okData } from "@/app/api/helpers";
@@ -12,6 +12,10 @@ interface Args {
graphExecId?: string;
graphId?: string;
chatSessionId?: string;
+ // Off for the chat's own list: it polls only while it holds cards, and
+ // fetches again whenever ``refetchKey`` changes (a new held call).
+ pollWhileEmpty?: boolean;
+ refetchKey?: number;
}
const POLL_MS = 2000;
@@ -22,7 +26,10 @@ export function useCopilotPendingReviews({
graphExecId = "",
graphId,
chatSessionId = "",
+ pollWhileEmpty = true,
+ refetchKey,
}: Args) {
+ const [hasRows, setHasRows] = useState(false);
const isRun = !!graphId;
// A run's chat message never changes, so its live status decides polling.
const { data: execution, isFetched } = useGetV1GetExecutionDetails(
@@ -45,7 +52,7 @@ export function useCopilotPendingReviews({
const forChat = usePendingReviewsForChatSession(chatSessionId, {
enabled: !!chatSessionId,
- refetchInterval: POLL_MS,
+ refetchInterval: pollWhileEmpty || hasRows ? POLL_MS : false,
});
const forRun = usePendingReviewsForExecution(graphExecId, {
enabled: !!graphExecId && (!isRun || !!status || statusUnavailable),
@@ -58,6 +65,14 @@ export function useCopilotPendingReviews({
});
const { pendingReviews, refetch } = chatSessionId ? forChat : forRun;
+ useEffect(() => {
+ setHasRows(pendingReviews.length > 0);
+ }, [pendingReviews.length]);
+
+ useEffect(() => {
+ if (refetchKey !== undefined) refetch();
+ }, [refetchKey, refetch]);
+
useEffect(() => {
if (status) refetch();
}, [status, refetch]);
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotPage.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotPage.ts
index b1fa02f72a..663fa4bc03 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotPage.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotPage.ts
@@ -169,6 +169,7 @@ export function useCopilotPage() {
isResolvingExpertIdentity || Boolean(expertIdentity?.isArchived);
const {
+ followBackendTurn,
messages: currentMessages,
setMessages,
sendMessage,
@@ -445,5 +446,6 @@ export function useCopilotPage() {
isResolvingExpertIdentity,
isAdoptingExpertSession,
isKickoffStarting: isKickoffResolving || isKickoffStarting,
+ followBackendTurn,
};
}
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotStream.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotStream.ts
index f94438863c..6ce50ffad7 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotStream.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotStream.ts
@@ -48,6 +48,7 @@ import { useCopilotStop } from "./useCopilotStop";
import { useHydrateOnStreamEnd } from "./useHydrateOnStreamEnd";
import { RESTORE_STALL_TIMEOUT_MS } from "./restoreConstants";
import { useStreamActivityWatchdog } from "./useStreamActivityWatchdog";
+import { useFollowBackendTurn } from "./useFollowBackendTurn";
import { useWakeResync } from "./useWakeResync";
/**
@@ -666,6 +667,12 @@ export function useCopilotStream({
};
});
+ const { followBackendTurn } = useFollowBackendTurn({
+ status,
+ refetchSession,
+ hasResumedRef,
+ });
+
// Wake detection: refetch + optional resume when the page becomes visible
// after being hidden for >30 s. See `useWakeResync` for details.
const { isSyncing } = useWakeResync({
@@ -856,6 +863,7 @@ export function useCopilotStream({
!hasConnectedThisMountRef.current;
return {
+ followBackendTurn,
messages,
setMessages,
sendMessage,
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/useFollowBackendTurn.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/useFollowBackendTurn.ts
new file mode 100644
index 0000000000..918df3956f
--- /dev/null
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/useFollowBackendTurn.ts
@@ -0,0 +1,38 @@
+import { useEffect, useRef } from "react";
+
+interface Args {
+ status: string;
+ refetchSession: () => Promise<{ data?: unknown }>;
+ hasResumedRef: React.MutableRefObject<boolean>;
+}
+
+// An answered approval card starts the chat's next turn on the server. The page
+// resumes a server-started stream once per mount, after hydration; re-arming
+// that resume lets the refetched rows land first, so the replay follows them.
+export function useFollowBackendTurn({
+ status,
+ refetchSession,
+ hasResumedRef,
+}: Args) {
+ const pendingRef = useRef(false);
+ const isBusy = status === "streaming" || status === "submitted";
+ const followRef = useRef(follow);
+ followRef.current = follow;
+
+ function follow() {
+ pendingRef.current = false;
+ hasResumedRef.current = false;
+ refetchSession();
+ }
+
+ useEffect(() => {
+ if (!isBusy && pendingRef.current) followRef.current();
+ }, [isBusy]);
+
+ function followBackendTurn() {
+ pendingRef.current = true;
+ if (!isBusy) follow();
+ }
+
+ return { followBackendTurn };
+}
diff --git a/autogpt_platform/frontend/src/components/organisms/PendingReviewsList/PendingReviewsList.tsx b/autogpt_platform/frontend/src/components/organisms/PendingReviewsList/PendingReviewsList.tsx
index 0f42fc5b4d..c2580d11e8 100644
--- a/autogpt_platform/frontend/src/components/organisms/PendingReviewsList/PendingReviewsList.tsx
+++ b/autogpt_platform/frontend/src/components/organisms/PendingReviewsList/PendingReviewsList.tsx
@@ -21,7 +21,7 @@ interface PendingReviewsListProps {
}
// Copilot action-gate approvals bind exact arguments; auto-approve cannot apply.
-const COPILOT_GATE_NODE_PREFIX = "copilot-node-gate-";
+export const COPILOT_GATE_NODE_PREFIX = "copilot-node-gate-";
export function PendingReviewsList({
reviews,