Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions lib/crewai/src/crewai/experimental/agent_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3196,6 +3196,43 @@ def _is_training_mode(self) -> bool:
"""
return bool(self.crew and self.crew._train)

def _format_feedback_message(self, feedback: str) -> LLMMessage:
"""Format feedback as a message for the LLM.

Args:
feedback: User feedback string.

Returns:
Formatted message dict.
"""
return format_message_for_llm(
I18N_DEFAULT.slice("feedback_instructions").format(feedback=feedback)
)
Comment on lines +3199 to +3210

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟠 Major | 🏗️ Heavy lift

Planning-mode reruns still drop the human feedback.

The provider appends _format_feedback_message(feedback) to context.messages, but a planning-enabled rerun goes through generate_plan() and StepExecutor.execute() using task text/dependency results only. Nothing on that path reads self.state.messages, so planned agents can satisfy the protocol surface while ignoring the feedback entirely.

Also applies to: 3212-3230

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@lib/crewai/src/crewai/experimental/agent_executor.py` around lines 3199 -
3210, Planning-mode reruns are still losing the human feedback because the
`generate_plan()` / `StepExecutor.execute()` path only uses task text and
dependency results, not the appended feedback message from
`_format_feedback_message()`. Update the planning execution flow in
`AgentExecutor` so the rerun preserves and forwards the latest feedback from
`self.state.messages` (or the equivalent stored context) into plan generation
and step execution, ensuring planned agents see the same feedback that
non-planning runs receive.


def _invoke_loop(self) -> AgentFinish:
"""Invoke the agent loop and return the result for HITL feedback processing."""
self._finalize_called = False
self.state.is_finished = False
self.state.iterations = 0
self.kickoff()
result = self.state.current_answer
if not isinstance(result, AgentFinish):
raise RuntimeError("Agent execution ended without reaching a final answer.")
return result
Comment thread
coderabbitai[bot] marked this conversation as resolved.

async def _ainvoke_loop(self) -> AgentFinish:
"""Invoke the agent loop asynchronously and return the result for HITL feedback processing."""
self._finalize_called = False
self.state.is_finished = False
self.state.iterations = 0
await self.kickoff_async()
result = self.state.current_answer
if not isinstance(result, AgentFinish):
raise RuntimeError("Agent execution ended without reaching a final answer.")
return result




# Backward compatibility alias (deprecated)
CrewAgentExecutorFlow = AgentExecutor
52 changes: 52 additions & 0 deletions lib/crewai/tests/agents/test_agent_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2391,3 +2391,55 @@ def test_anthropic_provider_has_image_block_converter(self):
assert hasattr(AnthropicCompletion, "_convert_image_blocks"), (
"Anthropic provider must have _convert_image_blocks for auto-conversion"
)


class TestAgentExecutorHumanInputProtocolContract:
"""AgentExecutor must implement full ExecutorContext and AsyncExecutorContext protocol for human_input=True."""

def test_agent_executor_implements_human_input_protocol(self):
from crewai.experimental.agent_executor import AgentExecutor

assert hasattr(AgentExecutor, "_format_feedback_message")
assert hasattr(AgentExecutor, "_invoke_loop")
assert hasattr(AgentExecutor, "_ainvoke_loop")
assert hasattr(AgentExecutor, "_is_training_mode")

def test_agent_executor_format_feedback_message(self, mock_dependencies):
executor = _build_executor(**mock_dependencies)
msg = executor._format_feedback_message("Please fix the summary")
assert msg["role"] == "user"
assert "Please fix the summary" in msg["content"]

def test_agent_executor_invoke_loop_resets_iterations(self, mock_dependencies):
executor = _build_executor(**mock_dependencies)
executor.state.iterations = 10
iterations_at_kickoff = None

def mock_kickoff_impl():
nonlocal iterations_at_kickoff
iterations_at_kickoff = executor.state.iterations
executor.state.current_answer = AgentFinish(thought="done", output="ok", text="ok")

with patch.object(executor, "kickoff", side_effect=mock_kickoff_impl):
res = executor._invoke_loop()
assert iterations_at_kickoff == 0
assert res.output == "ok"

@pytest.mark.asyncio
async def test_agent_executor_ainvoke_loop_resets_iterations(self, mock_dependencies):
executor = _build_executor(**mock_dependencies)
executor.state.iterations = 10
iterations_at_kickoff = None

async def mock_kickoff_async_impl():
nonlocal iterations_at_kickoff
iterations_at_kickoff = executor.state.iterations
executor.state.current_answer = AgentFinish(thought="done", output="ok_async", text="ok_async")

with patch.object(executor, "kickoff_async", side_effect=mock_kickoff_async_impl):
res = await executor._ainvoke_loop()
assert iterations_at_kickoff == 0
assert res.output == "ok_async"