Skip to content

Framework for orchestrating role-playing, autonomous AI agents. By fostering collaborative intelligence, CrewAI empowers agents to work together seamlessly, tackling complex tasks.

fix(agents): restore task description when replanning fails (#8308)

reuben rouseadd3f7bf1e43
diff --git a/lib/crewai/src/crewai/agents/agent_executor.py b/lib/crewai/src/crewai/agents/agent_executor.py
index 0974ae7cc..492faddf2 100644
--- a/lib/crewai/src/crewai/agents/agent_executor.py
+++ b/lib/crewai/src/crewai/agents/agent_executor.py
@@ -2844,8 +2844,10 @@ class AgentExecutor(Flow[AgentExecutorState], BaseAgentExecutor):
if self.task:
original_description = self.task.description
self.task.description = enhanced_description
- output = planning_handler.handle_agent_reasoning()
- self.task.description = original_description
+ try:
+ output = planning_handler.handle_agent_reasoning()
+ finally:
+ self.task.description = original_description
else:
# description is a read-only property — recreate with enhanced text
input_text = getattr(self, "_kickoff_input", "")
diff --git a/lib/crewai/tests/agents/test_agent_executor.py b/lib/crewai/tests/agents/test_agent_executor.py
index e3ea8d89f..6a42d9f59 100644
--- a/lib/crewai/tests/agents/test_agent_executor.py
+++ b/lib/crewai/tests/agents/test_agent_executor.py
@@ -2988,6 +2988,22 @@ class TestPlanGenerationIsolation:
"generate_plan should store plan in state, not task.description"
)
+ def test_failed_replan_restores_task_description(self):
+ task = SimpleNamespace(description="What is the capital of France?")
+ executor = _build_executor(
+ agent=SimpleNamespace(verbose=False),
+ task=task,
+ )
+
+ with patch("crewai.utilities.reasoning_handler.AgentReasoning") as reasoning:
+ reasoning.return_value.handle_agent_reasoning.side_effect = ValueError(
+ "planner failed"
+ )
+ executor._trigger_replan("step 1 is blocked")
+
+ assert task.description == "What is the capital of France?"
+ assert executor.state.last_replan_reason == "Replan failed: planner failed"
+
# Todo Status Tracking