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.

fix(backend/copilot): restore langsmith reply spans broken by per-message tasks (#14830)

Nicholas Tindle27dd9e32fa1f
diff --git a/autogpt_platform/backend/backend/blocks/orchestrator.py b/autogpt_platform/backend/backend/blocks/orchestrator.py
index 490faff8a1..ebfacb22c8 100644
--- a/autogpt_platform/backend/backend/blocks/orchestrator.py
+++ b/autogpt_platform/backend/backend/blocks/orchestrator.py
@@ -1,4 +1,5 @@
import asyncio
+import contextvars
import logging
import re
import shutil
@@ -1818,13 +1819,19 @@ class OrchestratorBlock(Block):
# Run SDK client with heartbeat-safe message iteration.
# We must NOT cancel __anext__() mid-flight — doing so corrupts
# the SDK's internal anyio memory stream (same pattern as
- # copilot/sdk/service.py:_iter_sdk_messages).
+ # copilot/sdk/service.py:_iter_sdk_messages). Every fetch task
+ # runs in one shared context: once configure_claude_agent_sdk()
+ # has wrapped the client, langsmith's tracing wrapper keeps the
+ # conversation run in a ContextVar set during the first fetch,
+ # and a fresh context per task would drop every later reply's
+ # span from the trace.
_HEARTBEAT_INTERVAL = 10.0 # seconds
async with ClaudeSDKClient(options=options) as client:
await client.query(user_message)
msg_iter = client.receive_response().__aiter__()
+ fetch_context = contextvars.copy_context()
pending_task: asyncio.Task[Any] | None = None
async def _next_msg() -> Any:
@@ -1833,7 +1840,9 @@ class OrchestratorBlock(Block):
try:
while True:
if pending_task is None:
- pending_task = asyncio.create_task(_next_msg())
+ pending_task = asyncio.create_task(
+ _next_msg(), context=fetch_context
+ )
done, _ = await asyncio.wait(
{pending_task}, timeout=_HEARTBEAT_INTERVAL
diff --git a/autogpt_platform/backend/backend/blocks/test/test_orchestrator_sdk_tracing.py b/autogpt_platform/backend/backend/blocks/test/test_orchestrator_sdk_tracing.py
new file mode 100644
index 0000000000..b4a990c38c
--- /dev/null
+++ b/autogpt_platform/backend/backend/blocks/test/test_orchestrator_sdk_tracing.py
@@ -0,0 +1,136 @@
+"""langsmith's Claude Agent SDK tracing must survive the orchestrator's SDK mode.
+
+The tracing wrapper around ``receive_response()`` keeps the
+``claude.conversation`` run in a ContextVar that it sets while the first
+message is fetched, and parents every ``claude.assistant.turn`` run on it.
+``_execute_tools_sdk_mode`` fetches each message in its own task, so unless
+every fetch runs in the same context, replies after the first message find no
+parent and are silently dropped from the trace.
+"""
+
+from collections.abc import AsyncGenerator
+from typing import Any
+from unittest.mock import MagicMock, patch
+
+import claude_agent_sdk
+import pytest
+from claude_agent_sdk import (
+ AssistantMessage,
+ ClaudeAgentOptions,
+ ResultMessage,
+ SystemMessage,
+ TextBlock,
+ ToolResultBlock,
+ ToolUseBlock,
+ UserMessage,
+)
+from langsmith import tracing_context
+from langsmith.integrations.claude_agent_sdk import configure_claude_agent_sdk
+from langsmith.run_trees import RunTree
+from pydantic import SecretStr
+
+from backend.blocks.orchestrator import OrchestratorBlock
+from backend.data.model import APIKeyCredentials
+
+
+class _ScriptedClient:
+ """Stands in for ``ClaudeSDKClient`` and replays one agent turn."""
+
+ def __init__(self, options: ClaudeAgentOptions) -> None:
+ self.options = options
+
+ async def __aenter__(self) -> "_ScriptedClient":
+ return self
+
+ async def __aexit__(self, *_: object) -> None:
+ return None
+
+ async def query(self, prompt: str) -> None:
+ return None
+
+ async def receive_response(self) -> AsyncGenerator[Any, None]:
+ yield SystemMessage(subtype="init", data={})
+ yield AssistantMessage(
+ content=[ToolUseBlock(id="toolu_1", name="lookup", input={})],
+ model="claude-test",
+ message_id="msg_1",
+ )
+ yield UserMessage(content=[ToolResultBlock(tool_use_id="toolu_1")])
+ yield AssistantMessage(
+ content=[TextBlock(text="done")], model="claude-test", message_id="msg_2"
+ )
+ yield ResultMessage(
+ subtype="success",
+ duration_ms=1,
+ duration_api_ms=1,
+ is_error=False,
+ num_turns=2,
+ session_id="sess-1",
+ )
+
+
+def traced_client() -> type[_ScriptedClient]:
+ """Wrap the scripted client exactly as production wraps ``ClaudeSDKClient``.
+
+ ``configure_claude_agent_sdk`` patches whichever class
+ ``claude_agent_sdk.ClaudeSDKClient`` names, so pointing that name at the
+ scripted client for the call wraps it through the public API. The real
+ tool class and the global trace config are left untouched.
+ """
+ with (
+ patch.object(claude_agent_sdk, "ClaudeSDKClient", _ScriptedClient),
+ patch.object(claude_agent_sdk, "SdkMcpTool", None),
+ patch("langsmith.integrations.claude_agent_sdk.set_tracing_config"),
+ ):
+ assert configure_claude_agent_sdk()
+ return _ScriptedClient
+
+
+def posted_runs(monkeypatch: pytest.MonkeyPatch) -> list[RunTree]:
+ """Record every run the tracing wrapper posts instead of exporting it."""
+ posted: list[RunTree] = []
+ monkeypatch.setattr(RunTree, "post", lambda self, *_, **__: posted.append(self))
+ monkeypatch.setattr(RunTree, "patch", lambda self, *_, **__: None)
+ return posted
+
+
+async def test_sdk_mode_replies_get_assistant_turn_runs(
+ traced_client: type[_ScriptedClient],
+ posted_runs: list[RunTree],
+) -> None:
+ input_data = MagicMock(sys_prompt="", prompt="list the files")
+ input_data.model.value = "claude-test"
+ credentials = APIKeyCredentials(
+ id="test-anthropic-id",
+ provider="anthropic",
+ api_key=SecretStr("mock-anthropic-key"),
+ title="Mock Anthropic key",
+ expires_at=None,
+ )
+
+ with (
+ patch.object(claude_agent_sdk, "ClaudeSDKClient", traced_client),
+ tracing_context(enabled=True),
+ ):
+ outputs = [
+ output
+ async for output in OrchestratorBlock()._execute_tools_sdk_mode(
+ input_data=input_data,
+ credentials=credentials,
+ tool_functions=[],
+ prompt=[{"role": "user", "content": "list the files"}],
+ execution_params=MagicMock(graph_exec_id="graph-exec-1"),
+ execution_processor=MagicMock(),
+ )
+ ]
+
+ assert ("finished", "done") in outputs
+ conversation = next(run for run in posted_runs if run.name == "claude.conversation")
+ turns = [run for run in posted_runs if run.name == "claude.assistant.turn"]
+ assert [turn.parent_run_id for turn in turns] == [
+ conversation.id,
+ conversation.id,
+ ], "each assistant reply should get a turn run under the conversation"
diff --git a/autogpt_platform/backend/backend/copilot/sdk/langsmith_tracing_test.py b/autogpt_platform/backend/backend/copilot/sdk/langsmith_tracing_test.py
new file mode 100644
index 0000000000..1a0acd1424
--- /dev/null
+++ b/autogpt_platform/backend/backend/copilot/sdk/langsmith_tracing_test.py
@@ -0,0 +1,119 @@
+"""langsmith's Claude Agent SDK tracing must survive ``_iter_sdk_messages``.
+
+The tracing wrapper around ``receive_response()`` keeps the
+``claude.conversation`` run in a ContextVar that it sets while the first
+message is fetched, and parents every ``claude.assistant.turn`` run on it.
+``_iter_sdk_messages`` fetches each message in its own task, so unless every
+fetch runs in the same context, main-agent replies after the first message
+find no parent and are silently dropped from the trace.
+"""
+
+from collections.abc import AsyncGenerator
+from typing import Any, cast
+from unittest.mock import patch
+
+import claude_agent_sdk
+import pytest
+from claude_agent_sdk import (
+ AssistantMessage,
+ ClaudeAgentOptions,
+ ClaudeSDKClient,
+ ResultMessage,
+ SystemMessage,
+ TextBlock,
+ ToolResultBlock,
+ ToolUseBlock,
+ UserMessage,
+)
+from langsmith import tracing_context
+from langsmith.integrations.claude_agent_sdk import configure_claude_agent_sdk
+from langsmith.run_trees import RunTree
+
+from .service import _iter_sdk_messages
+
+
+class _ScriptedClient:
+ """Stands in for ``ClaudeSDKClient`` and replays one main-agent turn."""
+
+ def __init__(self, options: ClaudeAgentOptions) -> None:
+ self.options = options
+
+ async def query(self, prompt: str) -> None:
+ return None
+
+ async def receive_response(self) -> AsyncGenerator[Any, None]:
+ yield SystemMessage(subtype="init", data={})
+ yield AssistantMessage(
+ content=[ToolUseBlock(id="toolu_1", name="bash_exec", input={})],
+ model="claude-test",
+ message_id="msg_1",
+ )
+ yield UserMessage(content=[ToolResultBlock(tool_use_id="toolu_1")])
+ yield AssistantMessage(
+ content=[TextBlock(text="done")], model="claude-test", message_id="msg_2"
+ )
+ yield ResultMessage(
+ subtype="success",
+ duration_ms=1,
+ duration_api_ms=1,
+ is_error=False,
+ num_turns=2,
+ session_id="sess-1",
+ )
+
+
+def traced_client() -> type[_ScriptedClient]:
+ """Wrap the scripted client exactly as production wraps ``ClaudeSDKClient``.
+
+ ``configure_claude_agent_sdk`` patches whichever class
+ ``claude_agent_sdk.ClaudeSDKClient`` names, so pointing that name at the
+ scripted client for the call wraps it through the public API. The real
+ tool class and the global trace config are left untouched.
+ """
+ with (
+ patch.object(claude_agent_sdk, "ClaudeSDKClient", _ScriptedClient),
+ patch.object(claude_agent_sdk, "SdkMcpTool", None),
+ patch("langsmith.integrations.claude_agent_sdk.set_tracing_config"),
+ ):
+ assert configure_claude_agent_sdk()
+ return _ScriptedClient
+
+
+def posted_runs(monkeypatch: pytest.MonkeyPatch) -> list[RunTree]:
+ """Record every run the tracing wrapper posts instead of exporting it."""
+ posted: list[RunTree] = []
+ monkeypatch.setattr(RunTree, "post", lambda self, *_, **__: posted.append(self))
+ monkeypatch.setattr(RunTree, "patch", lambda self, *_, **__: None)
+ return posted
+
+
+async def test_main_agent_replies_get_assistant_turn_runs(
+ traced_client: type[_ScriptedClient],
+ posted_runs: list[RunTree],
+) -> None:
+ client = traced_client(options=ClaudeAgentOptions())
+ await client.query("list the files")
+
+ with tracing_context(enabled=True):
+ received = [
+ msg
+ async for msg in _iter_sdk_messages(cast(ClaudeSDKClient, client))
+ if msg is not None
+ ]
+
+ assert [type(msg).__name__ for msg in received] == [
+ "SystemMessage",
+ "AssistantMessage",
+ "UserMessage",
+ "AssistantMessage",
+ "ResultMessage",
+ ]
+ conversation = next(run for run in posted_runs if run.name == "claude.conversation")
+ turns = [run for run in posted_runs if run.name == "claude.assistant.turn"]
+ assert [turn.parent_run_id for turn in turns] == [
+ conversation.id,
+ conversation.id,
+ ], "each main-agent reply should get a turn run under the conversation"
diff --git a/autogpt_platform/backend/backend/copilot/sdk/service.py b/autogpt_platform/backend/backend/copilot/sdk/service.py
index 663992f7a3..c1d8a0edcb 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/service.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/service.py
@@ -4,6 +4,7 @@
import asyncio
import contextlib
+import contextvars
import base64
import functools
from copy import copy
@@ -2084,12 +2085,21 @@ async def _iter_sdk_messages(
timeout. On timeout we yield a heartbeat sentinel but keep the Task
alive so it can deliver the next message.
+ Every fetch Task runs in one context, copied once up front. The
+ langsmith tracing wrapper around ``receive_response()`` stores the
+ ``claude.conversation`` run in a ContextVar during the first fetch and
+ parents each reply's ``claude.assistant.turn`` span on it; a Task given
+ a fresh copy of the caller's context per message never sees that run,
+ so every reply after the first message would drop out of the trace.
+ Only one fetch is in flight at a time, so sharing the context is safe.
+
Yields `None` on heartbeat timeout (caller should refresh locks and
emit heartbeat events). Yields the raw SDK message otherwise.
On stream end (`StopAsyncIteration`), the generator returns normally.
Any other exception from the SDK propagates to the caller.
"""
msg_iter = client.receive_response().__aiter__()
+ fetch_context = contextvars.copy_context()
pending_task: asyncio.Task[Any] | None = None
wake_tasks: dict[asyncio.Task[bool], asyncio.Event] = {}
@@ -2100,7 +2110,7 @@ async def _iter_sdk_messages(
try:
while True:
if pending_task is None:
- pending_task = asyncio.create_task(_next_msg())
+ pending_task = asyncio.create_task(_next_msg(), context=fetch_context)
waiters: set[asyncio.Task[Any]] = {pending_task}
for event in (wake, tool_display_wake):
if event is not None and event not in wake_tasks.values():