Skip to content

fix(agents): reset executor state at the start of each task invocation#5022

Open
NIK-TIGER-BILL wants to merge 1 commit intocrewAIInc:mainfrom
NIK-TIGER-BILL:fix/4389-reset-executor-state-between-tasks
Open

fix(agents): reset executor state at the start of each task invocation#5022
NIK-TIGER-BILL wants to merge 1 commit intocrewAIInc:mainfrom
NIK-TIGER-BILL:fix/4389-reset-executor-state-between-tasks

Conversation

@NIK-TIGER-BILL
Copy link
Copy Markdown

Description

CrewAgentExecutor.invoke() and ainvoke() were not resetting self.messages and self.iterations before starting a new execution. When the same executor instance is reused across tasks in a single Crew run, subsequent tasks inherit the message history and iteration count from previous tasks, leading to:

  • Stale conversation history being sent to the LLM
  • Artificially high iteration counts, causing premature forced-answer injection
  • Non-deterministic behaviour depending on task execution order

Fix

Add explicit resets at the top of both invoke() and ainvoke():

self.messages = []
self.iterations = 0

This mirrors the behaviour of the experimental AgentExecutor, which uses a fresh AgentReActState() for every invocation.

Testing

Added TestExecutorStateReset class in tests/agents/test_async_agent_executor.py with two tests:

  • test_invoke_resets_messages_and_iterations – verifies sync path
  • test_ainvoke_resets_messages_and_iterations – verifies async path

Fixes #4389

CrewAgentExecutor.invoke() and ainvoke() were not resetting
self.messages and self.iterations before starting a new execution,
causing subsequent tasks to inherit message history and iteration count
from previous tasks in the same Crew run.

Add explicit resets at the top of both invoke() and ainvoke():

    self.messages = []
    self.iterations = 0

This mirrors the behaviour of the experimental AgentExecutor which
uses a fresh AgentReActState() for every invocation.

Added regression tests in tests/agents/test_async_agent_executor.py
(TestExecutorStateReset) covering both sync and async paths.

Fixes crewAIInc#4389

Signed-off-by: NIK-TIGER-BILL <[email protected]>
@NIK-TIGER-BILL NIK-TIGER-BILL force-pushed the fix/4389-reset-executor-state-between-tasks branch from e2e6862 to d34938a Compare March 27, 2026 07:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] CrewAgentExecutor does not reset messages and iterations between task executions

1 participant