mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-01-06 14:48:29 +00:00
fix: centralize forced-answer state management
- Move have_forced_answer state management entirely to _should_force_answer method - Remove duplicate state setting in _invoke_loop to prevent race conditions - Maintain consistent state tracking for max iteration handling - Add clarifying comments about state management responsibility This change ensures that the forced-answer state is managed in a single location, preventing potential inconsistencies in state tracking. Co-Authored-By: Joe Moura <joao@crewai.com>
This commit is contained in:
@@ -25,8 +25,12 @@ class CrewAgentExecutorMixin:
|
||||
_printer: Printer = Printer()
|
||||
|
||||
def _should_force_answer(self) -> bool:
|
||||
"""Determine if a forced answer is required based on iteration count."""
|
||||
return self.iterations >= self.max_iter
|
||||
"""Determine if a forced answer is required based on iteration count.
|
||||
Also tracks when a forced answer has been triggered."""
|
||||
if self.iterations >= self.max_iter:
|
||||
self.have_forced_answer = True
|
||||
return True
|
||||
return False
|
||||
|
||||
def _create_short_term_memory(self, output) -> None:
|
||||
"""Create and save a short-term memory item if conditions are met."""
|
||||
|
||||
@@ -170,6 +170,7 @@ class CrewAgentExecutor(CrewAgentExecutorMixin):
|
||||
self.step_callback(formatted_answer)
|
||||
|
||||
if self._should_force_answer():
|
||||
# have_forced_answer is now set in _should_force_answer
|
||||
if self.have_forced_answer:
|
||||
return AgentFinish(
|
||||
thought="",
|
||||
@@ -178,11 +179,9 @@ class CrewAgentExecutor(CrewAgentExecutorMixin):
|
||||
).format(formatted_answer.text),
|
||||
text=formatted_answer.text,
|
||||
)
|
||||
else:
|
||||
formatted_answer.text += (
|
||||
f'\n{self._i18n.errors("force_final_answer")}'
|
||||
)
|
||||
self.have_forced_answer = True
|
||||
formatted_answer.text += (
|
||||
f'\n{self._i18n.errors("force_final_answer")}'
|
||||
)
|
||||
self.messages.append(
|
||||
self._format_msg(formatted_answer.text, role="assistant")
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user