refactor(graph): Add iteration limit to prevent infinite loops in code correction

This commit is contained in:
Yunxiao Xu
2026-02-11 16:50:48 -08:00
parent 156e1f7512
commit 004f9fd9c9
4 changed files with 15 additions and 2 deletions

View File

@@ -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)}")

View File

@@ -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
}

View File

@@ -31,3 +31,6 @@ class AgentState(AS):
# Routing hint: "clarify", "plan", "research", "end"
next_action: str
# Number of execution attempts
iterations: int

View File

@@ -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"