From b88ea420594f3b0863aaa4ec2785689ea12f7d24 Mon Sep 17 00:00:00 2001 From: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Fri, 9 May 2025 07:46:35 +0000 Subject: [PATCH] Add test for tool result duplication fix Co-Authored-By: Joe Moura --- tests/test_tool_result_duplication.py | 77 +++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 tests/test_tool_result_duplication.py diff --git a/tests/test_tool_result_duplication.py b/tests/test_tool_result_duplication.py new file mode 100644 index 000000000..232b443f8 --- /dev/null +++ b/tests/test_tool_result_duplication.py @@ -0,0 +1,77 @@ +import pytest +from unittest.mock import patch, MagicMock + +from crewai.agent import Agent +from crewai.task import Task +from crewai.crew import Crew +from crewai.tools import BaseTool +from crewai.agents.crew_agent_executor import CrewAgentExecutor + + +class TestTool(BaseTool): + name: str = "Test Tool" + description: str = "A test tool to verify tool result duplication is fixed" + + def _run(self) -> str: + return "Test tool result" + + +def test_tool_result_not_duplicated_in_messages(): + """Test that tool results are not duplicated in messages. + + This test verifies the fix for issue #2798, where tool results were being + duplicated in the LLM prompt, increasing token usage and latency. + """ + agent = Agent( + role="Test Agent", + goal="Test the tool result duplication fix", + backstory="I am a test agent", + tools=[TestTool()], + ) + + task = Task( + description="Use the test tool and return the result", + expected_output="The test tool result", + agent=agent, + ) + + crew = Crew(agents=[agent], tasks=[task]) + + with patch.object(CrewAgentExecutor, '_invoke_loop') as mock_invoke_loop: + executor = CrewAgentExecutor( + agent=agent, + task=task, + tools=agent.tools, + llm=agent.llm, + callbacks=[], + ) + + executor.messages = [{"role": "user", "content": "Use the test tool"}] + + from crewai.agents.parser import AgentAction + from crewai.tools.tool_types import ToolResult + + agent_action = AgentAction( + tool="Test Tool", + tool_input={}, + thought="I should use the test tool", + text="I'll use the Test Tool", + ) + + tool_result = ToolResult( + result="Test tool result", + tool_name="Test Tool", + tool_args={}, + result_as_answer=False, + ) + + executor._handle_agent_action(agent_action, tool_result) + + tool_result_count = sum( + 1 for msg in executor.messages if msg.get("content") == "Test tool result" + ) + + assert tool_result_count <= 1, "Tool result is duplicated in messages" + + observation_text = f"Observation: {tool_result.result}" + assert observation_text in agent_action.text, "Tool result not properly formatted in agent action text"