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(copilot): dedupe tool row, lift bash_exec timeout, Stop+resend recovery (#12862)

Zamil Majdya8226af7259e
diff --git a/autogpt_platform/backend/backend/copilot/baseline/service.py b/autogpt_platform/backend/backend/copilot/baseline/service.py
index 4c6ad04d60..7d27beac8b 100644
--- a/autogpt_platform/backend/backend/copilot/baseline/service.py
+++ b/autogpt_platform/backend/backend/copilot/baseline/service.py
@@ -38,7 +38,6 @@ from backend.copilot.model import (
from backend.copilot.pending_message_helpers import (
combine_pending_with_current,
drain_pending_safe,
- pending_texts_from,
persist_pending_as_user_rows,
persist_session_safe,
)
@@ -70,6 +69,7 @@ from backend.copilot.service import (
inject_user_context,
strip_user_context_tags,
)
+from backend.copilot.session_cleanup import prune_orphan_tool_calls
from backend.copilot.thinking_stripper import ThinkingStripper as _ThinkingStripper
from backend.copilot.token_tracking import persist_and_record_usage
from backend.copilot.tools import execute_tool, get_available_tools
@@ -948,6 +948,12 @@ async def stream_chat_completion_baseline(
f"Session {session_id} not found. Please create a new session first."
)
+ # Drop orphan tool_use + trailing stop-marker rows left by a previous
+ # Stop mid-tool-call so the new turn starts from a well-formed message list.
+ prune_orphan_tool_calls(
+ session.messages, log_prefix=f"[Baseline] [{session_id[:12]}]"
+ )
+
# Strip any user-injected <user_context> tags on every turn.
# Only the server-injected prefix on the first message is trusted.
if message:
@@ -982,7 +988,6 @@ async def stream_chat_completion_baseline(
len(drained_at_start_pending),
session_id,
)
- drained_at_start_content = pending_texts_from(drained_at_start_pending)
# Chronological combine: pending typed BEFORE this /stream
# request's arrival go ahead of ``message``; race-path follow-ups
# typed AFTER (queued while /stream was still processing) go
diff --git a/autogpt_platform/backend/backend/copilot/constants.py b/autogpt_platform/backend/backend/copilot/constants.py
index 9a7388ab1b..986a641c7e 100644
--- a/autogpt_platform/backend/backend/copilot/constants.py
+++ b/autogpt_platform/backend/backend/copilot/constants.py
@@ -9,6 +9,11 @@ COPILOT_RETRYABLE_ERROR_PREFIX = (
)
COPILOT_SYSTEM_PREFIX = "[__COPILOT_SYSTEM_e3b0__]" # Renders as system info message
+# Canonical marker appended as an assistant ChatMessage when the SDK stream
+# ends without a ResultMessage (user hit Stop). Checked by exact equality
+# at turn start so the next turn's --resume transcript doesn't carry it.
+STOPPED_BY_USER_MARKER = f"{COPILOT_SYSTEM_PREFIX} Execution stopped by user"
+
# Prefix for all synthetic IDs generated by CoPilot block execution.
# Used to distinguish CoPilot-generated records from real graph execution records
# in PendingHumanReview and other tables.
diff --git a/autogpt_platform/backend/backend/copilot/executor/processor.py b/autogpt_platform/backend/backend/copilot/executor/processor.py
index 8a25e1a1d9..f40264b70b 100644
--- a/autogpt_platform/backend/backend/copilot/executor/processor.py
+++ b/autogpt_platform/backend/backend/copilot/executor/processor.py
@@ -361,26 +361,34 @@ class CoPilotProcessor:
permissions=entry.permissions,
request_arrival_at=entry.request_arrival_at,
)
- async for chunk in stream_registry.stream_and_publish(
+ published_stream = stream_registry.stream_and_publish(
session_id=entry.session_id,
turn_id=entry.turn_id,
stream=raw_stream,
- ):
- if cancel.is_set():
- log.info("Cancel requested, breaking stream")
- break
-
- # Capture StreamError so mark_session_completed receives
- # the error message (stream_and_publish yields but does
- # not publish StreamError — that's done by mark_session_completed).
- if isinstance(chunk, StreamError):
- error_msg = chunk.errorText
- break
-
- current_time = time.monotonic()
- if current_time - last_refresh >= refresh_interval:
- cluster_lock.refresh()
- last_refresh = current_time
+ )
+ # Explicit aclose() on early exit: ``async for … break`` does
+ # not close the generator, so GeneratorExit would never reach
+ # stream_chat_completion_sdk, leaving its stream lock held
+ # until GC eventually runs.
+ try:
+ async for chunk in published_stream:
+ if cancel.is_set():
+ log.info("Cancel requested, breaking stream")
+ break
+
+ # Capture StreamError so mark_session_completed receives
+ # the error message (stream_and_publish yields but does
+ # not publish StreamError — that's done by mark_session_completed).
+ if isinstance(chunk, StreamError):
+ error_msg = chunk.errorText
+ break
+
+ current_time = time.monotonic()
+ if current_time - last_refresh >= refresh_interval:
+ cluster_lock.refresh()
+ last_refresh = current_time
+ finally:
+ await published_stream.aclose()
# Stream loop completed
if cancel.is_set():
diff --git a/autogpt_platform/backend/backend/copilot/executor/processor_test.py b/autogpt_platform/backend/backend/copilot/executor/processor_test.py
index f565c5a2b3..5541648747 100644
--- a/autogpt_platform/backend/backend/copilot/executor/processor_test.py
+++ b/autogpt_platform/backend/backend/copilot/executor/processor_test.py
@@ -10,14 +10,18 @@ the real production helpers from ``processor.py`` so the routing logic
has meaningful coverage.
"""
-from unittest.mock import AsyncMock, patch
+import logging
+import threading
+from unittest.mock import AsyncMock, MagicMock, patch
import pytest
from backend.copilot.executor.processor import (
+ CoPilotProcessor,
resolve_effective_mode,
resolve_use_sdk_for_mode,
)
+from backend.copilot.executor.utils import CoPilotExecutionEntry, CoPilotLogMetadata
class TestResolveUseSdkForMode:
@@ -173,3 +177,101 @@ class TestResolveEffectiveMode:
) as flag_mock:
assert await resolve_effective_mode("fast", None) is None
flag_mock.assert_awaited_once()
+
+
+# ---------------------------------------------------------------------------
+# _execute_async aclose propagation
+# ---------------------------------------------------------------------------
+
+
+class _TrackedStream:
+ """Minimal async-generator stand-in that records whether ``aclose``
+ was called, so tests can verify the processor forces explicit cleanup
+ of the published stream on every exit path (normal + break on cancel)."""
+
+ def __init__(self, events: list):
+ self._events = events
+ self.aclose_called = False
+
+ def __aiter__(self):
+ return self
+
+ async def __anext__(self):
+ if not self._events:
+ raise StopAsyncIteration
+ return self._events.pop(0)
+
+ async def aclose(self) -> None:
+ self.aclose_called = True
+
+
+def _make_entry() -> CoPilotExecutionEntry:
+ return CoPilotExecutionEntry(
+ session_id="sess-1",
+ turn_id="turn-1",
+ user_id="user-1",
+ message="hi",
+ is_user_message=True,
+ request_arrival_at=0.0,
+ )
+
+
+def _make_log() -> CoPilotLogMetadata:
+ return CoPilotLogMetadata(logger=logging.getLogger("test-copilot"))
+
+
+class TestExecuteAsyncAclose:
+ """``_execute_async`` must call ``aclose`` on the published stream both
+ when the loop exits naturally and when ``cancel`` is set mid-stream —
+ otherwise ``stream_chat_completion_sdk`` stays suspended and keeps
+ holding the per-session Redis lock until GC."""
+
+ def _patches(self, published_stream: _TrackedStream):
+ """Shared mock context: patches every dependency ``_execute_async``
+ touches so the aclose path is the only behaviour under test."""
+ return [
+ patch(
+ "backend.copilot.executor.processor.ChatConfig",
+ return_value=MagicMock(test_mode=True, use_claude_agent_sdk=True),
+ ),
+ patch(
+ "backend.copilot.executor.processor.stream_chat_completion_dummy",
+ return_value=MagicMock(),
+ ),
+ patch(
+ "backend.copilot.executor.processor.stream_registry.stream_and_publish",
+ return_value=published_stream,
+ ),
+ patch(
+ "backend.copilot.executor.processor.stream_registry.mark_session_completed",
+ new=AsyncMock(),
+ ),
+ ]
+
+ @pytest.mark.asyncio
+ async def test_normal_exit_calls_aclose(self) -> None:
+ published = _TrackedStream(events=[MagicMock(), MagicMock()])
+ proc = CoPilotProcessor()
+ cancel = threading.Event()
+ cluster_lock = MagicMock()
+
+ patches = self._patches(published)
+ with patches[0], patches[1], patches[2], patches[3]:
+ await proc._execute_async(_make_entry(), cancel, cluster_lock, _make_log())
+
+ assert published.aclose_called is True
+
+ @pytest.mark.asyncio
+ async def test_cancel_break_calls_aclose(self) -> None:
+ events = [MagicMock()] # first chunk delivered, then cancel fires
+ published = _TrackedStream(events=events)
+ proc = CoPilotProcessor()
+ cancel = threading.Event()
+ cancel.set() # pre-set so the loop breaks on the first chunk
+ cluster_lock = MagicMock()
+
+ patches = self._patches(published)
+ with patches[0], patches[1], patches[2], patches[3]:
+ await proc._execute_async(_make_entry(), cancel, cluster_lock, _make_log())
+
+ assert published.aclose_called is True
diff --git a/autogpt_platform/backend/backend/copilot/sdk/response_adapter_test.py b/autogpt_platform/backend/backend/copilot/sdk/response_adapter_test.py
index c93286a3d6..634454f9e5 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/response_adapter_test.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/response_adapter_test.py
@@ -21,8 +21,6 @@ from backend.copilot.response_model import (
StreamFinishStep,
StreamHeartbeat,
StreamReasoningDelta,
- StreamReasoningEnd,
- StreamReasoningStart,
StreamStart,
StreamStartStep,
StreamTextDelta,
diff --git a/autogpt_platform/backend/backend/copilot/sdk/service.py b/autogpt_platform/backend/backend/copilot/sdk/service.py
index 8fea273b5d..ea0a135559 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/service.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/service.py
@@ -48,11 +48,12 @@ from ..config import ChatConfig, CopilotLlmModel, CopilotMode
from ..constants import (
COPILOT_ERROR_PREFIX,
COPILOT_RETRYABLE_ERROR_PREFIX,
- COPILOT_SYSTEM_PREFIX,
FRIENDLY_TRANSIENT_MSG,
+ STOPPED_BY_USER_MARKER,
STREAM_IDLE_TIMEOUT_SECONDS,
is_transient_api_error,
)
+from ..session_cleanup import prune_orphan_tool_calls
from ..context import encode_cwd_for_cli, get_workspace_manager
from ..graphiti.config import is_enabled_for_user
from ..model import (
@@ -70,7 +71,6 @@ from ..pending_message_helpers import (
persist_session_safe,
)
from ..pending_messages import (
- PendingMessage,
drain_pending_for_persist,
push_pending_message,
)
@@ -2504,10 +2504,7 @@ async def _run_stream_attempt(
for r in closing_responses:
yield r
ctx.session.messages.append(
- ChatMessage(
- role="assistant",
- content=f"{COPILOT_SYSTEM_PREFIX} Execution stopped by user",
- )
+ ChatMessage(role="assistant", content=STOPPED_BY_USER_MARKER)
)
if (
@@ -2737,7 +2734,7 @@ async def stream_chat_completion_sdk(
model: CopilotLlmModel | None = None,
request_arrival_at: float = 0.0,
**_kwargs: Any,
-) -> AsyncIterator[StreamBaseResponse]:
+) -> AsyncGenerator[StreamBaseResponse, None]:
"""Stream chat completion using Claude Agent SDK.
Args:
@@ -2781,6 +2778,10 @@ async def stream_chat_completion_sdk(
)
session.messages.pop()
+ # Drop orphan tool_use + trailing stop-marker rows left by a previous
+ # Stop mid-tool-call so the next turn's --resume transcript is well-formed.
+ prune_orphan_tool_calls(session.messages, log_prefix=f"[SDK] [{session_id[:12]}]")
+
# Strip any user-injected <user_context> tags on every turn.
# Only the server-injected prefix on the first message is trusted.
if message:
@@ -3191,10 +3192,7 @@ async def stream_chat_completion_sdk(
# Chronological combine: items typed BEFORE this request
# arrived go ahead of ``current_message``; items typed AFTER
# (race path, queued while /stream was still processing) go
- # after. ``pending_texts`` is kept around because downstream
- # code (the executor's update_message_content_by_sequence
- # call) needs the pre-combine list.
- pending_texts = pending_texts_from(pending_messages)
+ # after.
current_message = combine_pending_with_current(
pending_messages,
current_message,
diff --git a/autogpt_platform/backend/backend/copilot/sdk/session_persistence_test.py b/autogpt_platform/backend/backend/copilot/sdk/session_persistence_test.py
index ea7b128927..d7cbc1d24e 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/session_persistence_test.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/session_persistence_test.py
@@ -19,9 +19,11 @@ from __future__ import annotations
from datetime import datetime, timezone
from unittest.mock import MagicMock
+from backend.copilot.constants import STOPPED_BY_USER_MARKER
from backend.copilot.model import ChatMessage, ChatSession
from backend.copilot.response_model import StreamStartStep, StreamTextDelta
from backend.copilot.sdk.service import _dispatch_response, _StreamAccumulator
+from backend.copilot.session_cleanup import prune_orphan_tool_calls
_NOW = datetime(2024, 1, 1, tzinfo=timezone.utc)
@@ -215,3 +217,183 @@ class TestPreCreateAssistantMessage:
_simulate_pre_create(acc, ctx)
assert len(ctx.session.messages) == 0
+
+
+class TestPruneOrphanToolCalls:
+ """A Stop mid-tool-call leaves the session ending on an assistant row whose
+ ``tool_calls`` have no matching ``role="tool"`` row. Unless pruned before
+ the next turn, the ``--resume`` transcript would hand Claude CLI a
+ ``tool_use`` without a paired ``tool_result`` and the SDK would fail.
+ """
+
+ @staticmethod
+ def _tool_call(call_id: str, name: str = "bash_exec") -> dict:
+ return {
+ "id": call_id,
+ "type": "function",
+ "function": {"name": name, "arguments": "{}"},
+ }
+
+ def test_stop_mid_tool_leaves_orphan_assistant(self) -> None:
+ """Stop between StreamToolInputAvailable and StreamToolOutputAvailable:
+ the assistant row has ``tool_calls`` but no matching tool row."""
+ messages: list[ChatMessage] = [
+ ChatMessage(role="user", content="do something"),
+ ChatMessage(
+ role="assistant",
+ content="",
+ tool_calls=[self._tool_call("tc_abc")],
+ ),
+ ]
+
+ removed = prune_orphan_tool_calls(messages)
+
+ assert removed == 1
+ assert len(messages) == 1
+ assert messages[-1].role == "user"
+
+ def test_stop_strips_stopped_by_user_marker_and_orphan(self) -> None:
+ """The service also appends a ``STOPPED_BY_USER_MARKER`` after a
+ user stop when the stream loop exits cleanly; both tail rows must go."""
+ messages: list[ChatMessage] = [
+ ChatMessage(role="user", content="do something"),
+ ChatMessage(
+ role="assistant",
+ content="",
+ tool_calls=[self._tool_call("tc_abc")],
+ ),
+ ChatMessage(role="assistant", content=STOPPED_BY_USER_MARKER),
+ ]
+
+ removed = prune_orphan_tool_calls(messages)
+
+ assert removed == 2
+ assert len(messages) == 1
+ assert messages[-1].role == "user"
+
+ def test_completed_tool_call_is_preserved(self) -> None:
+ """An assistant row whose tool_calls are all resolved is a healthy
+ trailing state and must not be popped."""
+ messages: list[ChatMessage] = [
+ ChatMessage(role="user", content="do something"),
+ ChatMessage(
+ role="assistant",
+ content="",
+ tool_calls=[self._tool_call("tc_abc")],
+ ),
+ ChatMessage(
+ role="tool",
+ content="ok",
+ tool_call_id="tc_abc",
+ ),
+ ]
+
+ removed = prune_orphan_tool_calls(messages)
+
+ assert removed == 0
+ assert len(messages) == 3
+
+ def test_partial_resolution_still_pops(self) -> None:
+ """If an assistant emits multiple tool_calls and only some are
+ resolved, the assistant row is still unsafe for ``--resume``."""
+ messages: list[ChatMessage] = [
+ ChatMessage(role="user", content="do something"),
+ ChatMessage(
+ role="assistant",
+ content="",
+ tool_calls=[
+ self._tool_call("tc_1"),
+ self._tool_call("tc_2"),
+ ],
+ ),
+ ChatMessage(
+ role="tool",
+ content="ok",
+ tool_call_id="tc_1",
+ ),
+ ]
+
+ removed = prune_orphan_tool_calls(messages)
+
+ # Both the orphan assistant and its partial tool row are dropped.
+ assert removed == 2
+ assert len(messages) == 1
+ assert messages[-1].role == "user"
+
+ def test_plain_assistant_text_preserved(self) -> None:
+ """A regular text-only assistant tail is healthy and must be kept."""
+ messages: list[ChatMessage] = [
+ ChatMessage(role="user", content="hi"),
+ ChatMessage(role="assistant", content="hello"),
+ ]
+
+ removed = prune_orphan_tool_calls(messages)
+
+ assert removed == 0
+ assert len(messages) == 2
+
+ def test_empty_session_is_noop(self) -> None:
+ messages: list[ChatMessage] = []
+ assert prune_orphan_tool_calls(messages) == 0
+
+
+class TestPruneOrphanToolCallsLogging:
+ """``prune_orphan_tool_calls`` emits an INFO log when the caller passes
+ ``log_prefix`` and something was actually popped. Shared by the SDK
+ and baseline turn-start cleanup so both paths log in the same shape."""
+
+ def _tool_call(self, call_id: str) -> dict:
+ return {"id": call_id, "type": "function", "function": {"name": "bash"}}
+
+ def test_logs_when_something_was_pruned(self, caplog) -> None:
+ import backend.copilot.session_cleanup as sc
+
+ messages: list[ChatMessage] = [
+ ChatMessage(role="user", content="hi"),
+ ChatMessage(
+ role="assistant", content="", tool_calls=[self._tool_call("tc_1")]
+ ),
+ ]
+
+ sc.logger.propagate = True
+ caplog.set_level("INFO", logger=sc.logger.name)
+ removed = prune_orphan_tool_calls(messages, log_prefix="[TEST] [abc123]")
+
+ assert removed == 1
+ assert any(
+ "[TEST] [abc123]" in r.message and "Dropped 1" in r.message
+ for r in caplog.records
+ ), caplog.text
+
+ def test_no_log_when_nothing_to_prune(self, caplog) -> None:
+ import backend.copilot.session_cleanup as sc
+
+ messages: list[ChatMessage] = [
+ ChatMessage(role="user", content="hi"),
+ ChatMessage(role="assistant", content="hello"),
+ ]
+
+ sc.logger.propagate = True
+ caplog.set_level("INFO", logger=sc.logger.name)
+ removed = prune_orphan_tool_calls(messages, log_prefix="[TEST] [xyz]")
+
+ assert removed == 0
+ assert not any("[TEST] [xyz]" in r.message for r in caplog.records), caplog.text
+
+ def test_no_log_when_log_prefix_is_none(self, caplog) -> None:
+ """Without ``log_prefix``, ``prune_orphan_tool_calls`` is silent."""
+ import backend.copilot.session_cleanup as sc
+
+ messages: list[ChatMessage] = [
+ ChatMessage(role="user", content="hi"),
+ ChatMessage(
+ role="assistant", content="", tool_calls=[self._tool_call("tc_1")]
+ ),
+ ]
+
+ sc.logger.propagate = True
+ caplog.set_level("INFO", logger=sc.logger.name)
+ removed = prune_orphan_tool_calls(messages)
+
+ assert removed == 1
+ assert caplog.text == ""
diff --git a/autogpt_platform/backend/backend/copilot/session_cleanup.py b/autogpt_platform/backend/backend/copilot/session_cleanup.py
new file mode 100644
index 0000000000..b23056ca68
--- /dev/null
+++ b/autogpt_platform/backend/backend/copilot/session_cleanup.py
@@ -0,0 +1,77 @@
+"""Pre-turn cleanup of transient markers left on ``session.messages`` by
+prior turns (user-initiated Stop, cancelled tool calls, etc.).
+
+Shared by both the SDK and baseline chat entry points so both code paths
+start every new turn from a well-formed message list.
+"""
+
+import logging
+
+from backend.copilot.constants import STOPPED_BY_USER_MARKER
+from backend.copilot.model import ChatMessage
+
+logger = logging.getLogger(__name__)
+
+
+def prune_orphan_tool_calls(
+ messages: list[ChatMessage],
+ log_prefix: str | None = None,
+) -> int:
+ """Pop trailing orphan tool-use blocks from *messages* in place.
+
+ A Stop mid-tool-call leaves the session ending on an assistant message
+ whose ``tool_calls`` have no matching ``role="tool"`` row — the tool
+ never produced output because the executor was cancelled. Feeding that
+ tail to the next ``--resume`` turn would hand the Claude CLI a
+ ``tool_use`` with no paired ``tool_result`` and the SDK raises a
+ generic error.
+
+ Also strips trailing ``STOPPED_BY_USER_MARKER`` assistant rows emitted
+ by the same Stop path so the next turn's transcript starts clean.
+
+ If *log_prefix* is given, emits an INFO log with the prefix whenever
+ something was actually popped so the turn-start cleanup is visible.
+
+ In-memory only — the DB write path is append-only via
+ ``start_sequence`` so no delete is needed; the same rows are popped
+ again on the next session load.
+ """
+ cut_index: int | None = None
+ resolved_ids: set[str] = set()
+
+ for i in range(len(messages) - 1, -1, -1):
+ msg = messages[i]
+
+ if msg.role == "tool" and msg.tool_call_id:
+ resolved_ids.add(msg.tool_call_id)
+ continue
+
+ if msg.role == "assistant" and msg.content == STOPPED_BY_USER_MARKER:
+ cut_index = i
+ continue
+
+ if msg.role == "assistant" and msg.tool_calls:
+ pending_ids = {
+ tc.get("id")
+ for tc in msg.tool_calls
+ if isinstance(tc, dict) and tc.get("id")
+ }
+ if pending_ids and not pending_ids.issubset(resolved_ids):
+ cut_index = i
+ break
+
+ break
+
+ if cut_index is None:
+ return 0
+
+ removed = len(messages) - cut_index
+ del messages[cut_index:]
+ if log_prefix:
+ logger.info(
+ "%s Dropped %d trailing orphan tool-use/stop row(s) "
+ "before starting new turn",
+ log_prefix,
+ removed,
+ )
+ return removed
diff --git a/autogpt_platform/backend/backend/copilot/stream_registry.py b/autogpt_platform/backend/backend/copilot/stream_registry.py
index 111fbef90a..f4a26b7008 100644
--- a/autogpt_platform/backend/backend/copilot/stream_registry.py
+++ b/autogpt_platform/backend/backend/copilot/stream_registry.py
@@ -17,7 +17,7 @@ Subscribers:
import asyncio
import logging
import time
-from collections.abc import AsyncIterator
+from collections.abc import AsyncGenerator
from dataclasses import dataclass, field
from datetime import datetime, timezone
from typing import Any, Literal
@@ -329,8 +329,8 @@ async def publish_chunk(
async def stream_and_publish(
session_id: str,
turn_id: str,
- stream: AsyncIterator[StreamBaseResponse],
-) -> AsyncIterator[StreamBaseResponse]:
+ stream: AsyncGenerator[StreamBaseResponse, None],
+) -> AsyncGenerator[StreamBaseResponse, None]:
"""Wrap an async stream iterator with registry publishing.
Publishes each chunk to the stream registry for frontend SSE consumption,
@@ -353,27 +353,35 @@ async def stream_and_publish(
"""
publish_failed_once = False
- async for event in stream:
- if turn_id and not isinstance(event, (StreamFinish, StreamError)):
- try:
- await publish_chunk(turn_id, event, session_id=session_id)
- except (RedisError, ConnectionError, OSError):
- if not publish_failed_once:
- publish_failed_once = True
- logger.warning(
- "[stream_and_publish] Failed to publish chunk %s for %s "
- "(further failures logged at DEBUG)",
- type(event).__name__,
- session_id[:12],
- exc_info=True,
- )
- else:
- logger.debug(
- "[stream_and_publish] Failed to publish chunk %s",
- type(event).__name__,
- exc_info=True,
- )
- yield event
+ # async-for does not close an iterator on GeneratorExit; forward close
+ # to ``stream`` explicitly so its own cleanup (stream lock, persist)
+ # runs deterministically instead of waiting for GC.
+ try:
+ async for event in stream:
+ if turn_id and not isinstance(event, (StreamFinish, StreamError)):
+ try:
+ await publish_chunk(turn_id, event, session_id=session_id)
+ except (RedisError, ConnectionError, OSError):
+ # Full stack trace on the first failure; terser lines
+ # for the rest so subsequent failures don't flood logs
+ # while still being visible at WARNING.
+ if not publish_failed_once:
+ publish_failed_once = True
+ logger.warning(
+ "[stream_and_publish] Failed to publish chunk %s for %s",
+ type(event).__name__,
+ session_id[:12],
+ exc_info=True,
+ )
+ else:
+ logger.warning(
+ "[stream_and_publish] Failed to publish chunk %s for %s",
+ type(event).__name__,
+ session_id[:12],
+ )
+ yield event
+ finally:
+ await stream.aclose()
async def subscribe_to_session(
diff --git a/autogpt_platform/backend/backend/copilot/stream_registry_test.py b/autogpt_platform/backend/backend/copilot/stream_registry_test.py
index a09940a4a8..28ec199025 100644
--- a/autogpt_platform/backend/backend/copilot/stream_registry_test.py
+++ b/autogpt_platform/backend/backend/copilot/stream_registry_test.py
@@ -108,3 +108,116 @@ async def test_disconnect_all_listeners_timeout_not_counted():
await task
except asyncio.CancelledError:
pass
+
+
+# ---------------------------------------------------------------------------
+# stream_and_publish: closing the wrapper forwards GeneratorExit into the
+# inner stream so its finally (stream lock release, etc.) runs deterministically.
+# ---------------------------------------------------------------------------
+
+
+class _FakeEvent:
+ """Minimal stand-in for a StreamBaseResponse so publish_chunk is a no-op."""
+
+ def __init__(self, idx: int):
+ self.idx = idx
+
+
+async def test_stream_and_publish_aclose_propagates_to_inner_stream():
+ """Closing the wrapper MUST run the inner generator's finally block."""
+ inner_finally_ran = asyncio.Event()
+
+ async def _inner():
+ try:
+ yield _FakeEvent(0)
+ yield _FakeEvent(1)
+ yield _FakeEvent(2)
+ finally:
+ inner_finally_ran.set()
+
+ inner = _inner()
+ # Empty turn_id skips publish_chunk — keeps the test hermetic (no Redis).
+ wrapper = stream_registry.stream_and_publish(
+ session_id="sess-test", turn_id="", stream=inner
+ )
+
+ # Consume one event, then close the wrapper early.
+ first = await wrapper.__anext__()
+ assert isinstance(first, _FakeEvent)
+
+ await wrapper.aclose()
+
+ # The inner generator's finally must have run deterministically
+ # (not deferred to GC) so the caller's cleanup (lock release, etc.)
+ # is observable right after aclose returns.
+ assert inner_finally_ran.is_set()
+
+
+async def test_stream_and_publish_logs_warning_on_publish_chunk_failure():
+ """``stream_and_publish`` must not propagate a Redis publish failure —
+ it warns once with full stack trace, keeps yielding, and logs
+ subsequent failures at WARNING (terser, no exc_info) so repeated
+ errors stay visible without flooding the trace."""
+ from redis.exceptions import RedisError
+
+ async def _inner():
+ yield _FakeEvent(0)
+ yield _FakeEvent(1)
+ yield _FakeEvent(2)
+
+ async def _raising_publish(turn_id, event, session_id=None):
+ raise RedisError("boom")
+
+ warning_mock = patch.object(
+ stream_registry.logger, "warning", autospec=True
+ ).start()
+ try:
+ with patch.object(stream_registry, "publish_chunk", new=_raising_publish):
+ wrapper = stream_registry.stream_and_publish(
+ session_id="sess-test", turn_id="turn-1", stream=_inner()
+ )
+ received = [evt async for evt in wrapper]
+ finally:
+ patch.stopall()
+
+ # Every event still yields through — publish failures don't break the stream.
+ assert len(received) == 3
+ # One warning per failed publish (3 total). First call carries a
+ # stack trace (``exc_info=True``); subsequent calls are terser.
+ assert warning_mock.call_count == 3
+ assert warning_mock.call_args_list[0].kwargs.get("exc_info") is True
+ assert warning_mock.call_args_list[1].kwargs.get("exc_info") is not True
+
+
+async def test_stream_and_publish_consumer_break_then_aclose_releases_inner():
+ """The processor pattern — break on cancel, then aclose — must release."""
+ inner_finally_ran = asyncio.Event()
+
+ async def _inner():
+ try:
+ for idx in range(100):
+ yield _FakeEvent(idx)
+ finally:
+ inner_finally_ran.set()
+
+ inner = _inner()
+ wrapper = stream_registry.stream_and_publish(
+ session_id="sess-test", turn_id="", stream=inner
+ )
+
+ # Mimic the processor: consume a few events, simulate Stop by breaking,
+ # then aclose the wrapper (as processor._execute_async now does in the
+ # try/finally around the async for).
+ try:
+ count = 0
+ async for _ in wrapper:
+ count += 1
+ if count >= 2:
+ break
+ finally:
+ await wrapper.aclose()
+
+ assert inner_finally_ran.is_set()
diff --git a/autogpt_platform/backend/backend/copilot/tools/bash_exec.py b/autogpt_platform/backend/backend/copilot/tools/bash_exec.py
index ee87386cdb..1fbf4adc9c 100644
--- a/autogpt_platform/backend/backend/copilot/tools/bash_exec.py
+++ b/autogpt_platform/backend/backend/copilot/tools/bash_exec.py
@@ -47,7 +47,7 @@ class BashExecTool(BaseTool):
return (
"Execute a Bash command or script. Shares filesystem with SDK file tools. "
"Useful for scripts, data processing, and package installation. "
- "Killed after timeout (default 30s, max 120s)."
+ "Killed after `timeout` seconds."
)
@property
@@ -61,8 +61,8 @@ class BashExecTool(BaseTool):
},
"timeout": {
"type": "integer",
- "description": "Max seconds (default 30, max 120).",
- "default": 30,
+ "description": "Timeout in seconds; raise for long-running commands.",
+ "default": 120,
},
},
"required": ["command"],
@@ -80,7 +80,7 @@ class BashExecTool(BaseTool):
user_id: str | None,
session: ChatSession,
command: str = "",
- timeout: int = 30,
+ timeout: int = 120,
**kwargs: Any,
) -> ToolResponseBase:
"""Run a bash command on E2B (if available) or in a bubblewrap sandbox.
@@ -129,7 +129,7 @@ class BashExecTool(BaseTool):
message=(
"Execution timed out"
if timed_out
- else f"Command executed (exit {exit_code})"
+ else f"Command executed with status code {exit_code}"
),
stdout=stdout,
stderr=stderr,
@@ -183,7 +183,7 @@ class BashExecTool(BaseTool):
stdout = stdout.replace(secret, "[REDACTED]")
stderr = stderr.replace(secret, "[REDACTED]")
return BashExecResponse(
- message=f"Command executed on E2B (exit {result.exit_code})",
+ message=f"Command executed with status code {result.exit_code}",
stdout=stdout,
stderr=stderr,
exit_code=result.exit_code,
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/tools/GenericTool/GenericTool.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/tools/GenericTool/GenericTool.tsx
index c897da9bdb..995c18df05 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/tools/GenericTool/GenericTool.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/tools/GenericTool/GenericTool.tsx
@@ -236,9 +236,39 @@ function getBashAccordionData(
? `Command failed (exit ${exitCode})`
: "Command output";
+ // The command itself is already in the subtitle row above; surface the
+ // outcome here so scanning the closed accordion tells the reader "how it
+ // ended" at a glance. Prefer the backend's own first line of output
+ // (stderr for failures/timeouts — that's where bash_exec writes
+ // "Timed out after Xs" and where shells emit "command not found" etc.,
+ // stdout for success) over a terse "exit N" so the reader actually sees
+ // WHY the command ended.
+ const firstNonEmptyLine = (s: string | null): string | null => {
+ if (!s) return null;
+ const line = s.split("\n").find((l) => l.trim().length > 0);
+ return line ? truncate(line.trim(), 80) : null;
+ };
+ const stderrPreview = firstNonEmptyLine(stderr);
+ const stdoutPreview = firstNonEmptyLine(stdout);
+ let description: string | undefined;
+ if (timedOut) {
+ description = stderrPreview ?? "timed out";
+ } else if (exitCode !== null && exitCode !== 0) {
+ description = stderrPreview
+ ? `status code ${exitCode} · ${stderrPreview}`
+ : `status code ${exitCode}`;
+ } else if (exitCode === 0) {
+ description = stdoutPreview ?? "completed";
+ } else {
+ // Historical sessions persisted before exit_code/timed_out were added
+ // fall through here — fall back to the command preview so the closed
+ // accordion still tells the reader what ran.
+ description = truncate(command, 80);
+ }
+
return {
title,
- description: truncate(command, 80),
+ description,
content: (
<div className="space-y-2">
{command && (
@@ -703,7 +733,6 @@ export function GenericTool({ part }: Props) {
return (
<div className="py-2">
- {/* Status line: always visible so the user sees what tool ran */}
<div className="flex items-center gap-2 text-sm text-muted-foreground">
<ToolIcon
category={category}
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/tools/GenericTool/__tests__/GenericTool.test.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/tools/GenericTool/__tests__/GenericTool.test.tsx
new file mode 100644
index 0000000000..4308eb49bf
--- /dev/null
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/tools/GenericTool/__tests__/GenericTool.test.tsx
@@ -0,0 +1,139 @@
+import { describe, expect, it } from "vitest";
+import type { ToolUIPart } from "ai";
+import { render, screen } from "@/tests/integrations/test-utils";
+import { GenericTool } from "../GenericTool";
+
+function makePart(overrides: Record<string, unknown> = {}): ToolUIPart {
+ return {
+ type: "tool-bash_exec",
+ toolCallId: "call-1",
+ state: "input-streaming",
+ input: { command: 'echo "hi"' },
+ ...overrides,
+ } as ToolUIPart;
+}
+
+describe("GenericTool", () => {
+ it("shows a subtitle and no accordion while the tool is streaming", () => {
+ const { container } = render(
+ <GenericTool part={makePart({ state: "input-streaming" })} />,
+ );
+ expect(screen.queryByRole("button")).toBeNull();
+ expect(container.textContent).toContain("Running");
+ });
+
+ it("renders exactly one row once output is available (accordion only, no loose status line)", () => {
+ render(
+ <GenericTool
+ part={makePart({
+ state: "output-available",
+ input: { command: 'echo "starting simulation run 2"' },
+ output: { exit_code: 1, stdout: "", stderr: "boom" },
+ })}
+ />,
+ );
+ // The accordion trigger is the only interactive element; no separate
+ // MorphingTextAnimation status row is rendered alongside it.
+ const triggers = screen.getAllByRole("button");
+ expect(triggers.length).toBe(1);
+ expect(triggers[0].textContent).toContain("Command failed (exit 1)");
+ });
+
+ it("shows 'status code N · <first line of stderr>' on non-zero exit", () => {
+ render(
+ <GenericTool
+ part={makePart({
+ state: "output-available",
+ input: { command: "missing-bin" },
+ output: {
+ exit_code: 127,
+ stdout: "",
+ stderr: "bash: missing-bin: command not found\n",
+ },
+ })}
+ />,
+ );
+ const trigger = screen.getByRole("button", { expanded: false });
+ expect(trigger.textContent).toContain("Command failed (exit 127)");
+ expect(trigger.textContent).toContain(
+ "status code 127 · bash: missing-bin: command not found",
+ );
+ });
+
+ it("falls back to bare 'status code N' when stderr is empty", () => {
+ render(
+ <GenericTool
+ part={makePart({
+ state: "output-available",
+ output: { exit_code: 2, stdout: "", stderr: "" },
+ })}
+ />,
+ );
+ const trigger = screen.getByRole("button", { expanded: false });
+ expect(trigger.textContent).toContain("status code 2");
+ expect(trigger.textContent).not.toContain("·");
+ });
+
+ it("shows the stderr first line for a timed-out command", () => {
+ render(
+ <GenericTool
+ part={makePart({
+ state: "output-available",
+ input: { command: "sleep 120" },
+ output: {
+ exit_code: -1,
+ timed_out: true,
+ stderr: "Timed out after 120s",
+ },
+ })}
+ />,
+ );
+ const trigger = screen.getByRole("button", { expanded: false });
+ expect(trigger.textContent).toContain("Command timed out");
+ expect(trigger.textContent).toContain("Timed out after 120s");
+ expect(trigger.textContent).not.toContain("sleep 120");
+ });
+
+ it("falls back to the command preview for legacy outputs missing exit_code/timed_out", () => {
+ render(
+ <GenericTool
+ part={makePart({
+ state: "output-available",
+ input: { command: "echo hello" },
+ output: { stdout: "hello\n" },
+ })}
+ />,
+ );
+ const trigger = screen.getByRole("button", { expanded: false });
+ expect(trigger.textContent).toContain("echo hello");
+ });
+
+ it("prefers stdout first line on exit 0, falls back to 'completed'", () => {
+ const { rerender } = render(
+ <GenericTool
+ part={makePart({
+ state: "output-available",
+ output: {
+ exit_code: 0,
+ stdout: "Hello, world!\nmore lines below\n",
+ stderr: "",
+ },
+ })}
+ />,
+ );
+ const trigger1 = screen.getByRole("button", { expanded: false });
+ expect(trigger1.textContent).toContain("Hello, world!");
+ expect(trigger1.textContent).not.toContain("more lines below");
+
+ rerender(
+ <GenericTool
+ part={makePart({
+ state: "output-available",
+ output: { exit_code: 0, stdout: "", stderr: "" },
+ })}
+ />,
+ );
+ const trigger2 = screen.getByRole("button", { expanded: false });
+ expect(trigger2.textContent).toContain("completed");
+ });
+});
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/tools/GenericTool/__tests__/helpers.test.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/tools/GenericTool/__tests__/helpers.test.ts
index cc8bcc8afb..de0b9155b6 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/tools/GenericTool/__tests__/helpers.test.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/tools/GenericTool/__tests__/helpers.test.ts
@@ -202,14 +202,14 @@ describe("getAnimationText", () => {
expect(getAnimationText(part, "bash")).toBe("Ran: echo hello");
});
- it("shows exit code on non-zero exit", () => {
+ it("still shows the command even on non-zero exit (exit code lives in the accordion description)", () => {
const part = makePart({
type: "tool-bash_exec",
state: "output-available",
input: { command: "false" },
output: { exit_code: 1 },
});
- expect(getAnimationText(part, "bash")).toBe("Command exited with code 1");
+ expect(getAnimationText(part, "bash")).toBe("Ran: false");
});
it("shows error text for bash failure", () => {
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/tools/GenericTool/helpers.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/tools/GenericTool/helpers.ts
index f0a1cd6853..f8da6fbc2f 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/tools/GenericTool/helpers.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/tools/GenericTool/helpers.ts
@@ -199,17 +199,6 @@ export function humanizeFileName(filePath: string): string {
return `"${words.join(" ")}"`;
}
-/* ------------------------------------------------------------------ */
-/* Exit code helper */
-/* ------------------------------------------------------------------ */
-
-function getExitCode(output: unknown): number | null {
- if (!output || typeof output !== "object") return null;
- const parsed = output as Record<string, unknown>;
- if (typeof parsed.exit_code === "number") return parsed.exit_code;
- return null;
-}
-
/* ------------------------------------------------------------------ */
/* Animation text */
/* ------------------------------------------------------------------ */
@@ -287,13 +276,11 @@ export function getAnimationText(
}
case "output-available": {
switch (category) {
- case "bash": {
- const exitCode = getExitCode(part.output);
- if (exitCode !== null && exitCode !== 0) {
- return `Command exited with code ${exitCode}`;
- }
+ case "bash":
+ // Subtitle always shows WHAT ran. The accordion title + description
+ // carry HOW it ended (exit code / "timed out"), so repeating the
+ // exit status here would just double up.
return shortSummary ? `Ran: ${shortSummary}` : "Command completed";
- }
case "web":
if (toolName === "WebSearch") {
return shortSummary