AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
feat(backend): Add AI-generated activity status for agent executions (#10487)
Zamil Majdye63254917589
diff --git a/autogpt_platform/autogpt_libs/autogpt_libs/feature_flag/client.py b/autogpt_platform/autogpt_libs/autogpt_libs/feature_flag/client.pyindex 9aed891706..1bd7584942 100644--- a/autogpt_platform/autogpt_libs/autogpt_libs/feature_flag/client.py+++ b/autogpt_platform/autogpt_libs/autogpt_libs/feature_flag/client.py@@ -17,9 +17,13 @@ logger = logging.getLogger(__name__)P = ParamSpec("P")T = TypeVar("T")+_is_initialized = False+def get_client() -> LDClient:"""Get the LaunchDarkly client singleton."""+ if not _is_initialized:+ initialize_launchdarkly()return ldclient.get()@@ -37,6 +41,8 @@ def initialize_launchdarkly() -> None:ldclient.set_config(config)if ldclient.get().is_initialized():+ global _is_initialized+ _is_initialized = Truelogger.info("LaunchDarkly client initialized successfully")else:logger.error("LaunchDarkly client failed to initialize")@@ -60,6 +66,30 @@ def create_context(return builder.build()+def is_feature_enabled(flag_key: str, user_id: str, default: bool = False) -> bool:+ """+ Simple helper to check if a feature flag is enabled for a user.++ Args:+ flag_key: The LaunchDarkly feature flag key+ user_id: The user ID to evaluate the flag for+ default: Default value if LaunchDarkly is unavailable or flag evaluation fails++ Returns:+ True if feature is enabled, False otherwise+ """+ try:+ client = get_client()+ context = create_context(str(user_id))+ return client.variation(flag_key, context, default)++ except Exception as e:+ logger.debug(+ f"LaunchDarkly flag evaluation failed for {flag_key}: {e}, using default={default}"+ )+ return default++def feature_flag(flag_key: str,default: bool = False,diff --git a/autogpt_platform/autogpt_libs/autogpt_libs/feature_flag/client_test.py b/autogpt_platform/autogpt_libs/autogpt_libs/feature_flag/client_test.pyindex 8fccfb28b5..ac0a3bc6ef 100644--- a/autogpt_platform/autogpt_libs/autogpt_libs/feature_flag/client_test.py+++ b/autogpt_platform/autogpt_libs/autogpt_libs/feature_flag/client_test.py@@ -1,7 +1,11 @@import pytestfrom ldclient import LDClient-from autogpt_libs.feature_flag.client import feature_flag, mock_flag_variation+from autogpt_libs.feature_flag.client import (+ feature_flag,+ is_feature_enabled,+ mock_flag_variation,+)@pytest.fixture@@ -43,3 +47,38 @@ def test_mock_flag_variation(ld_client):with mock_flag_variation("test-flag", False):assert ld_client.variation("test-flag", None, False)+++def test_is_feature_enabled(ld_client):+ """Test the is_feature_enabled helper function."""+ ld_client.is_initialized.return_value = True+ ld_client.variation.return_value = True++ result = is_feature_enabled("test-flag", "user123", default=False)+ assert result is True++ ld_client.variation.assert_called_once()+ call_args = ld_client.variation.call_args+ assert call_args[0][0] == "test-flag" # flag_key+ assert call_args[0][2] is False # default value+++def test_is_feature_enabled_not_initialized(ld_client):+ """Test is_feature_enabled when LaunchDarkly is not initialized."""+ ld_client.is_initialized.return_value = False++ result = is_feature_enabled("test-flag", "user123", default=True)+ assert result is True # Should return default++ ld_client.variation.assert_not_called()+++def test_is_feature_enabled_exception(mocker):+ """Test is_feature_enabled when get_client() raises an exception."""+ mocker.patch(+ "autogpt_libs.feature_flag.client.get_client",+ side_effect=Exception("Client error"),+ )++ result = is_feature_enabled("test-flag", "user123", default=True)+ assert result is True # Should return defaultdiff --git a/autogpt_platform/backend/backend/data/execution.py b/autogpt_platform/backend/backend/data/execution.pyindex ded76cc02c..f3e82b02ed 100644--- a/autogpt_platform/backend/backend/data/execution.py+++ b/autogpt_platform/backend/backend/data/execution.py@@ -134,6 +134,10 @@ class GraphExecutionMeta(BaseDbModel):default=None,description="Error message if any",)+ activity_status: str | None = Field(+ default=None,+ description="AI-generated summary of what the agent did",+ )def to_db(self) -> GraphExecutionStats:return GraphExecutionStats(@@ -145,6 +149,7 @@ class GraphExecutionMeta(BaseDbModel):node_count=self.node_exec_count,node_error_count=self.node_error_count,error=self.error,+ activity_status=self.activity_status,)stats: Stats | None@@ -189,6 +194,7 @@ class GraphExecutionMeta(BaseDbModel):if isinstance(stats.error, Exception)else stats.error),+ activity_status=stats.activity_status,)if statselse Nonediff --git a/autogpt_platform/backend/backend/data/model.py b/autogpt_platform/backend/backend/data/model.pyindex c750b80482..57c3ec4eb8 100644--- a/autogpt_platform/backend/backend/data/model.py+++ b/autogpt_platform/backend/backend/data/model.py@@ -706,3 +706,6 @@ class GraphExecutionStats(BaseModel):default=0, description="Total number of errors generated")cost: int = Field(default=0, description="Total execution cost (cents)")+ activity_status: Optional[str] = Field(+ default=None, description="AI-generated summary of what the agent did"+ )diff --git a/autogpt_platform/backend/backend/executor/activity_status_generator.py b/autogpt_platform/backend/backend/executor/activity_status_generator.pynew file mode 100644index 0000000000..1ac46aa420--- /dev/null+++ b/autogpt_platform/backend/backend/executor/activity_status_generator.py@@ -0,0 +1,435 @@+"""+Module for generating AI-based activity status for graph executions.+"""++import json+import logging+from typing import TYPE_CHECKING, Any, NotRequired, TypedDict++from autogpt_libs.feature_flag.client import is_feature_enabled+from pydantic import SecretStr++from backend.blocks.llm import LlmModel, llm_call+from backend.data.block import get_block+from backend.data.execution import ExecutionStatus, NodeExecutionResult+from backend.data.model import APIKeyCredentials, GraphExecutionStats+from backend.util.settings import Settings+from backend.util.truncate import truncate++# LaunchDarkly feature flag key for AI activity status generation+AI_ACTIVITY_STATUS_FLAG_KEY = "ai-agent-execution-summary"++if TYPE_CHECKING:+ from backend.executor import DatabaseManagerAsyncClient++logger = logging.getLogger(__name__)+++class ErrorInfo(TypedDict):+ """Type definition for error information."""++ error: str+ execution_id: str+ timestamp: str+++class InputOutputInfo(TypedDict):+ """Type definition for input/output information."""++ execution_id: str+ output_data: dict[str, Any] # Used for both input and output data+ timestamp: str+++class NodeInfo(TypedDict):+ """Type definition for node information."""++ node_id: str+ block_id: str+ block_name: str+ block_description: str+ execution_count: int+ error_count: int+ recent_errors: list[ErrorInfo]+ recent_outputs: list[InputOutputInfo]+ recent_inputs: list[InputOutputInfo]+++class NodeRelation(TypedDict):+ """Type definition for node relation information."""++ source_node_id: str+ sink_node_id: str+ source_name: str+ sink_name: str+ is_static: bool+ source_block_name: NotRequired[str] # Optional, only set if block exists+ sink_block_name: NotRequired[str] # Optional, only set if block exists+++def _truncate_uuid(uuid_str: str) -> str:+ """Truncate UUID to first segment to reduce payload size."""+ if not uuid_str:+ return uuid_str+ return uuid_str.split("-")[0] if "-" in uuid_str else uuid_str[:8]+++async def generate_activity_status_for_execution(+ graph_exec_id: str,+ graph_id: str,+ graph_version: int,+ execution_stats: GraphExecutionStats,+ db_client: "DatabaseManagerAsyncClient",+ user_id: str,+ execution_status: ExecutionStatus | None = None,+) -> str | None:+ """+ Generate an AI-based activity status summary for a graph execution.++ This function handles all the data collection and AI generation logic,+ keeping the manager integration simple.++ Args:+ graph_exec_id: The graph execution ID+ graph_id: The graph ID+ graph_version: The graph version+ execution_stats: Execution statistics+ db_client: Database client for fetching data+ user_id: User ID for LaunchDarkly feature flag evaluation+ execution_status: The overall execution status (COMPLETED, FAILED, TERMINATED)++ Returns:+ AI-generated activity status string, or None if feature is disabled+ """+ # Check LaunchDarkly feature flag for AI activity status generation+ if not is_feature_enabled(AI_ACTIVITY_STATUS_FLAG_KEY, user_id, default=False):+ logger.debug("AI activity status generation is disabled via LaunchDarkly")+ return None++ # Check if we have OpenAI API key+ try:+ settings = Settings()+ if not settings.secrets.openai_api_key:+ logger.debug(+ "OpenAI API key not configured, skipping activity status generation"+ )+ return None++ # Get all node executions for this graph execution+ node_executions = await db_client.get_node_executions(+ graph_exec_id, include_exec_data=True+ )++ # Get graph metadata and full graph structure for name, description, and links+ graph_metadata = await db_client.get_graph_metadata(graph_id, graph_version)+ graph = await db_client.get_graph(graph_id, graph_version)++ graph_name = graph_metadata.name if graph_metadata else f"Graph {graph_id}"+ graph_description = graph_metadata.description if graph_metadata else ""+ graph_links = graph.links if graph else []++ # Build execution data summary+ execution_data = _build_execution_summary(+ node_executions,+ execution_stats,+ graph_name,+ graph_description,+ graph_links,+ execution_status,+ )++ # Prepare prompt for AI+ prompt = [+ {+ "role": "system",+ "content": (+ "You are an AI assistant summarizing what you just did for a user in simple, friendly language. "+ "Write from the user's perspective about what they accomplished, NOT about technical execution details. "+ "Focus on the ACTUAL TASK the user wanted done, not the internal workflow steps. "+ "Avoid technical terms like 'workflow', 'execution', 'components', 'nodes', 'processing', etc. "+ "Keep it to 3 sentences maximum. Be conversational and human-friendly.\n\n"+ "IMPORTANT: Be HONEST about what actually happened:\n"+ "- If the input was invalid/nonsensical, say so directly\n"+ "- If the task failed, explain what went wrong in simple terms\n"+ "- If errors occurred, focus on what the user needs to know\n"+ "- Only claim success if the task was genuinely completed\n"+ "- Don't sugar-coat failures or present them as helpful feedback\n\n"+ "Understanding Errors:\n"+ "- Node errors: Individual steps may fail but the overall task might still complete (e.g., one data source fails but others work)\n"+ "- Graph error (in overall_status.graph_error): This means the entire execution failed and nothing was accomplished\n"+ "- Even if execution shows 'completed', check if critical nodes failed that would prevent the desired outcome\n"+ "- Focus on the end result the user wanted, not whether technical steps completed"+ ),+ },+ {+ "role": "user",+ "content": (+ f"A user ran '{graph_name}' to accomplish something. Based on this execution data, "+ f"write what they achieved in simple, user-friendly terms:\n\n"+ f"{json.dumps(execution_data, indent=2)}\n\n"+ "CRITICAL: Check overall_status.graph_error FIRST - if present, the entire execution failed.\n"+ "Then check individual node errors to understand partial failures.\n\n"+ "Write 1-3 sentences about what the user accomplished, such as:\n"+ "- 'I analyzed your resume and provided detailed feedback for the IT industry.'\n"+ "- 'I couldn't analyze your resume because the input was just nonsensical text.'\n"+ "- 'I failed to complete the task due to missing API access.'\n"+ "- 'I extracted key information from your documents and organized it into a summary.'\n"+ "- 'The task failed to run due to system configuration issues.'\n\n"+ "Focus on what ACTUALLY happened, not what was attempted."+ ),+ },+ ]++ # Log the prompt for debugging purposes+ logger.debug(+ f"Sending prompt to LLM for graph execution {graph_exec_id}: {json.dumps(prompt, indent=2)}"+ )++ # Create credentials for LLM call+ credentials = APIKeyCredentials(+ id="openai",+ provider="openai",+ api_key=SecretStr(settings.secrets.openai_api_key),+ title="System OpenAI",+ )++ # Make LLM call using current event loop+ activity_status = await _call_llm_direct(credentials, prompt)++ logger.debug(+ f"Generated activity status for {graph_exec_id}: {activity_status}"+ )+ return activity_status++ except Exception as e:+ logger.error(+ f"Failed to generate activity status for execution {graph_exec_id}: {str(e)}"+ )+ return None+++def _build_execution_summary(+ node_executions: list[NodeExecutionResult],+ execution_stats: GraphExecutionStats,+ graph_name: str,+ graph_description: str,+ graph_links: list[Any],+ execution_status: ExecutionStatus | None = None,+) -> dict[str, Any]:+ """Build a structured summary of execution data for AI analysis."""++ nodes: list[NodeInfo] = []+ node_execution_counts: dict[str, int] = {}+ node_error_counts: dict[str, int] = {}+ node_errors: dict[str, list[ErrorInfo]] = {}+ node_outputs: dict[str, list[InputOutputInfo]] = {}+ node_inputs: dict[str, list[InputOutputInfo]] = {}+ input_output_data: dict[str, Any] = {}+ node_map: dict[str, NodeInfo] = {}++ # Process node executions+ for node_exec in node_executions:+ block = get_block(node_exec.block_id)+ if not block:+ logger.warning(+ f"Block {node_exec.block_id} not found for node {node_exec.node_id}"+ )+ continue++ # Track execution counts per node+ if node_exec.node_id not in node_execution_counts:+ node_execution_counts[node_exec.node_id] = 0+ node_execution_counts[node_exec.node_id] += 1++ # Track errors per node and group them+ if node_exec.status == ExecutionStatus.FAILED:+ if node_exec.node_id not in node_error_counts:+ node_error_counts[node_exec.node_id] = 0+ node_error_counts[node_exec.node_id] += 1++ # Extract actual error message from output_data+ error_message = "Unknown error"+ if node_exec.output_data and isinstance(node_exec.output_data, dict):+ # Check if error is in output_data+ if "error" in node_exec.output_data:+ error_output = node_exec.output_data["error"]+ if isinstance(error_output, list) and error_output:+ error_message = str(error_output[0])+ else:+ error_message = str(error_output)++ # Group errors by node_id+ if node_exec.node_id not in node_errors:+ node_errors[node_exec.node_id] = []++ node_errors[node_exec.node_id].append(+ {+ "error": error_message,+ "execution_id": _truncate_uuid(node_exec.node_exec_id),+ "timestamp": node_exec.add_time.isoformat(),+ }+ )++ # Collect output samples for each node (latest executions)+ if node_exec.output_data:+ if node_exec.node_id not in node_outputs:+ node_outputs[node_exec.node_id] = []++ # Truncate output data to 100 chars to save space+ truncated_output = truncate(node_exec.output_data, 100)++ node_outputs[node_exec.node_id].append(+ {+ "execution_id": _truncate_uuid(node_exec.node_exec_id),+ "output_data": truncated_output,+ "timestamp": node_exec.add_time.isoformat(),+ }+ )++ # Collect input samples for each node (latest executions)+ if node_exec.input_data:+ if node_exec.node_id not in node_inputs:+ node_inputs[node_exec.node_id] = []++ # Truncate input data to 100 chars to save space+ truncated_input = truncate(node_exec.input_data, 100)++ node_inputs[node_exec.node_id].append(+ {+ "execution_id": _truncate_uuid(node_exec.node_exec_id),+ "output_data": truncated_input, # Reuse field name for consistency+ "timestamp": node_exec.add_time.isoformat(),+ }+ )++ # Build node data (only add unique nodes)+ if node_exec.node_id not in node_map:+ node_data: NodeInfo = {+ "node_id": _truncate_uuid(node_exec.node_id),+ "block_id": _truncate_uuid(node_exec.block_id),+ "block_name": block.name,+ "block_description": block.description or "",+ "execution_count": 0, # Will be set later+ "error_count": 0, # Will be set later+ "recent_errors": [], # Will be set later+ "recent_outputs": [], # Will be set later+ "recent_inputs": [], # Will be set later+ }+ nodes.append(node_data)+ node_map[node_exec.node_id] = node_data++ # Store input/output data for special blocks (input/output blocks)+ if block.name in ["AgentInputBlock", "AgentOutputBlock", "UserInputBlock"]:+ if node_exec.input_data:+ input_output_data[f"{node_exec.node_id}_inputs"] = dict(+ node_exec.input_data+ )+ if node_exec.output_data:+ input_output_data[f"{node_exec.node_id}_outputs"] = dict(+ node_exec.output_data+ )++ # Add execution and error counts to node data, plus limited errors and output samples+ for node in nodes:+ # Use original node_id for lookups (before truncation)+ original_node_id = None+ for orig_id, node_data in node_map.items():+ if node_data == node:+ original_node_id = orig_id+ break++ if original_node_id:+ node["execution_count"] = node_execution_counts.get(original_node_id, 0)+ node["error_count"] = node_error_counts.get(original_node_id, 0)++ # Add limited errors for this node (latest 10 or first 5 + last 5)+ if original_node_id in node_errors:+ node_error_list = node_errors[original_node_id]+ if len(node_error_list) <= 10:+ node["recent_errors"] = node_error_list+ else:+ # First 5 + last 5 if more than 10 errors+ node["recent_errors"] = node_error_list[:5] + node_error_list[-5:]++ # Add latest output samples (latest 3)+ if original_node_id in node_outputs:+ node_output_list = node_outputs[original_node_id]+ # Sort by timestamp if available, otherwise take last 3+ if node_output_list and node_output_list[0].get("timestamp"):+ node_output_list.sort(+ key=lambda x: x.get("timestamp", ""), reverse=True+ )+ node["recent_outputs"] = node_output_list[:3]++ # Add latest input samples (latest 3)+ if original_node_id in node_inputs:+ node_input_list = node_inputs[original_node_id]+ # Sort by timestamp if available, otherwise take last 3+ if node_input_list and node_input_list[0].get("timestamp"):+ node_input_list.sort(+ key=lambda x: x.get("timestamp", ""), reverse=True+ )+ node["recent_inputs"] = node_input_list[:3]++ # Build node relations from graph links+ node_relations: list[NodeRelation] = []+ for link in graph_links:+ # Include link details with source and sink information (truncated UUIDs)+ relation: NodeRelation = {+ "source_node_id": _truncate_uuid(link.source_id),+ "sink_node_id": _truncate_uuid(link.sink_id),+ "source_name": link.source_name,+ "sink_name": link.sink_name,+ "is_static": link.is_static if hasattr(link, "is_static") else False,+ }++ # Add block names if nodes exist in our map+ if link.source_id in node_map:+ relation["source_block_name"] = node_map[link.source_id]["block_name"]+ if link.sink_id in node_map:+ relation["sink_block_name"] = node_map[link.sink_id]["block_name"]++ node_relations.append(relation)++ # Build overall summary+ return {+ "graph_info": {"name": graph_name, "description": graph_description},+ "nodes": nodes,+ "node_relations": node_relations,+ "input_output_data": input_output_data,+ "overall_status": {+ "total_nodes_in_graph": len(nodes),+ "total_executions": execution_stats.node_count,+ "total_errors": execution_stats.node_error_count,+ "execution_time_seconds": execution_stats.walltime,+ "has_errors": bool(+ execution_stats.error or execution_stats.node_error_count > 0+ ),+ "graph_error": (+ str(execution_stats.error) if execution_stats.error else None+ ),+ "graph_execution_status": (+ execution_status.value if execution_status else None+ ),+ },+ }+++async def _call_llm_direct(+ credentials: APIKeyCredentials, prompt: list[dict[str, str]]+) -> str:+ """Make direct LLM call."""++ response = await llm_call(+ credentials=credentials,+ llm_model=LlmModel.GPT4O_MINI,+ prompt=prompt,+ json_format=False,+ max_tokens=150,+ compress_prompt_to_fit=True,+ )++ if response and response.response:+ return response.response.strip()+ else:+ return "Unable to generate activity summary"diff --git a/autogpt_platform/backend/backend/executor/activity_status_generator_test.py b/autogpt_platform/backend/backend/executor/activity_status_generator_test.pynew file mode 100644index 0000000000..08ff3a7c2a--- /dev/null+++ b/autogpt_platform/backend/backend/executor/activity_status_generator_test.py@@ -0,0 +1,702 @@+"""+Tests for activity status generator functionality.+"""++from datetime import datetime, timezone+from unittest.mock import AsyncMock, MagicMock, patch++import pytest++from backend.blocks.llm import LLMResponse+from backend.data.execution import ExecutionStatus, NodeExecutionResult+from backend.data.model import GraphExecutionStats+from backend.executor.activity_status_generator import (+ _build_execution_summary,+ _call_llm_direct,+ generate_activity_status_for_execution,+)+++def mock_node_executions():+ """Create mock node executions for testing."""+ return [+ NodeExecutionResult(+ user_id="test_user",+ graph_id="test_graph",+ graph_version=1,+ graph_exec_id="test_exec",+ node_exec_id="123e4567-e89b-12d3-a456-426614174001",+ node_id="456e7890-e89b-12d3-a456-426614174002",+ block_id="789e1234-e89b-12d3-a456-426614174003",+ status=ExecutionStatus.COMPLETED,+ input_data={"user_input": "Hello, world!"},+ output_data={"processed_input": ["Hello, world!"]},+ add_time=datetime.now(timezone.utc),+ queue_time=None,+ start_time=None,+ end_time=None,+ ),+ NodeExecutionResult(+ user_id="test_user",+ graph_id="test_graph",+ graph_version=1,+ graph_exec_id="test_exec",+ node_exec_id="234e5678-e89b-12d3-a456-426614174004",+ node_id="567e8901-e89b-12d3-a456-426614174005",+ block_id="890e2345-e89b-12d3-a456-426614174006",+ status=ExecutionStatus.COMPLETED,+ input_data={"data": "Hello, world!"},+ output_data={"result": ["Processed data"]},+ add_time=datetime.now(timezone.utc),+ queue_time=None,+ start_time=None,+ end_time=None,+ ),+ NodeExecutionResult(+ user_id="test_user",+ graph_id="test_graph",+ graph_version=1,+ graph_exec_id="test_exec",+ node_exec_id="345e6789-e89b-12d3-a456-426614174007",+ node_id="678e9012-e89b-12d3-a456-426614174008",+ block_id="901e3456-e89b-12d3-a456-426614174009",+ status=ExecutionStatus.FAILED,+ input_data={"final_data": "Processed data"},+ output_data={+ "error": ["Connection timeout: Unable to reach external service"]+ },+ add_time=datetime.now(timezone.utc),+ queue_time=None,+ start_time=None,+ end_time=None,+ ),+ ]+++def mock_execution_stats():+ """Create mock execution stats for testing."""+ return GraphExecutionStats(+ walltime=2.5,+ cputime=1.8,+ nodes_walltime=2.0,+ nodes_cputime=1.5,+ node_count=3,+ node_error_count=1,+ cost=10,+ error=None,+ )+++def mock_execution_stats_with_graph_error():+ """Create mock execution stats with graph-level error."""+ return GraphExecutionStats(+ walltime=2.5,+ cputime=1.8,+ nodes_walltime=2.0,+ nodes_cputime=1.5,+ node_count=3,+ node_error_count=1,+ cost=10,+ error="Graph execution failed: Invalid API credentials",+ )+++def mock_blocks():+ """Create mock blocks for testing."""+ input_block = MagicMock()+ input_block.name = "AgentInputBlock"+ input_block.description = "Handles user input"++ process_block = MagicMock()+ process_block.name = "ProcessingBlock"+ process_block.description = "Processes data"++ output_block = MagicMock()+ output_block.name = "AgentOutputBlock"+ output_block.description = "Provides output to user"++ return {+ "789e1234-e89b-12d3-a456-426614174003": input_block,+ "890e2345-e89b-12d3-a456-426614174006": process_block,+ "901e3456-e89b-12d3-a456-426614174009": output_block,+ "process_block_id": process_block, # Keep old key for different error format test+ }+++class TestBuildExecutionSummary:+ """Tests for _build_execution_summary function."""++ def test_build_summary_with_successful_execution(+ self, mock_node_executions, mock_execution_stats, mock_blocks+ ):+ """Test building summary for successful execution."""+ # Create mock links with realistic UUIDs+ mock_links = [+ MagicMock(+ source_id="456e7890-e89b-12d3-a456-426614174002",+ sink_id="567e8901-e89b-12d3-a456-426614174005",+ source_name="output",+ sink_name="input",+ is_static=False,+ )+ ]++ with patch(+ "backend.executor.activity_status_generator.get_block"+ ) as mock_get_block:+ mock_get_block.side_effect = lambda block_id: mock_blocks.get(block_id)++ summary = _build_execution_summary(+ mock_node_executions[:2],+ mock_execution_stats,+ "Test Graph",+ "A test graph for processing",+ mock_links,+ ExecutionStatus.COMPLETED,+ )++ # Check graph info+ assert summary["graph_info"]["name"] == "Test Graph"+ assert summary["graph_info"]["description"] == "A test graph for processing"++ # Check nodes with per-node counts+ assert len(summary["nodes"]) == 2+ assert summary["nodes"][0]["block_name"] == "AgentInputBlock"+ assert summary["nodes"][0]["execution_count"] == 1+ assert summary["nodes"][0]["error_count"] == 0+ assert summary["nodes"][1]["block_name"] == "ProcessingBlock"+ assert summary["nodes"][1]["execution_count"] == 1+ assert summary["nodes"][1]["error_count"] == 0++ # Check node relations (UUIDs are truncated to first segment)+ assert len(summary["node_relations"]) == 1+ assert (+ summary["node_relations"][0]["source_node_id"] == "456e7890"+ ) # Truncated+ assert (+ summary["node_relations"][0]["sink_node_id"] == "567e8901"+ ) # Truncated+ assert (+ summary["node_relations"][0]["source_block_name"] == "AgentInputBlock"+ )+ assert summary["node_relations"][0]["sink_block_name"] == "ProcessingBlock"++ # Check overall status+ assert summary["overall_status"]["total_nodes_in_graph"] == 2+ assert summary["overall_status"]["total_executions"] == 3+ assert summary["overall_status"]["total_errors"] == 1+ assert summary["overall_status"]["execution_time_seconds"] == 2.5+ assert summary["overall_status"]["graph_execution_status"] == "COMPLETED"++ # Check input/output data (using actual node UUIDs)+ assert (+ "456e7890-e89b-12d3-a456-426614174002_inputs"+ in summary["input_output_data"]+ )+ assert (+ "456e7890-e89b-12d3-a456-426614174002_outputs"+ in summary["input_output_data"]+ )++ def test_build_summary_with_failed_execution(+ self, mock_node_executions, mock_execution_stats, mock_blocks+ ):+ """Test building summary for execution with failures."""+ mock_links = [] # No links for this test++ with patch(+ "backend.executor.activity_status_generator.get_block"+ ) as mock_get_block:+ mock_get_block.side_effect = lambda block_id: mock_blocks.get(block_id)++ summary = _build_execution_summary(+ mock_node_executions,+ mock_execution_stats,+ "Failed Graph",+ "Test with failures",+ mock_links,+ ExecutionStatus.FAILED,+ )++ # Check that errors are now in node's recent_errors field+ # Find the output node (with truncated UUID)+ output_node = next(+ n for n in summary["nodes"] if n["node_id"] == "678e9012" # Truncated+ )+ assert output_node["error_count"] == 1+ assert output_node["execution_count"] == 1++ # Check recent_errors field+ assert "recent_errors" in output_node+ assert len(output_node["recent_errors"]) == 1+ assert (+ output_node["recent_errors"][0]["error"]+ == "Connection timeout: Unable to reach external service"+ )+ assert (+ "execution_id" in output_node["recent_errors"][0]+ ) # Should include execution ID++ def test_build_summary_with_missing_blocks(+ self, mock_node_executions, mock_execution_stats+ ):+ """Test building summary when blocks are missing."""+ with patch(+ "backend.executor.activity_status_generator.get_block"+ ) as mock_get_block:+ mock_get_block.return_value = None++ summary = _build_execution_summary(+ mock_node_executions,+ mock_execution_stats,+ "Missing Blocks Graph",+ "Test with missing blocks",+ [],+ ExecutionStatus.COMPLETED,+ )++ # Should handle missing blocks gracefully+ assert len(summary["nodes"]) == 0+ # No top-level errors field anymore, errors are in nodes' recent_errors+ assert summary["graph_info"]["name"] == "Missing Blocks Graph"++ def test_build_summary_with_graph_error(+ self, mock_node_executions, mock_execution_stats_with_graph_error, mock_blocks+ ):+ """Test building summary with graph-level error."""+ mock_links = []++ with patch(+ "backend.executor.activity_status_generator.get_block"+ ) as mock_get_block:+ mock_get_block.side_effect = lambda block_id: mock_blocks.get(block_id)++ summary = _build_execution_summary(+ mock_node_executions,+ mock_execution_stats_with_graph_error,+ "Graph with Error",+ "Test with graph error",+ mock_links,+ ExecutionStatus.FAILED,+ )++ # Check that graph error is included in overall status+ assert summary["overall_status"]["has_errors"] is True+ assert (+ summary["overall_status"]["graph_error"]+ == "Graph execution failed: Invalid API credentials"+ )+ assert summary["overall_status"]["total_errors"] == 1+ assert summary["overall_status"]["graph_execution_status"] == "FAILED"++ def test_build_summary_with_different_error_formats(+ self, mock_execution_stats, mock_blocks+ ):+ """Test building summary with different error formats."""+ # Create node executions with different error formats and realistic UUIDs+ mock_executions = [+ NodeExecutionResult(+ user_id="test_user",+ graph_id="test_graph",+ graph_version=1,+ graph_exec_id="test_exec",+ node_exec_id="111e2222-e89b-12d3-a456-426614174010",+ node_id="333e4444-e89b-12d3-a456-426614174011",+ block_id="process_block_id",+ status=ExecutionStatus.FAILED,+ input_data={},+ output_data={"error": ["Simple string error message"]},+ add_time=datetime.now(timezone.utc),+ queue_time=None,+ start_time=None,+ end_time=None,+ ),+ NodeExecutionResult(+ user_id="test_user",+ graph_id="test_graph",+ graph_version=1,+ graph_exec_id="test_exec",+ node_exec_id="555e6666-e89b-12d3-a456-426614174012",+ node_id="777e8888-e89b-12d3-a456-426614174013",+ block_id="process_block_id",+ status=ExecutionStatus.FAILED,+ input_data={},+ output_data={}, # No error in output+ add_time=datetime.now(timezone.utc),+ queue_time=None,+ start_time=None,+ end_time=None,+ ),+ ]++ with patch(+ "backend.executor.activity_status_generator.get_block"+ ) as mock_get_block:+ mock_get_block.side_effect = lambda block_id: mock_blocks.get(block_id)++ summary = _build_execution_summary(+ mock_executions,+ mock_execution_stats,+ "Error Test Graph",+ "Testing error formats",+ [],+ ExecutionStatus.FAILED,+ )++ # Check different error formats - errors are now in nodes' recent_errors+ error_nodes = [n for n in summary["nodes"] if n.get("recent_errors")]+ assert len(error_nodes) == 2++ # String error format - find node with truncated ID+ string_error_node = next(+ n for n in summary["nodes"] if n["node_id"] == "333e4444" # Truncated+ )+ assert len(string_error_node["recent_errors"]) == 1+ assert (+ string_error_node["recent_errors"][0]["error"]+ == "Simple string error message"+ )++ # No error output format - find node with truncated ID+ no_error_node = next(+ n for n in summary["nodes"] if n["node_id"] == "777e8888" # Truncated+ )+ assert len(no_error_node["recent_errors"]) == 1+ assert no_error_node["recent_errors"][0]["error"] == "Unknown error"+++class TestLLMCall:+ """Tests for LLM calling functionality."""++ @pytest.mark.asyncio+ async def test_call_llm_direct_success(self):+ """Test successful LLM call."""+ from pydantic import SecretStr++ from backend.data.model import APIKeyCredentials++ mock_response = LLMResponse(+ raw_response={},+ prompt=[],+ response="Agent successfully processed user input and generated response.",+ tool_calls=None,+ prompt_tokens=50,+ completion_tokens=20,+ )++ with patch(+ "backend.executor.activity_status_generator.llm_call"+ ) as mock_llm_call:+ mock_llm_call.return_value = mock_response++ credentials = APIKeyCredentials(+ id="test",+ provider="openai",+ api_key=SecretStr("test_key"),+ title="Test",+ )++ prompt = [{"role": "user", "content": "Test prompt"}]++ result = await _call_llm_direct(credentials, prompt)++ assert (+ result+ == "Agent successfully processed user input and generated response."+ )+ mock_llm_call.assert_called_once()++ @pytest.mark.asyncio+ async def test_call_llm_direct_no_response(self):+ """Test LLM call with no response."""+ from pydantic import SecretStr++ from backend.data.model import APIKeyCredentials++ with patch(+ "backend.executor.activity_status_generator.llm_call"+ ) as mock_llm_call:+ mock_llm_call.return_value = None++ credentials = APIKeyCredentials(+ id="test",+ provider="openai",+ api_key=SecretStr("test_key"),+ title="Test",+ )++ prompt = [{"role": "user", "content": "Test prompt"}]++ result = await _call_llm_direct(credentials, prompt)++ assert result == "Unable to generate activity summary"+++class TestGenerateActivityStatusForExecution:+ """Tests for the main generate_activity_status_for_execution function."""++ @pytest.mark.asyncio+ async def test_generate_status_success(+ self, mock_node_executions, mock_execution_stats, mock_blocks+ ):+ """Test successful activity status generation."""+ mock_db_client = AsyncMock()+ mock_db_client.get_node_executions.return_value = mock_node_executions++ mock_graph_metadata = MagicMock()+ mock_graph_metadata.name = "Test Agent"+ mock_graph_metadata.description = "A test agent"+ mock_db_client.get_graph_metadata.return_value = mock_graph_metadata++ mock_graph = MagicMock()+ mock_graph.links = []+ mock_db_client.get_graph.return_value = mock_graph++ with patch(+ "backend.executor.activity_status_generator.get_block"+ ) as mock_get_block, patch(+ "backend.executor.activity_status_generator.Settings"+ ) as mock_settings, patch(+ "backend.executor.activity_status_generator._call_llm_direct"+ ) as mock_llm, patch(+ "backend.executor.activity_status_generator.is_feature_enabled",+ return_value=True,+ ):++ mock_get_block.side_effect = lambda block_id: mock_blocks.get(block_id)+ mock_settings.return_value.secrets.openai_api_key = "test_key"+ mock_llm.return_value = (+ "I analyzed your data and provided the requested insights."+ )++ result = await generate_activity_status_for_execution(+ graph_exec_id="test_exec",+ graph_id="test_graph",+ graph_version=1,+ execution_stats=mock_execution_stats,+ db_client=mock_db_client,+ user_id="test_user",+ )++ assert result == "I analyzed your data and provided the requested insights."+ mock_db_client.get_node_executions.assert_called_once()+ mock_db_client.get_graph_metadata.assert_called_once()+ mock_db_client.get_graph.assert_called_once()+ mock_llm.assert_called_once()++ @pytest.mark.asyncio+ async def test_generate_status_feature_disabled(self, mock_execution_stats):+ """Test activity status generation when feature is disabled."""+ mock_db_client = AsyncMock()++ with patch(+ "backend.executor.activity_status_generator.is_feature_enabled",+ return_value=False,+ ):+ result = await generate_activity_status_for_execution(+ graph_exec_id="test_exec",+ graph_id="test_graph",+ graph_version=1,+ execution_stats=mock_execution_stats,+ db_client=mock_db_client,+ user_id="test_user",+ )++ assert result is None+ mock_db_client.get_node_executions.assert_not_called()++ @pytest.mark.asyncio+ async def test_generate_status_no_api_key(self, mock_execution_stats):+ """Test activity status generation with no API key."""+ mock_db_client = AsyncMock()++ with patch(+ "backend.executor.activity_status_generator.Settings"+ ) as mock_settings, patch(+ "backend.executor.activity_status_generator.is_feature_enabled",+ return_value=True,+ ):+ mock_settings.return_value.secrets.openai_api_key = ""++ result = await generate_activity_status_for_execution(+ graph_exec_id="test_exec",+ graph_id="test_graph",+ graph_version=1,+ execution_stats=mock_execution_stats,+ db_client=mock_db_client,+ user_id="test_user",+ )++ assert result is None+ mock_db_client.get_node_executions.assert_not_called()++ @pytest.mark.asyncio+ async def test_generate_status_exception_handling(self, mock_execution_stats):+ """Test activity status generation with exception."""+ mock_db_client = AsyncMock()+ mock_db_client.get_node_executions.side_effect = Exception("Database error")++ with patch(+ "backend.executor.activity_status_generator.Settings"+ ) as mock_settings, patch(+ "backend.executor.activity_status_generator.is_feature_enabled",+ return_value=True,+ ):+ mock_settings.return_value.secrets.openai_api_key = "test_key"++ result = await generate_activity_status_for_execution(+ graph_exec_id="test_exec",+ graph_id="test_graph",+ graph_version=1,+ execution_stats=mock_execution_stats,+ db_client=mock_db_client,+ user_id="test_user",+ )++ assert result is None++ @pytest.mark.asyncio+ async def test_generate_status_with_graph_name_fallback(+ self, mock_node_executions, mock_execution_stats, mock_blocks+ ):+ """Test activity status generation with graph name fallback."""+ mock_db_client = AsyncMock()+ mock_db_client.get_node_executions.return_value = mock_node_executions+ mock_db_client.get_graph_metadata.return_value = None # No metadata+ mock_db_client.get_graph.return_value = None # No graph++ with patch(+ "backend.executor.activity_status_generator.get_block"+ ) as mock_get_block, patch(+ "backend.executor.activity_status_generator.Settings"+ ) as mock_settings, patch(+ "backend.executor.activity_status_generator._call_llm_direct"+ ) as mock_llm, patch(+ "backend.executor.activity_status_generator.is_feature_enabled",+ return_value=True,+ ):++ mock_get_block.side_effect = lambda block_id: mock_blocks.get(block_id)+ mock_settings.return_value.secrets.openai_api_key = "test_key"+ mock_llm.return_value = "Agent completed execution."++ result = await generate_activity_status_for_execution(+ graph_exec_id="test_exec",+ graph_id="test_graph",+ graph_version=1,+ execution_stats=mock_execution_stats,+ db_client=mock_db_client,+ user_id="test_user",+ )++ assert result == "Agent completed execution."+ # Should use fallback graph name in prompt+ call_args = mock_llm.call_args[0][1] # prompt argument+ assert "Graph test_graph" in call_args[1]["content"]+++class TestIntegration:+ """Integration tests to verify the complete flow."""++ @pytest.mark.asyncio+ async def test_full_integration_flow(+ self, mock_node_executions, mock_execution_stats, mock_blocks+ ):+ """Test the complete integration flow."""+ mock_db_client = AsyncMock()+ mock_db_client.get_node_executions.return_value = mock_node_executions++ mock_graph_metadata = MagicMock()+ mock_graph_metadata.name = "Test Integration Agent"+ mock_graph_metadata.description = "Integration test agent"+ mock_db_client.get_graph_metadata.return_value = mock_graph_metadata++ mock_graph = MagicMock()+ mock_graph.links = []+ mock_db_client.get_graph.return_value = mock_graph++ expected_activity = "I processed user input but failed during final output generation due to system error."++ with patch(+ "backend.executor.activity_status_generator.get_block"+ ) as mock_get_block, patch(+ "backend.executor.activity_status_generator.Settings"+ ) as mock_settings, patch(+ "backend.executor.activity_status_generator.llm_call"+ ) as mock_llm_call, patch(+ "backend.executor.activity_status_generator.is_feature_enabled",+ return_value=True,+ ):++ mock_get_block.side_effect = lambda block_id: mock_blocks.get(block_id)+ mock_settings.return_value.secrets.openai_api_key = "test_key"++ mock_response = LLMResponse(+ raw_response={},+ prompt=[],+ response=expected_activity,+ tool_calls=None,+ prompt_tokens=100,+ completion_tokens=30,+ )+ mock_llm_call.return_value = mock_response++ result = await generate_activity_status_for_execution(+ graph_exec_id="test_exec",+ graph_id="test_graph",+ graph_version=1,+ execution_stats=mock_execution_stats,+ db_client=mock_db_client,+ user_id="test_user",+ )++ assert result == expected_activity++ # Verify the correct data was passed to LLM+ llm_call_args = mock_llm_call.call_args+ prompt = llm_call_args[1]["prompt"]++ # Check system prompt+ assert prompt[0]["role"] == "system"+ assert "user's perspective" in prompt[0]["content"]++ # Check user prompt contains expected data+ user_content = prompt[1]["content"]+ assert "Test Integration Agent" in user_content+ assert "user-friendly terms" in user_content.lower()++ # Verify that execution data is present in the prompt+ assert "{" in user_content # Should contain JSON data+ assert "overall_status" in user_content++ @pytest.mark.asyncio+ async def test_manager_integration_with_disabled_feature(+ self, mock_execution_stats+ ):+ """Test that when feature returns None, manager doesn't set activity_status."""+ mock_db_client = AsyncMock()++ with patch(+ "backend.executor.activity_status_generator.is_feature_enabled",+ return_value=False,+ ):+ result = await generate_activity_status_for_execution(+ graph_exec_id="test_exec",+ graph_id="test_graph",+ graph_version=1,+ execution_stats=mock_execution_stats,+ db_client=mock_db_client,+ user_id="test_user",+ )++ # Should return None when disabled+ assert result is None++ # Verify no database calls were made+ mock_db_client.get_node_executions.assert_not_called()+ mock_db_client.get_graph_metadata.assert_not_called()+ mock_db_client.get_graph.assert_not_called()diff --git a/autogpt_platform/backend/backend/executor/manager.py b/autogpt_platform/backend/backend/executor/manager.pyindex 8d26845f0f..e443602713 100644--- a/autogpt_platform/backend/backend/executor/manager.py+++ b/autogpt_platform/backend/backend/executor/manager.py@@ -24,6 +24,9 @@ from backend.data.notifications import (NotificationType,)from backend.data.rabbitmq import SyncRabbitMQ+from backend.executor.activity_status_generator import (+ generate_activity_status_for_execution,+)from backend.executor.utils import LogMetadata, create_execution_queue_configfrom backend.notifications.notifications import queue_notificationfrom backend.util.exceptions import InsufficientBalanceError@@ -575,6 +578,32 @@ class Executor:f"Graph Execution #{graph_exec.graph_exec_id} ended with unexpected status {status}")+ # Generate AI activity status before updating stats+ try:+ activity_status = asyncio.run_coroutine_threadsafe(+ generate_activity_status_for_execution(+ graph_exec_id=graph_exec.graph_exec_id,+ graph_id=graph_exec.graph_id,+ graph_version=graph_exec.graph_version,+ execution_stats=exec_stats,+ db_client=get_db_async_client(),+ user_id=graph_exec.user_id,+ execution_status=status,+ ),+ cls.node_execution_loop,+ ).result(timeout=60.0)+ if activity_status is not None:+ exec_stats.activity_status = activity_status+ log_metadata.info(f"Generated activity status: {activity_status}")+ else:+ log_metadata.debug(+ "Activity status generation disabled, not setting field"+ )++ except Exception as e:+ log_metadata.error(f"Failed to generate activity status: {str(e)}")+ # Don't set activity_status on exception - let it remain None/unset+if graph_exec_result := db_client.update_graph_execution_stats(graph_exec_id=graph_exec.graph_exec_id,status=status,diff --git a/autogpt_platform/frontend/src/components/agents/agent-run-details-view.tsx b/autogpt_platform/frontend/src/components/agents/agent-run-details-view.tsxindex 4588f69a2b..422aa24939 100644--- a/autogpt_platform/frontend/src/components/agents/agent-run-details-view.tsx+++ b/autogpt_platform/frontend/src/components/agents/agent-run-details-view.tsx@@ -15,9 +15,19 @@ import { useBackendAPI } from "@/lib/autogpt-server-api/context";import ActionButtonGroup from "@/components/agptui/action-button-group";import type { ButtonAction } from "@/components/agptui/types";import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";-import { IconRefresh, IconSquare } from "@/components/ui/icons";+import {+ IconRefresh,+ IconSquare,+ IconCircleAlert,+} from "@/components/ui/icons";import { Input } from "@/components/ui/input";import LoadingBox from "@/components/ui/loading";+import {+ Tooltip,+ TooltipContent,+ TooltipProvider,+ TooltipTrigger,+} from "@/components/ui/tooltip";import { useToastOnFail } from "@/components/molecules/Toast/use-toast";import {@@ -228,6 +238,37 @@ export default function AgentRunDetailsView({</CardContent></Card>+ {/* Smart Agent Execution Summary */}+ {run.stats?.activity_status && (+ <Card className="agpt-box">+ <CardHeader>+ <CardTitle className="flex items-center gap-2 font-poppins text-lg">+ Smart Agent Execution Summary+ <TooltipProvider>+ <Tooltip>+ <TooltipTrigger asChild>+ <IconCircleAlert className="size-4 cursor-help text-neutral-500 hover:text-neutral-700" />+ </TooltipTrigger>+ <TooltipContent>+ <p className="max-w-xs">+ This is an AI-generated summary and may not be+ completely accurate. It provides a conversational+ overview of what the agent accomplished during+ execution.+ </p>+ </TooltipContent>+ </Tooltip>+ </TooltipProvider>+ </CardTitle>+ </CardHeader>+ <CardContent>+ <p className="text-sm leading-relaxed text-neutral-700">+ {run.stats.activity_status}+ </p>+ </CardContent>+ </Card>+ )}+{agentRunOutputs !== null && (<Card className="agpt-box"><CardHeader>diff --git a/autogpt_platform/frontend/src/lib/autogpt-server-api/types.ts b/autogpt_platform/frontend/src/lib/autogpt-server-api/types.tsindex dca88d90be..5ae3734484 100644--- a/autogpt_platform/frontend/src/lib/autogpt-server-api/types.ts+++ b/autogpt_platform/frontend/src/lib/autogpt-server-api/types.ts@@ -268,6 +268,7 @@ export type GraphExecutionMeta = {node_exec_time: number;node_exec_time_cpu_only: number;node_exec_count: number;+ activity_status?: string;};};