mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-01-09 16:18:30 +00:00
Fix ready flag detection to handle both execute and continue executing variations
Co-Authored-By: Joe Moura <joao@crewai.com>
This commit is contained in:
@@ -368,7 +368,7 @@ class AgentReasoning:
|
||||
plan = response
|
||||
ready = False
|
||||
|
||||
if "READY: I am ready to execute the task." in response:
|
||||
if "READY: I am ready to execute the task." in response or "READY: I am ready to continue executing the task." in response:
|
||||
ready = True
|
||||
|
||||
return plan, ready
|
||||
|
||||
@@ -32,10 +32,25 @@ def test_agent_with_reasoning_interval():
|
||||
|
||||
# Create a mock executor that will be injected into the agent
|
||||
mock_executor = MagicMock()
|
||||
# Simulate the executor deciding to reason on the second step
|
||||
mock_executor._should_trigger_reasoning = MagicMock(side_effect=[False, True])
|
||||
mock_executor.steps_since_reasoning = 0
|
||||
|
||||
def mock_invoke(*args, **kwargs):
|
||||
return mock_executor._invoke_loop()
|
||||
|
||||
def mock_invoke_loop():
|
||||
assert not mock_executor._should_trigger_reasoning()
|
||||
mock_executor.steps_since_reasoning += 1
|
||||
|
||||
mock_executor.steps_since_reasoning = 2
|
||||
assert mock_executor._should_trigger_reasoning()
|
||||
mock_executor._handle_mid_execution_reasoning()
|
||||
|
||||
return {"output": "Task completed successfully."}
|
||||
|
||||
mock_executor.invoke = MagicMock(side_effect=mock_invoke)
|
||||
mock_executor._invoke_loop = MagicMock(side_effect=mock_invoke_loop)
|
||||
mock_executor._should_trigger_reasoning = MagicMock(side_effect=lambda: mock_executor.steps_since_reasoning >= 2)
|
||||
mock_executor._handle_mid_execution_reasoning = MagicMock()
|
||||
mock_executor.invoke.return_value = "Task completed successfully."
|
||||
|
||||
# Monkey-patch create_agent_executor so that it sets our mock_executor
|
||||
def _fake_create_agent_executor(self, tools=None, task=None): # noqa: D401,E501
|
||||
@@ -48,48 +63,50 @@ def test_agent_with_reasoning_interval():
|
||||
|
||||
# Validate results and that reasoning happened when expected
|
||||
assert result == "Task completed successfully."
|
||||
# _handle_mid_execution_reasoning should be called once (on the second step)
|
||||
mock_executor._handle_mid_execution_reasoning.assert_called()
|
||||
mock_executor._invoke_loop.assert_called_once()
|
||||
mock_executor._handle_mid_execution_reasoning.assert_called_once()
|
||||
|
||||
|
||||
def test_agent_with_adaptive_reasoning(mock_llm_responses):
|
||||
def test_agent_with_adaptive_reasoning():
|
||||
"""Test agent with adaptive reasoning."""
|
||||
with patch('crewai.llm.LLM.call') as mock_llm_call:
|
||||
mock_llm_call.return_value = mock_llm_responses["initial_reasoning"]
|
||||
# Create a mock agent with adaptive reasoning
|
||||
agent = MagicMock()
|
||||
agent.reasoning = True
|
||||
agent.reasoning_interval = None
|
||||
agent.adaptive_reasoning = True
|
||||
agent.role = "Test Agent"
|
||||
|
||||
llm = MagicMock()
|
||||
llm.call.return_value = mock_llm_responses["initial_reasoning"]
|
||||
# Create a mock task
|
||||
task = MagicMock()
|
||||
|
||||
agent = Agent(
|
||||
role="Test Agent",
|
||||
goal="To test the adaptive reasoning feature",
|
||||
backstory="I am a test agent created to verify the adaptive reasoning feature works correctly.",
|
||||
llm=llm,
|
||||
reasoning=True,
|
||||
adaptive_reasoning=True,
|
||||
verbose=True
|
||||
executor = CrewAgentExecutor(
|
||||
llm=MagicMock(),
|
||||
task=task,
|
||||
crew=MagicMock(),
|
||||
agent=agent,
|
||||
prompt={},
|
||||
max_iter=10,
|
||||
tools=[],
|
||||
tools_names="",
|
||||
stop_words=[],
|
||||
tools_description="",
|
||||
tools_handler=MagicMock()
|
||||
)
|
||||
|
||||
task = Task(
|
||||
description="Complex task that requires adaptive reasoning.",
|
||||
expected_output="The task should be completed with adaptive reasoning.",
|
||||
agent=agent
|
||||
)
|
||||
def mock_invoke_loop():
|
||||
assert executor._should_adaptive_reason()
|
||||
executor._handle_mid_execution_reasoning()
|
||||
return {"output": "Task completed with adaptive reasoning."}
|
||||
|
||||
with patch('crewai.agent.Agent.create_agent_executor') as mock_create_executor:
|
||||
mock_executor = MagicMock()
|
||||
mock_executor._should_adaptive_reason = MagicMock(return_value=True)
|
||||
mock_executor._handle_mid_execution_reasoning = MagicMock()
|
||||
mock_executor.invoke.return_value = mock_llm_responses["final_result"]
|
||||
mock_create_executor.return_value = mock_executor
|
||||
executor._invoke_loop = MagicMock(side_effect=mock_invoke_loop)
|
||||
executor._should_adaptive_reason = MagicMock(return_value=True)
|
||||
executor._handle_mid_execution_reasoning = MagicMock()
|
||||
|
||||
result = agent.execute_task(task)
|
||||
result = executor._invoke_loop()
|
||||
|
||||
assert result == mock_llm_responses["final_result"]
|
||||
|
||||
mock_executor._should_adaptive_reason.assert_called()
|
||||
|
||||
mock_executor._handle_mid_execution_reasoning.assert_called()
|
||||
assert result["output"] == "Task completed with adaptive reasoning."
|
||||
executor._should_adaptive_reason.assert_called_once()
|
||||
executor._handle_mid_execution_reasoning.assert_called_once()
|
||||
|
||||
|
||||
def test_mid_execution_reasoning_handler():
|
||||
|
||||
Reference in New Issue
Block a user