refactor(graph): Add iteration limit to prevent infinite loops in code correction
This commit is contained in:
@@ -31,9 +31,13 @@ def error_corrector_node(state: AgentState) -> dict:
|
||||
try:
|
||||
response = structured_llm.invoke(messages)
|
||||
logger.info("[bold green]Correction generated.[/bold green]")
|
||||
|
||||
current_iterations = state.get("iterations", 0)
|
||||
|
||||
return {
|
||||
"code": response.parsed_code,
|
||||
"error": None # Clear error after fix attempt
|
||||
"error": None, # Clear error after fix attempt
|
||||
"iterations": current_iterations + 1
|
||||
}
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to correct code: {str(e)}")
|
||||
|
||||
@@ -66,7 +66,8 @@ def query_analyzer_node(state: AgentState) -> dict:
|
||||
|
||||
return {
|
||||
"analysis": analysis_dict,
|
||||
"next_action": next_action
|
||||
"next_action": next_action,
|
||||
"iterations": 0
|
||||
}
|
||||
|
||||
|
||||
@@ -31,3 +31,6 @@ class AgentState(AS):
|
||||
|
||||
# Routing hint: "clarify", "plan", "research", "end"
|
||||
next_action: str
|
||||
|
||||
# Number of execution attempts
|
||||
iterations: int
|
||||
|
||||
@@ -10,6 +10,8 @@ from ea_chatbot.graph.nodes.researcher import researcher_node
|
||||
from ea_chatbot.graph.nodes.clarification import clarification_node
|
||||
from ea_chatbot.graph.nodes.summarize_conversation import summarize_conversation_node
|
||||
|
||||
MAX_ITERATIONS = 3
|
||||
|
||||
def router(state: AgentState) -> str:
|
||||
"""Route to the next node based on the analysis."""
|
||||
next_action = state.get("next_action")
|
||||
@@ -59,6 +61,9 @@ def create_workflow():
|
||||
# Executor routing
|
||||
def executor_router(state: AgentState) -> str:
|
||||
if state.get("error"):
|
||||
# Check for iteration limit to prevent infinite loops
|
||||
if state.get("iterations", 0) >= MAX_ITERATIONS:
|
||||
return "summarizer"
|
||||
return "error_corrector"
|
||||
return "summarizer"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user