AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
fix(platform/copilot): keep an approved call's result out of the user's bubbles (#15484)
Reinier van der Leer7b790626e452
diff --git a/autogpt_platform/backend/backend/copilot/pending_message_helpers.py b/autogpt_platform/backend/backend/copilot/pending_message_helpers.pyindex 197698c760..52f9f6faa4 100644--- a/autogpt_platform/backend/backend/copilot/pending_message_helpers.py+++ b/autogpt_platform/backend/backend/copilot/pending_message_helpers.py@@ -563,11 +563,13 @@ def drained_rows_entry(content_of: Callable[[PendingMessage], str] = lambda pm: pm.content,) -> StreamPendingDrained:"""The stream entry for the user rows ``persist_pending_as_user_rows``- landed: one message per row, with the row's own content."""+ landed: one message per row, with the row's own content and metadata."""return StreamPendingDrained(drainedCount=len(pending),messages=[- StreamPendingDrainedMessage(id=pm.id, content=content_of(pm))+ StreamPendingDrainedMessage(+ id=pm.id, content=content_of(pm), metadata=pm.metadata or None+ )for pm in pending],)diff --git a/autogpt_platform/backend/backend/copilot/response_model.py b/autogpt_platform/backend/backend/copilot/response_model.pyindex e23d033ec4..60e12e8ba6 100644--- a/autogpt_platform/backend/backend/copilot/response_model.py+++ b/autogpt_platform/backend/backend/copilot/response_model.py@@ -482,6 +482,9 @@ class StreamPendingDrainedMessage(BaseModel):id: str = Field(description="Stable id of the drained pending message")content: str = Field(description="Raw text the user typed mid-turn")+ metadata: dict[str, Any] | None = Field(+ default=None, description="The metadata persisted on the row's user message"+ )class StreamPendingDrained(StreamBaseResponse):@@ -520,7 +523,7 @@ class StreamPendingDrained(StreamBaseResponse):"type": self.type.value,"data": {"drainedCount": self.drainedCount,- "messages": [m.model_dump() for m in self.messages],+ "messages": [m.model_dump(exclude_none=True) for m in self.messages],},}return f"data: {json.dumps(data)}\n\n"diff --git a/autogpt_platform/backend/backend/copilot/stream_drift/recorded_turns_test.py b/autogpt_platform/backend/backend/copilot/stream_drift/recorded_turns_test.pyindex f45dd8bd2b..ae2ff0a0ea 100644--- a/autogpt_platform/backend/backend/copilot/stream_drift/recorded_turns_test.py+++ b/autogpt_platform/backend/backend/copilot/stream_drift/recorded_turns_test.py@@ -6,8 +6,9 @@ A red here means the sender changed; re-record (see ``recording.py``) and rerunthe frontend suite."""+import jsonimport uuid-from unittest.mock import AsyncMock+from unittest.mock import AsyncMock, MagicMock, patchimport pytestfrom claude_agent_sdk import (@@ -23,11 +24,20 @@ from claude_agent_sdk import (from prisma.models import Userfrom backend.copilot.baseline import service as baseline+from backend.copilot.constants import COPILOT_NODE_PREFIX+from backend.copilot.gate import heldfrom backend.copilot.model import ChatMessagefrom backend.copilot.pending_messages import PendingMessagefrom backend.copilot.response_model import StreamToolOutputAvailablefrom backend.copilot.sdk import dummyfrom backend.copilot.sdk import service as sdk+from backend.copilot.sdk.tool_adapter import cap_late_tool_result+from backend.copilot.tools import base+from backend.copilot.tools.models import (+ ApprovalRequiredResponse,+ MCPToolInfo,+ MCPToolsDiscoveredResponse,+)from backend.data import db, redis_clientfrom backend.data.user import get_or_create_userfrom backend.util.testing import is_tcp_port_reachable@@ -251,6 +261,103 @@ async def test_sdk_auto_continue_turn(user_id: str) -> None:check_fixture("sdk-auto-continue-turn", recorded)+async def test_sdk_held_result_turn(user_id: str) -> None:+ """An approved held read's late result opens the woken turn: the card's+ own turn, the wake row, then the result row the hint draws live."""+ call = held.HeldCall(+ review_id=f"{COPILOT_NODE_PREFIX}gate-read-run_capability:0a1b2c3d4e5f6a7b",+ tool_name="run_capability",+ tool_call_id="sdk-0a1b2c3d4e5f",+ args={"id": "mcp.example.com", "action": "list_tools"},+ )+ card_turn = [+ ChatMessage(role="user", content="Which CRM tools can you use?"),+ ChatMessage(+ role="assistant",+ content="",+ tool_calls=[+ {+ "id": call.tool_call_id,+ "type": "function",+ "function": {+ "name": call.tool_name,+ "arguments": json.dumps(call.args),+ },+ }+ ],+ ),+ ChatMessage(+ role="tool",+ tool_call_id=call.tool_call_id,+ content=ApprovalRequiredResponse(+ message="Content withheld pending your review.",+ tool_name=call.tool_name,+ reason="Content withheld pending your review: tools mcp.example.com.",+ review_id=call.review_id,+ ask="Read",+ object="mcp.example.com",+ ).model_dump_json(),+ ),+ ChatMessage(role="assistant", content="That needs your approval first."),+ ]+ session = await persisted_session(+ user_id,+ held.WAKE_MESSAGE,+ history=card_turn,+ metadata={"held_calls_answered": True},+ )+ result = held._result_row(+ call,+ cap_late_tool_result(await _digested(_discovered_tools(), call.tool_call_id)),+ "approved",+ )+ turn_id = str(uuid.uuid4())++ recorded = await record_turn(+ sdk_service_turn(+ session,+ turn_id,+ [_reply("You can use 20 CRM tools.")],+ queued_after_first=[],+ held_results=[result],+ ),+ session=session,+ turn_id=turn_id,+ )++ check_fixture("sdk-held-result-turn", recorded)+++def _discovered_tools() -> str:+ """Large enough to be digested; its ``params`` mark required fields with ``*``."""+ return MCPToolsDiscoveredResponse(+ message="Discovered 20 tool(s) on mcp.example.com.",+ server_url="https://mcp.example.com/mcp",+ tools=[+ MCPToolInfo(+ name=f"manage_objects_{i}",+ description="Creates or updates CRM objects. " * 15,+ params="operationType*, createRequest, updateRequest",+ )+ for i in range(20)+ ],+ ).model_dump_json(exclude_none=True)+++async def _digested(output: str, tool_call_id: str) -> str:+ """The large-output preview the tool hands the model, persisted nowhere."""+ workspaces = MagicMock(+ get_or_create_workspace=AsyncMock(return_value=MagicMock(id="ws"))+ )+ with (+ patch.object(base, "workspace_db", return_value=workspaces),+ patch.object(base, "WorkspaceManager", return_value=AsyncMock()),+ ):+ return await base._persist_and_summarize(+ output, "drift-user", "drift-session", tool_call_id, digest=True+ )++def _reply(text: str) -> list:return [SystemMessage(subtype="init", data={}),diff --git a/autogpt_platform/backend/backend/copilot/stream_drift/recording.py b/autogpt_platform/backend/backend/copilot/stream_drift/recording.pyindex e25a4ae8db..a76ba8f638 100644--- a/autogpt_platform/backend/backend/copilot/stream_drift/recording.py+++ b/autogpt_platform/backend/backend/copilot/stream_drift/recording.py@@ -103,11 +103,17 @@ async def record_turn(async def persisted_session(- user_id: str, prompt: str, *, history: Sequence[ChatMessage] = ()+ user_id: str,+ prompt: str,+ *,+ history: Sequence[ChatMessage] = (),+ metadata: dict[str, Any] | None = None,) -> ChatSession:"""A session whose prompt is already persisted, as the POST route leaves it."""session = ChatSession.new(user_id, dry_run=False)- session.messages.extend([*history, ChatMessage(role="user", content=prompt)])+ session.messages.extend(+ [*history, ChatMessage(role="user", content=prompt, metadata=metadata)]+ )await upsert_chat_session(session)return sessiondiff --git a/autogpt_platform/backend/backend/copilot/stream_drift/scripted.py b/autogpt_platform/backend/backend/copilot/stream_drift/scripted.pyindex a9d08ecc6a..c7134da954 100644--- a/autogpt_platform/backend/backend/copilot/stream_drift/scripted.py+++ b/autogpt_platform/backend/backend/copilot/stream_drift/scripted.py@@ -149,11 +149,13 @@ async def sdk_service_turn(queries: list[list[Message]],*,queued_after_first: list[PendingMessage],+ held_results: list[PendingMessage] | None = None,) -> AsyncGenerator[StreamBaseResponse, None]:"""The whole SDK engine over a scripted CLI, one list of messages perquery, with ``queued_after_first`` arriving while the first one runs:- its turn-end persist and auto-continue included. Only what reaches- beyond the process (CLI, sandbox, RPC services, billing) is stubbed."""+ its turn-end persist and auto-continue included. ``held_results`` are the+ answered cards' results the turn opens with. Only what reaches beyond the+ process (CLI, sandbox, RPC services, billing) is stubbed."""scripted = iter(queries)def cli(*_: Any, **__: Any) -> AsyncMock:@@ -195,7 +197,7 @@ async def sdk_service_turn("drain_pending_safe": AsyncMock(side_effect=[[], queued_after_first] + [[]] * len(queries)),- "resolve_answered": AsyncMock(return_value=[]),+ "resolve_answered": AsyncMock(return_value=held_results or []),"build_session_context": AsyncMock(return_value=""),"build_skills_context": AsyncMock(return_value=""),"_maybe_prepend_skills_update": AsyncMock(side_effect=lambda *a: a[-1]),diff --git a/autogpt_platform/backend/test/fixtures/copilot_stream/baseline-drain-turn/entries.jsonl b/autogpt_platform/backend/test/fixtures/copilot_stream/baseline-drain-turn/entries.jsonlindex 1f881af369..ebc134c4fc 100644--- a/autogpt_platform/backend/test/fixtures/copilot_stream/baseline-drain-turn/entries.jsonl+++ b/autogpt_platform/backend/test/fixtures/copilot_stream/baseline-drain-turn/entries.jsonl@@ -1,7 +1,7 @@{"id": "1-0", "data": {"type": "data-status", "message": "Message received…"}}{"id": "2-0", "data": {"type": "data-status", "message": "Setting up your environment…"}}{"id": "3-0", "data": {"type": "start", "messageId": "00000000-0000-4000-8000-000000000001", "sessionId": "drift-session"}}-{"id": "4-0", "data": {"type": "data-pending-drained", "drainedCount": 1, "messages": [{"id": "00000000-0000-4000-8000-000000000002", "content": "Keep it short."}]}}+{"id": "4-0", "data": {"type": "data-pending-drained", "drainedCount": 1, "messages": [{"id": "00000000-0000-4000-8000-000000000002", "content": "Keep it short.", "metadata": null}]}}{"id": "5-0", "data": {"type": "start-step"}}{"id": "6-0", "data": {"type": "reasoning-start", "id": "00000000-0000-4000-8000-000000000003"}}{"id": "7-0", "data": {"type": "reasoning-delta", "id": "00000000-0000-4000-8000-000000000003", "delta": "The user wants "}}@@ -14,7 +14,7 @@{"id": "14-0", "data": {"type": "tool-input-start", "toolCallId": "call-fetch", "toolName": "web_fetch"}}{"id": "15-0", "data": {"type": "tool-input-available", "toolCallId": "call-fetch", "toolName": "web_fetch", "input": {"url": "example.com"}}}{"id": "16-0", "data": {"type": "tool-output-available", "toolCallId": "call-fetch", "output": "Example Domain", "toolName": "web_fetch", "success": true}}-{"id": "17-0", "data": {"type": "data-pending-drained", "drainedCount": 1, "messages": [{"id": "00000000-0000-4000-8000-000000000005", "content": "In French, please."}]}}+{"id": "17-0", "data": {"type": "data-pending-drained", "drainedCount": 1, "messages": [{"id": "00000000-0000-4000-8000-000000000005", "content": "In French, please.", "metadata": null}]}}{"id": "18-0", "data": {"type": "data-checkpoint", "rows": 5, "sequence": 1, "digest": "630cc061bcb09f33a99719db8a07e65f79152226f69db47702f7a59ea02b17be"}}{"id": "19-0", "data": {"type": "start-step"}}{"id": "20-0", "data": {"type": "reasoning-start", "id": "00000000-0000-4000-8000-000000000006"}}diff --git a/autogpt_platform/backend/test/fixtures/copilot_stream/sdk-auto-continue-turn/entries.jsonl b/autogpt_platform/backend/test/fixtures/copilot_stream/sdk-auto-continue-turn/entries.jsonlindex b5c5b06b6b..01fef05bec 100644--- a/autogpt_platform/backend/test/fixtures/copilot_stream/sdk-auto-continue-turn/entries.jsonl+++ b/autogpt_platform/backend/test/fixtures/copilot_stream/sdk-auto-continue-turn/entries.jsonl@@ -13,7 +13,7 @@{"id": "13-0", "data": {"type": "data-checkpoint", "rows": 1, "sequence": 3, "digest": "171cf7547b79de2f13e2cbd3830a9773ac270ed1582c197f54309a03334f9490"}}{"id": "14-0", "data": {"type": "data-status", "message": "Restoring your session…"}}{"id": "15-0", "data": {"type": "start", "messageId": "00000000-0000-4000-8000-000000000003", "sessionId": "drift-session"}}-{"id": "16-0", "data": {"type": "data-pending-drained", "drainedCount": 2, "messages": [{"id": "00000000-0000-4000-8000-000000000004", "content": "Mention the new export."}, {"id": "00000000-0000-4000-8000-000000000005", "content": "And keep it to one line."}]}}+{"id": "16-0", "data": {"type": "data-pending-drained", "drainedCount": 2, "messages": [{"id": "00000000-0000-4000-8000-000000000004", "content": "Mention the new export.", "metadata": null}, {"id": "00000000-0000-4000-8000-000000000005", "content": "And keep it to one line.", "metadata": null}]}}{"id": "17-0", "data": {"type": "data-status", "message": "Preparing conversation context…"}}{"id": "18-0", "data": {"type": "data-status", "message": "Starting the assistant…"}}{"id": "19-0", "data": {"type": "data-status", "message": "Reading your message…"}}diff --git a/autogpt_platform/backend/test/fixtures/copilot_stream/sdk-held-result-turn/entries.jsonl b/autogpt_platform/backend/test/fixtures/copilot_stream/sdk-held-result-turn/entries.jsonlnew file mode 100644index 0000000000..0739d8b73b--- /dev/null+++ b/autogpt_platform/backend/test/fixtures/copilot_stream/sdk-held-result-turn/entries.jsonl@@ -0,0 +1,15 @@+{"id": "1-0", "data": {"type": "data-status", "message": "Message received…"}}+{"id": "2-0", "data": {"type": "data-status", "message": "Setting up your environment…"}}+{"id": "3-0", "data": {"type": "data-status", "message": "Restoring your session…"}}+{"id": "4-0", "data": {"type": "start", "messageId": "00000000-0000-4000-8000-000000000001", "sessionId": "drift-session"}}+{"id": "5-0", "data": {"type": "data-status", "message": "Preparing conversation context…"}}+{"id": "6-0", "data": {"type": "data-pending-drained", "drainedCount": 1, "messages": [{"id": "00000000-0000-4000-8000-000000000002", "content": "<held_call_result tool=\"run_capability\" tool_call_id=\"sdk-0a1b2c3d4e5f\" review_id=\"copilot-node-gate-read-run_capability:0a1b2c3d4e5f6a7b\">\n<tool-output-truncated total_chars=11768 workspace_path=\"tool-outputs/sdk-0a1b2c3d4e5f.json\" format=\"outline\">\n$: {type=\"mcp_tools_discovered\", message=\"Discovered 20 tool(s) on mcp.example.com.\", server_url=\"https://mcp.example.com/mcp\", tools=[…20 items]}\n$.tools: [0={…3 keys @137+580}, 1={…3 keys @718+580}, 2={…3 keys @1299+580}, 3={…3 keys @1880+580}, 4={…3 keys @2461+580}, 5={…3 keys @3042+580}, 6={…3 keys @3623+580}, 7={…3 keys @4204+580}, 8={…3 keys @4785+580}, 9={…3 keys @5366+580}, 10={…3 keys @5947+581}, 11={…3 keys @6529+581}, 12={…3 keys @7111+581}, 13={…3 keys @7693+581}, 14={…3 keys @8275+581}, 15={…3 keys @8857+581}, 16={…3 keys @9439+581}, 17={…3 keys @10021+581}, 18={…3 keys @10603+581}, 19={…3 keys @11185+581}]\n$.tools[0]: {name=\"manage_objects_0\", description=\"Creates or updates CRM objects. Creates or updates CRM objects. Creates or updates CRM objects. Creates or updates CRM…, params=\"operationType*, createRequest, updateRequest\"}\n$.tools[1]: {name=\"manage_objects_1\", description=\"Creates or updates CRM objects. Creates or updates CRM objects. Creates or updates CRM objects. Creates or updates CRM…, params=\"operationType*, createRequest, updateRequest\"}\n$.tools[2]: {name=\"manage_objects_2\", description=\"Creates or updates CRM objects. Creates or updates CRM objects. Creates or updates CRM objects. Creates or updates CRM…, params=\"operationType*, createRequest, updateRequest\"}\n$.tools[3]: {name=\"manage_objects_3\", description=\"Creates or updates CRM objects. Creates or updates CRM objects. Creates or updates CRM objects. Creates or updates CRM…, params=\"operationType*, createRequest, updateRequest\"}\n$.tools[4]: {name=\"manage_objects_4\", description=\"Creates or updates CRM objects. Creates or updates CRM objects. Creates or updates CRM objects. Creates or updates CRM…, params=\"operationType*, createRequest, updateRequest\"}\n$.tools[6]: {name=\"manage_objects_6\", description=\"Creates or updates CRM objects. Creates or updates CRM objects. Creates or updates CRM objects. Creates or updates CRM…, params=\"operationT…\n… 14 more nodes not shown; read their windows\n\nThe preview above is a structural outline of the 11,768-char output, not a literal prefix of it. Each `@offset+length` is that node's exact window in the file: read_workspace_file(path=\"tool-outputs/sdk-0a1b2c3d4e5f.json\", offset=<offset>, length=<length>) returns it and nothing else.\nTo process the file in the sandbox/working dir, use read_workspace_file(path=\"tool-outputs/sdk-0a1b2c3d4e5f.json\", save_to_path=\"<working_dir>/sdk-0a1b2c3d4e5f.json\") first, then use bash_exec to work with the local copy.\n</tool-output-truncated>\n</held_call_result>", "metadata": {"held_call": {"review_id": "copilot-node-gate-read-run_capability:0a1b2c3d4e5f6a7b", "tool_name": "run_capability", "tool_call_id": "sdk-0a1b2c3d4e5f", "outcome": "approved"}}}]}}+{"id": "7-0", "data": {"type": "data-status", "message": "Starting the assistant…"}}+{"id": "8-0", "data": {"type": "data-status", "message": "Reading your message…"}}+{"id": "9-0", "data": {"type": "start-step"}}+{"id": "10-0", "data": {"type": "text-start", "id": "00000000-0000-4000-8000-000000000003"}}+{"id": "11-0", "data": {"type": "text-delta", "id": "00000000-0000-4000-8000-000000000003", "delta": "You can use 20 CRM tools."}}+{"id": "12-0", "data": {"type": "text-end", "id": "00000000-0000-4000-8000-000000000003"}}+{"id": "13-0", "data": {"type": "finish-step"}}+{"id": "14-0", "data": {"type": "data-checkpoint", "rows": 2, "sequence": 5, "digest": "d050cf15c9bb1825dc463d21c3e5a6db42e814e9e6c1c4ea8c5bafc24f8bf299"}}+{"id": "15-0", "data": {"type": "finish"}}diff --git a/autogpt_platform/backend/test/fixtures/copilot_stream/sdk-held-result-turn/frames.jsonl b/autogpt_platform/backend/test/fixtures/copilot_stream/sdk-held-result-turn/frames.jsonlnew file mode 100644index 0000000000..8f8f511713--- /dev/null+++ b/autogpt_platform/backend/test/fixtures/copilot_stream/sdk-held-result-turn/frames.jsonl@@ -0,0 +1,15 @@+{"id": "1-0", "sse": "id: drift-turn:1-0\ndata: {\"type\": \"data-status\", \"data\": {\"message\": \"Message received\\u2026\"}}\n\n"}+{"id": "2-0", "sse": "id: drift-turn:2-0\ndata: {\"type\": \"data-status\", \"data\": {\"message\": \"Setting up your environment\\u2026\"}}\n\n"}+{"id": "3-0", "sse": "id: drift-turn:3-0\ndata: {\"type\": \"data-status\", \"data\": {\"message\": \"Restoring your session\\u2026\"}}\n\n"}+{"id": "4-0", "sse": "id: drift-turn:4-0\ndata: {\"type\": \"start\", \"messageId\": \"00000000-0000-4000-8000-000000000001\"}\n\n"}+{"id": "5-0", "sse": "id: drift-turn:5-0\ndata: {\"type\": \"data-status\", \"data\": {\"message\": \"Preparing conversation context\\u2026\"}}\n\n"}+{"id": "6-0", "sse": "id: drift-turn:6-0\ndata: {\"type\": \"data-pending-drained\", \"data\": {\"drainedCount\": 1, \"messages\": [{\"id\": \"00000000-0000-4000-8000-000000000002\", \"content\": \"<held_call_result tool=\\\"run_capability\\\" tool_call_id=\\\"sdk-0a1b2c3d4e5f\\\" review_id=\\\"copilot-node-gate-read-run_capability:0a1b2c3d4e5f6a7b\\\">\\n<tool-output-truncated total_chars=11768 workspace_path=\\\"tool-outputs/sdk-0a1b2c3d4e5f.json\\\" format=\\\"outline\\\">\\n$: {type=\\\"mcp_tools_discovered\\\", message=\\\"Discovered 20 tool(s) on mcp.example.com.\\\", server_url=\\\"https://mcp.example.com/mcp\\\", tools=[\\u202620 items]}\\n$.tools: [0={\\u20263 keys @137+580}, 1={\\u20263 keys @718+580}, 2={\\u20263 keys @1299+580}, 3={\\u20263 keys @1880+580}, 4={\\u20263 keys @2461+580}, 5={\\u20263 keys @3042+580}, 6={\\u20263 keys @3623+580}, 7={\\u20263 keys @4204+580}, 8={\\u20263 keys @4785+580}, 9={\\u20263 keys @5366+580}, 10={\\u20263 keys @5947+581}, 11={\\u20263 keys @6529+581}, 12={\\u20263 keys @7111+581}, 13={\\u20263 keys @7693+581}, 14={\\u20263 keys @8275+581}, 15={\\u20263 keys @8857+581}, 16={\\u20263 keys @9439+581}, 17={\\u20263 keys @10021+581}, 18={\\u20263 keys @10603+581}, 19={\\u20263 keys @11185+581}]\\n$.tools[0]: {name=\\\"manage_objects_0\\\", description=\\\"Creates or updates CRM objects. Creates or updates CRM objects. Creates or updates CRM objects. Creates or updates CRM\\u2026, params=\\\"operationType*, createRequest, updateRequest\\\"}\\n$.tools[1]: {name=\\\"manage_objects_1\\\", description=\\\"Creates or updates CRM objects. Creates or updates CRM objects. Creates or updates CRM objects. Creates or updates CRM\\u2026, params=\\\"operationType*, createRequest, updateRequest\\\"}\\n$.tools[2]: {name=\\\"manage_objects_2\\\", description=\\\"Creates or updates CRM objects. Creates or updates CRM objects. Creates or updates CRM objects. Creates or updates CRM\\u2026, params=\\\"operationType*, createRequest, updateRequest\\\"}\\n$.tools[3]: {name=\\\"manage_objects_3\\\", description=\\\"Creates or updates CRM objects. Creates or updates CRM objects. Creates or updates CRM objects. Creates or updates CRM\\u2026, params=\\\"operationType*, createRequest, updateRequest\\\"}\\n$.tools[4]: {name=\\\"manage_objects_4\\\", description=\\\"Creates or updates CRM objects. Creates or updates CRM objects. Creates or updates CRM objects. Creates or updates CRM\\u2026, params=\\\"operationType*, createRequest, updateRequest\\\"}\\n$.tools[6]: {name=\\\"manage_objects_6\\\", description=\\\"Creates or updates CRM objects. Creates or updates CRM objects. Creates or updates CRM objects. Creates or updates CRM\\u2026, params=\\\"operationT\\u2026\\n\\u2026 14 more nodes not shown; read their windows\\n\\nThe preview above is a structural outline of the 11,768-char output, not a literal prefix of it. Each `@offset+length` is that node's exact window in the file: read_workspace_file(path=\\\"tool-outputs/sdk-0a1b2c3d4e5f.json\\\", offset=<offset>, length=<length>) returns it and nothing else.\\nTo process the file in the sandbox/working dir, use read_workspace_file(path=\\\"tool-outputs/sdk-0a1b2c3d4e5f.json\\\", save_to_path=\\\"<working_dir>/sdk-0a1b2c3d4e5f.json\\\") first, then use bash_exec to work with the local copy.\\n</tool-output-truncated>\\n</held_call_result>\", \"metadata\": {\"held_call\": {\"review_id\": \"copilot-node-gate-read-run_capability:0a1b2c3d4e5f6a7b\", \"tool_name\": \"run_capability\", \"tool_call_id\": \"sdk-0a1b2c3d4e5f\", \"outcome\": \"approved\"}}}]}}\n\n"}+{"id": "7-0", "sse": "id: drift-turn:7-0\ndata: {\"type\": \"data-status\", \"data\": {\"message\": \"Starting the assistant\\u2026\"}}\n\n"}+{"id": "8-0", "sse": "id: drift-turn:8-0\ndata: {\"type\": \"data-status\", \"data\": {\"message\": \"Reading your message\\u2026\"}}\n\n"}+{"id": "9-0", "sse": "id: drift-turn:9-0\ndata: {\"type\":\"start-step\"}\n\n"}+{"id": "10-0", "sse": "id: drift-turn:10-0\ndata: {\"type\":\"text-start\",\"id\":\"00000000-0000-4000-8000-000000000003\"}\n\n"}+{"id": "11-0", "sse": "id: drift-turn:11-0\ndata: {\"type\":\"text-delta\",\"id\":\"00000000-0000-4000-8000-000000000003\",\"delta\":\"You can use 20 CRM tools.\"}\n\n"}+{"id": "12-0", "sse": "id: drift-turn:12-0\ndata: {\"type\":\"text-end\",\"id\":\"00000000-0000-4000-8000-000000000003\"}\n\n"}+{"id": "13-0", "sse": "id: drift-turn:13-0\ndata: {\"type\":\"finish-step\"}\n\n"}+{"id": "14-0", "sse": "id: drift-turn:14-0\ndata: {\"type\": \"data-checkpoint\", \"data\": {\"rows\": 2, \"sequence\": 5, \"digest\": \"d050cf15c9bb1825dc463d21c3e5a6db42e814e9e6c1c4ea8c5bafc24f8bf299\"}, \"transient\": true}\n\n"}+{"id": "15-0", "sse": "id: drift-turn:15-0\ndata: {\"type\":\"finish\"}\n\n"}diff --git a/autogpt_platform/backend/test/fixtures/copilot_stream/sdk-held-result-turn/rows.json b/autogpt_platform/backend/test/fixtures/copilot_stream/sdk-held-result-turn/rows.jsonnew file mode 100644index 0000000000..e5fd9a4c97--- /dev/null+++ b/autogpt_platform/backend/test/fixtures/copilot_stream/sdk-held-result-turn/rows.json@@ -0,0 +1,146 @@+[+ {+ "id": "row-0",+ "role": "user",+ "content": "Which CRM tools can you use?",+ "name": null,+ "tool_call_id": null,+ "refusal": null,+ "tool_calls": null,+ "function_call": null,+ "sequence": 0,+ "duration_ms": null,+ "created_at": "2026-09-30T00:00:00Z",+ "model": null,+ "llm_auth_provider": null,+ "llm_credential_id": null,+ "session_id": "drift-session",+ "metadata": null+ },+ {+ "id": "row-1",+ "role": "assistant",+ "content": "",+ "name": null,+ "tool_call_id": null,+ "refusal": null,+ "tool_calls": [+ {+ "id": "sdk-0a1b2c3d4e5f",+ "type": "function",+ "function": {+ "name": "run_capability",+ "arguments": "{\"id\": \"mcp.example.com\", \"action\": \"list_tools\"}"+ }+ }+ ],+ "function_call": null,+ "sequence": 1,+ "duration_ms": null,+ "created_at": "2026-09-30T00:00:01Z",+ "model": null,+ "llm_auth_provider": null,+ "llm_credential_id": null,+ "session_id": "drift-session",+ "metadata": null+ },+ {+ "id": "row-2",+ "role": "tool",+ "content": "{\"type\":\"approval_required\",\"message\":\"Content withheld pending your review.\",\"session_id\":null,\"tool_name\":\"run_capability\",\"reason\":\"Content withheld pending your review: tools mcp.example.com.\",\"review_id\":\"copilot-node-gate-read-run_capability:0a1b2c3d4e5f6a7b\",\"ask\":\"Read\",\"object\":\"mcp.example.com\"}",+ "name": null,+ "tool_call_id": "sdk-0a1b2c3d4e5f",+ "refusal": null,+ "tool_calls": null,+ "function_call": null,+ "sequence": 2,+ "duration_ms": null,+ "created_at": "2026-09-30T00:00:02Z",+ "model": null,+ "llm_auth_provider": null,+ "llm_credential_id": null,+ "session_id": "drift-session",+ "metadata": null+ },+ {+ "id": "row-3",+ "role": "assistant",+ "content": "That needs your approval first.",+ "name": null,+ "tool_call_id": null,+ "refusal": null,+ "tool_calls": null,+ "function_call": null,+ "sequence": 3,+ "duration_ms": null,+ "created_at": "2026-09-30T00:00:03Z",+ "model": null,+ "llm_auth_provider": null,+ "llm_credential_id": null,+ "session_id": "drift-session",+ "metadata": null+ },+ {+ "id": "row-4",+ "role": "user",+ "content": "I answered an action that was waiting for my approval.",+ "name": null,+ "tool_call_id": null,+ "refusal": null,+ "tool_calls": null,+ "function_call": null,+ "sequence": 4,+ "duration_ms": null,+ "created_at": "2026-09-30T00:00:04Z",+ "model": null,+ "llm_auth_provider": null,+ "llm_credential_id": null,+ "session_id": "drift-session",+ "metadata": {+ "held_calls_answered": true+ }+ },+ {+ "id": "row-5",+ "role": "user",+ "content": "<held_call_result tool=\"run_capability\" tool_call_id=\"sdk-0a1b2c3d4e5f\" review_id=\"copilot-node-gate-read-run_capability:0a1b2c3d4e5f6a7b\">\n<tool-output-truncated total_chars=11768 workspace_path=\"tool-outputs/sdk-0a1b2c3d4e5f.json\" format=\"outline\">\n$: {type=\"mcp_tools_discovered\", message=\"Discovered 20 tool(s) on mcp.example.com.\", server_url=\"https://mcp.example.com/mcp\", tools=[…20 items]}\n$.tools: [0={…3 keys @137+580}, 1={…3 keys @718+580}, 2={…3 keys @1299+580}, 3={…3 keys @1880+580}, 4={…3 keys @2461+580}, 5={…3 keys @3042+580}, 6={…3 keys @3623+580}, 7={…3 keys @4204+580}, 8={…3 keys @4785+580}, 9={…3 keys @5366+580}, 10={…3 keys @5947+581}, 11={…3 keys @6529+581}, 12={…3 keys @7111+581}, 13={…3 keys @7693+581}, 14={…3 keys @8275+581}, 15={…3 keys @8857+581}, 16={…3 keys @9439+581}, 17={…3 keys @10021+581}, 18={…3 keys @10603+581}, 19={…3 keys @11185+581}]\n$.tools[0]: {name=\"manage_objects_0\", description=\"Creates or updates CRM objects. Creates or updates CRM objects. Creates or updates CRM objects. Creates or updates CRM…, params=\"operationType*, createRequest, updateRequest\"}\n$.tools[1]: {name=\"manage_objects_1\", description=\"Creates or updates CRM objects. Creates or updates CRM objects. Creates or updates CRM objects. Creates or updates CRM…, params=\"operationType*, createRequest, updateRequest\"}\n$.tools[2]: {name=\"manage_objects_2\", description=\"Creates or updates CRM objects. Creates or updates CRM objects. Creates or updates CRM objects. Creates or updates CRM…, params=\"operationType*, createRequest, updateRequest\"}\n$.tools[3]: {name=\"manage_objects_3\", description=\"Creates or updates CRM objects. Creates or updates CRM objects. Creates or updates CRM objects. Creates or updates CRM…, params=\"operationType*, createRequest, updateRequest\"}\n$.tools[4]: {name=\"manage_objects_4\", description=\"Creates or updates CRM objects. Creates or updates CRM objects. Creates or updates CRM objects. Creates or updates CRM…, params=\"operationType*, createRequest, updateRequest\"}\n$.tools[6]: {name=\"manage_objects_6\", description=\"Creates or updates CRM objects. Creates or updates CRM objects. Creates or updates CRM objects. Creates or updates CRM…, params=\"operationT…\n… 14 more nodes not shown; read their windows\n\nThe preview above is a structural outline of the 11,768-char output, not a literal prefix of it. Each `@offset+length` is that node's exact window in the file: read_workspace_file(path=\"tool-outputs/sdk-0a1b2c3d4e5f.json\", offset=<offset>, length=<length>) returns it and nothing else.\nTo process the file in the sandbox/working dir, use read_workspace_file(path=\"tool-outputs/sdk-0a1b2c3d4e5f.json\", save_to_path=\"<working_dir>/sdk-0a1b2c3d4e5f.json\") first, then use bash_exec to work with the local copy.\n</tool-output-truncated>\n</held_call_result>",+ "name": null,+ "tool_call_id": null,+ "refusal": null,+ "tool_calls": null,+ "function_call": null,+ "sequence": 5,+ "duration_ms": null,+ "created_at": "2026-09-30T00:00:05Z",+ "model": null,+ "llm_auth_provider": null,+ "llm_credential_id": null,+ "session_id": "drift-session",+ "metadata": {+ "held_call": {+ "outcome": "approved",+ "review_id": "copilot-node-gate-read-run_capability:0a1b2c3d4e5f6a7b",+ "tool_name": "run_capability",+ "tool_call_id": "sdk-0a1b2c3d4e5f"+ }+ }+ },+ {+ "id": "row-6",+ "role": "assistant",+ "content": "You can use 20 CRM tools.",+ "name": null,+ "tool_call_id": null,+ "refusal": null,+ "tool_calls": null,+ "function_call": null,+ "sequence": 6,+ "duration_ms": null,+ "created_at": "2026-09-30T00:00:06Z",+ "model": "drift",+ "llm_auth_provider": "platform",+ "llm_credential_id": null,+ "session_id": "drift-session",+ "metadata": null+ }+]diff --git a/autogpt_platform/backend/test/fixtures/copilot_stream/sdk-reasoning-turn/entries.jsonl b/autogpt_platform/backend/test/fixtures/copilot_stream/sdk-reasoning-turn/entries.jsonlindex e5dc6d0e28..4024404296 100644--- a/autogpt_platform/backend/test/fixtures/copilot_stream/sdk-reasoning-turn/entries.jsonl+++ b/autogpt_platform/backend/test/fixtures/copilot_stream/sdk-reasoning-turn/entries.jsonl@@ -8,7 +8,7 @@{"id": "8-0", "data": {"type": "tool-input-start", "toolCallId": "bash-1", "toolName": "bash_exec"}}{"id": "9-0", "data": {"type": "tool-input-available", "toolCallId": "bash-1", "toolName": "bash_exec", "input": {"command": "ls"}}}{"id": "10-0", "data": {"type": "tool-output-available", "toolCallId": "bash-1", "output": "a.md b.py", "toolName": "bash_exec", "success": true}}-{"id": "11-0", "data": {"type": "data-pending-drained", "drainedCount": 1, "messages": [{"id": "00000000-0000-4000-8000-000000000003", "content": "Only count markdown files."}]}}+{"id": "11-0", "data": {"type": "data-pending-drained", "drainedCount": 1, "messages": [{"id": "00000000-0000-4000-8000-000000000003", "content": "Only count markdown files.", "metadata": null}]}}{"id": "12-0", "data": {"type": "finish-step"}}{"id": "13-0", "data": {"type": "data-status", "message": "Analyzing result…"}}{"id": "14-0", "data": {"type": "start-step"}}diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/drift/backend-sim.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/drift/backend-sim.tsindex 28a707e046..320c6dda1c 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/drift/backend-sim.ts+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/drift/backend-sim.ts@@ -81,7 +81,7 @@ export function createBackendSim(plans: TurnPlan[]) {state.startedAt = new Date().toISOString();state.trimmedBefore = 0;state.running = true;- state.current = leadingUserRows(plans[index].turn.rows);+ state.current = rowsBeforeTurn(plans[index].turn, turns[index]);published.set(index, 0);}@@ -358,9 +358,20 @@ function frameData(frame: { sse: string }) {return line ? line.slice("data: ".length) : null;}-function leadingUserRows(rows: Row[]) {- const firstOther = rows.findIndex((row) => row.role !== "user");- return firstOther === -1 ? rows : rows.slice(0, firstOther);+// What the GET holds as the turn starts: everything before the rows its+// checkpoints name, history and wake row included, else its leading prompt.+function rowsBeforeTurn(+ turn: RecordedTurn,+ { recordedCheckpoints }: ReturnType<typeof simulatedTurn>,+) {+ const [first] = recordedCheckpoints.values();+ if (first) {+ return turn.rows.filter(+ (row) => Number(row.sequence ?? 0) < first.sequence,+ );+ }+ const firstOther = turn.rows.findIndex((row) => row.role !== "user");+ return firstOther === -1 ? turn.rows : turn.rows.slice(0, firstOther);}function numbered(rows: Row[]): Row[] {diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/drift/stream-drift-content.test.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/drift/stream-drift-content.test.tsxindex 3f177222b0..31eb8cdd8e 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/drift/stream-drift-content.test.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/drift/stream-drift-content.test.tsx@@ -1,4 +1,5 @@-import { afterEach, beforeEach, describe, it, vi } from "vitest";+import { screen } from "@testing-library/react";+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";import { STREAM_PATHS, TEST_BACKEND_BASE_URL } from "../sse-helpers";import {createBackendSim,@@ -56,6 +57,7 @@ vi.mock("@/components/molecules/Toast/use-toast", async (importActual) => {const toolTurn = loadRecordedTurn("baseline-tool-turn");const textTurn = loadRecordedTurn("dummy-text-turn");+const heldTurn = loadRecordedTurn("sdk-held-result-turn");// Frame 6 is the first block's second delta, 12 the tool's output, and// 13-17 the whole second text block.const SECOND_DELTA = 6;@@ -151,6 +153,48 @@ describe.each(STREAM_PATHS)("on the %s path", (path) => {},);+ it(+ "approval wake: an approved call's result never paints as the user's message",+ { timeout: 60_000 },+ async () => {+ const [prompt] = heldTurn.rows;+ const sim = createBackendSim([{ turn: heldTurn }]);+ sim.beginRunning();+ renderAgainst(sim);+ const painted = sampleTranscripts();+ await waitForConnections(sim, 1);+ sim.publish();++ const labelled = await screen+ .findByText(+ 'Approval answered: Read "mcp.example.com". Result sent to Otto.',+ undefined,+ { timeout: 15_000 },+ )+ .then(+ () => true,+ () => false,+ );+ const report = await reportDrift({+ sim,+ turns: [heldTurn],+ painted,+ toast: toastMock,+ });+ const otherUserBubbles = painted.samples+ .flat()+ .filter(+ (entry) =>+ entry.role === "user" &&+ !entry.text.startsWith(String(prompt.content)),+ )+ .map((entry) => entry.text.slice(0, 80));+ expect(otherUserBubbles).toEqual([]);+ expect(labelled).toBe(true);+ expectDrift(report, {});+ },+ );+it("W8: a reply whose final persist failed stays on screen",{ timeout: 60_000 },diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/ChatMessagesContainer.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/ChatMessagesContainer.tsxindex 4ded8b8713..2e1cd9c478 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@@ -24,7 +24,11 @@ import { CopilotPendingReviews } from "../CopilotPendingReviews/CopilotPendingReimport type { TurnStatsMap } from "../../helpers/convertChatSessionToUiMessages";import { hideKickoffMessages } from "../../expertKickoff";import { Text } from "@/components/atoms/Text/Text";-import { countHeldCalls, getHeldCallRowKind } from "./heldCallRows";+import {+ answeredLabel,+ countHeldCalls,+ getHeldCallRowKind,+} from "./heldCallRows";import {extractReviewTarget,getLastCompactionCallId,@@ -551,7 +555,7 @@ export function ChatMessagesContainer({variant="small"className="py-1 text-center text-zinc-500">- Approval answered+ {answeredLabel(renderRows, rowIndex)}</Text>);}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.tsindex c0628752e5..f58d3390b0 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/heldCallRows.ts+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/heldCallRows.ts@@ -1,5 +1,7 @@+import { AUTOPILOT_NAME } from "@/components/molecules/AutopilotAvatar/helpers";import type { UIDataTypes, UIMessage, UITools } from "ai";-import { cardArguments } from "../ApprovalQueue/helpers";+import { cardArguments, fallbackAsk } from "../ApprovalQueue/helpers";+import { heldAskText, heldToolName } from "../ToolChain/heldRow";// 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.@@ -31,7 +33,42 @@ export function countHeldCalls();}+// What a wake row says: the cards its turn delivered, by their own headlines.+export function answeredLabel(+ messages: UIMessage<unknown, UIDataTypes, UITools>[],+ wakeIndex: number,+): string {+ const asks = new Map<string, string>();+ for (const message of messages.slice(0, wakeIndex)) {+ for (const part of message.parts) {+ if (!("output" in part)) continue;+ const card = heldCardOutput(part.output);+ const reviewId = card ? str(card.review_id) : null;+ if (!card || !reviewId) continue;+ asks.set(reviewId, heldAskText(card, heldToolName(card, partTool(part))));+ }+ }+ const names: string[] = [];+ for (const message of messages.slice(wakeIndex + 1)) {+ if (getHeldCallRowKind(message.metadata) !== "result") break;+ const held = (message.metadata as { held_call: Record<string, unknown> })+ .held_call;+ const tool = str(held?.tool_name);+ const name =+ asks.get(str(held?.review_id) ?? "") ?? (tool && fallbackAsk(tool));+ if (name) names.push(name);+ }+ return names.length+ ? `Approval answered: ${names.join(", ")}. Result sent to ${AUTOPILOT_NAME}.`+ : "Approval answered";+}+function heldReviewId(output: unknown): string | null {+ const reviewId = heldCardOutput(output)?.review_id;+ return typeof reviewId === "string" ? reviewId : null;+}++function heldCardOutput(output: unknown): Record<string, unknown> | null {let value = output;if (typeof value === "string") {try {@@ -46,8 +83,16 @@ function heldReviewId(output: unknown): string | null {(value as { type?: unknown }).type !== "approval_required")return null;- const reviewId = (value as { review_id?: unknown }).review_id;- return typeof reviewId === "string" ? reviewId : null;+ return value as Record<string, unknown>;+}++function partTool(part: { type: string; toolName?: unknown }): string {+ if (part.type === "dynamic-tool") return str(part.toolName) ?? "";+ return part.type.startsWith("tool-") ? part.type.slice(5) : "";+}++function str(value: unknown): string | null {+ return typeof value === "string" && value ? value : null;}export type HeldOutcomeKind =diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/stream/turnConverter.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/stream/turnConverter.tsindex fe7c5d4976..5aa738e420 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/stream/turnConverter.ts+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/stream/turnConverter.ts@@ -286,10 +286,15 @@ function protocolError(function drainedMessages(data: unknown) {const messages = record(data).messages;- return (Array.isArray(messages) ? messages : []).map((m) => ({- id: str(record(m).id),- content: str(record(m).content),- }));+ return (Array.isArray(messages) ? messages : []).map((m) => {+ const metadata = record(m).metadata;+ return {+ id: str(record(m).id),+ content: str(record(m).content),+ metadata:+ metadata && typeof metadata === "object" ? record(metadata) : null,+ };+ });}export function jsonEqual(a: unknown, b: unknown): boolean {diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/stream/turnLog.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/stream/turnLog.tsindex 0a38f09ff3..ecc05e4f6b 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/stream/turnLog.ts+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/stream/turnLog.ts@@ -236,17 +236,22 @@ export function setToolDisplayName(/*** Row rule 8: drained follow-ups are user rows, keyed by pending message id,- * at the hint's place in the stream.+ * at the hint's place in the stream, with the metadata their stored row has.*/export function appendDrainedUserRows(log: TurnLog,- messages: readonly { id: string; content: string }[],+ messages: readonly {+ id: string;+ content: string;+ metadata?: Record<string, unknown> | null;+ }[],) {const known = new Set(log.rows.map((r) => r.key));const rows = messages.map((m) => ({...assistantRow(`user:${m.id}`, m.content),role: "user" as const,+ metadata: m.metadata ?? null,})).filter((row) => !known.has(row.key));return rows.reduce(pushRow, log);diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/stream/turnShadow.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/stream/turnShadow.tsindex 4566c054ff..e33213a0d9 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/stream/turnShadow.ts+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/stream/turnShadow.ts@@ -270,6 +270,10 @@ function rowDifferences(a: LogRow | undefined, b: LogRow | undefined) {} else if (a.content !== b.content) {fields.push("content");}+ // A drained row's metadata decides how it renders (a held call's result is hidden).+ if (a.role === "user" && !jsonEqual(a.metadata, b.metadata)) {+ fields.push("metadata");+ }const sameCalls =a.toolCalls.length === b.toolCalls.length &&a.toolCalls.every((call, i) => {