JatinAutonomousLabs commited on
Commit
9bba2c0
·
verified ·
1 Parent(s): 7042006

Update graph_merged_simple.py

Browse files
Files changed (1) hide show
  1. graph_merged_simple.py +23 -16
graph_merged_simple.py CHANGED
@@ -594,22 +594,19 @@ def run_qa_agent(state: AgentState) -> Dict[str, Any]:
594
  **add_status("QA", "Approved (Lite tier)")
595
  }
596
 
597
- # Check if at limit
598
  if rework_cycles >= max_rework:
 
599
  return {
600
  "approved": True,
601
- **add_status("QA", "Limit reached - approved")
602
  }
603
 
604
  # QA review
605
  prompt = f"""Review this response against user request.
606
-
607
  REQUEST: {user_input}
608
-
609
  RESPONSE: {draft[:1000]}
610
-
611
  Is this complete and satisfactory? Reply APPROVED or provide specific feedback.
612
-
613
  REVIEW:"""
614
 
615
  try:
@@ -622,11 +619,15 @@ REVIEW:"""
622
  **add_status("QA", "Approved")
623
  }
624
  else:
 
 
 
 
625
  return {
626
  "approved": False,
627
  "qaFeedback": content[:500],
628
- "rework_cycles": rework_cycles + 1,
629
- **add_status("QA", f"Rework needed (cycle {rework_cycles + 1})")
630
  }
631
  except Exception as e:
632
  log.error(f"QA failed: {e}")
@@ -745,16 +746,18 @@ def should_rework(state: AgentState) -> str:
745
  else:
746
  return "archive"
747
  else:
748
- # Check if can rework
749
  cycles = state.get('rework_cycles', 0)
750
  max_cycles = state.get('max_rework_cycles', 0)
751
 
752
- if cycles >= max_cycles:
 
 
753
  return "archive" # Force completion
754
  else:
 
755
  return "planning" # Rework
756
 
757
-
758
  def route_execution_mode(state: AgentState) -> str:
759
  """Route based on execution mode."""
760
  mode = state.get('execution_mode', 'coding')
@@ -971,19 +974,24 @@ def execute_request(user_input: str, session_id: str = None,
971
  "budget_limit": tier_config["max_cost"] or float('inf')
972
  }
973
 
974
- # Select appropriate graph
 
975
  if preferred_tier == cfg.TIER_LITE:
976
  app = lite_app
 
977
  elif preferred_tier == cfg.TIER_FULL:
978
  app = full_app
 
979
  else:
980
  app = standard_app
 
981
 
982
- log.info(f"🚀 Executing with {preferred_tier} tier")
983
 
984
- # Execute graph
985
  try:
986
- final_state = app.invoke(initial_state)
 
987
  return final_state
988
  except Exception as e:
989
  log.exception(f"Execution failed: {e}")
@@ -993,7 +1001,6 @@ def execute_request(user_input: str, session_id: str = None,
993
  "approved": False
994
  }
995
 
996
-
997
  # =============================================================================
998
  # SECTION 10: LEGACY COMPATIBILITY
999
  # =============================================================================
 
594
  **add_status("QA", "Approved (Lite tier)")
595
  }
596
 
597
+ # CRITICAL FIX: Check if at limit BEFORE trying to rework
598
  if rework_cycles >= max_rework:
599
+ log.warning(f"Rework limit reached ({rework_cycles}/{max_rework}), auto-approving")
600
  return {
601
  "approved": True,
602
+ **add_status("QA", f"Approved (limit reached: {rework_cycles}/{max_rework})")
603
  }
604
 
605
  # QA review
606
  prompt = f"""Review this response against user request.
 
607
  REQUEST: {user_input}
 
608
  RESPONSE: {draft[:1000]}
 
609
  Is this complete and satisfactory? Reply APPROVED or provide specific feedback.
 
610
  REVIEW:"""
611
 
612
  try:
 
619
  **add_status("QA", "Approved")
620
  }
621
  else:
622
+ # Increment rework counter
623
+ new_cycles = rework_cycles + 1
624
+ log.info(f"QA requesting rework (cycle {new_cycles}/{max_rework})")
625
+
626
  return {
627
  "approved": False,
628
  "qaFeedback": content[:500],
629
+ "rework_cycles": new_cycles,
630
+ **add_status("QA", f"Rework needed (cycle {new_cycles}/{max_rework})")
631
  }
632
  except Exception as e:
633
  log.error(f"QA failed: {e}")
 
746
  else:
747
  return "archive"
748
  else:
749
+ # CRITICAL FIX: Check the rework cycles BEFORE incrementing
750
  cycles = state.get('rework_cycles', 0)
751
  max_cycles = state.get('max_rework_cycles', 0)
752
 
753
+ # Add 1 because we're about to do another rework
754
+ if cycles + 1 >= max_cycles:
755
+ log.warning(f"Rework limit reached ({cycles + 1}/{max_cycles}), forcing approval")
756
  return "archive" # Force completion
757
  else:
758
+ log.info(f"Rework cycle {cycles + 1}/{max_cycles}")
759
  return "planning" # Rework
760
 
 
761
  def route_execution_mode(state: AgentState) -> str:
762
  """Route based on execution mode."""
763
  mode = state.get('execution_mode', 'coding')
 
974
  "budget_limit": tier_config["max_cost"] or float('inf')
975
  }
976
 
977
+ # Select appropriate graph and set recursion limit
978
+ # CRITICAL: Recursion limits prevent infinite loops
979
  if preferred_tier == cfg.TIER_LITE:
980
  app = lite_app
981
+ recursion_limit = 10 # Lite: ~3-5 nodes
982
  elif preferred_tier == cfg.TIER_FULL:
983
  app = full_app
984
+ recursion_limit = 100 # Full: up to 3 reworks = ~40 nodes max
985
  else:
986
  app = standard_app
987
+ recursion_limit = 30 # Standard: up to 1 rework = ~15-20 nodes
988
 
989
+ log.info(f"🚀 Executing with {preferred_tier} tier (recursion_limit={recursion_limit})")
990
 
991
+ # Execute graph with recursion limit config
992
  try:
993
+ config = {"recursion_limit": recursion_limit}
994
+ final_state = app.invoke(initial_state, config=config)
995
  return final_state
996
  except Exception as e:
997
  log.exception(f"Execution failed: {e}")
 
1001
  "approved": False
1002
  }
1003
 
 
1004
  # =============================================================================
1005
  # SECTION 10: LEGACY COMPATIBILITY
1006
  # =============================================================================