diff --git a/ra_aid/agent_utils.py b/ra_aid/agent_utils.py index c132060..8b78cef 100644 --- a/ra_aid/agent_utils.py +++ b/ra_aid/agent_utils.py @@ -306,6 +306,10 @@ def run_agent_with_retry(agent, prompt: str, config: dict) -> Optional[str]: with InterruptibleSection(): try: + # Track agent execution depth + current_depth = _global_memory.get('agent_depth', 0) + _global_memory['agent_depth'] = current_depth + 1 + for attempt in range(max_retries): check_interrupt() try: @@ -325,5 +329,8 @@ def run_agent_with_retry(agent, prompt: str, config: dict) -> Optional[str]: check_interrupt() time.sleep(0.1) finally: + # Reset depth tracking + _global_memory['agent_depth'] = _global_memory.get('agent_depth', 1) - 1 + if original_handler and threading.current_thread() is threading.main_thread(): signal.signal(signal.SIGINT, original_handler) diff --git a/ra_aid/tools/agent.py b/ra_aid/tools/agent.py index 830aa6f..7cc7505 100644 --- a/ra_aid/tools/agent.py +++ b/ra_aid/tools/agent.py @@ -33,7 +33,7 @@ def request_research(query: str) -> ResearchResult: model = initialize_llm(config.get('provider', 'anthropic'), config.get('model', 'claude-3-5-sonnet-20241022')) # Check recursion depth - current_depth = _global_memory.get('research_depth', 0) + current_depth = _global_memory.get('agent_depth', 0) if current_depth >= RESEARCH_AGENT_RECURSION_LIMIT: print_error("Maximum research recursion depth reached") return { @@ -50,9 +50,6 @@ def request_research(query: str) -> ResearchResult: reason = None try: - # Increment depth counter - _global_memory['research_depth'] = current_depth + 1 - # Run research agent from ..agent_utils import run_research_agent result = run_research_agent( @@ -72,15 +69,12 @@ def request_research(query: str) -> ResearchResult: success = False reason = f"error: {str(e)}" finally: - # Always decrement depth counter - _global_memory['research_depth'] = current_depth + # Get completion message if available + completion_message = _global_memory.get('completion_message', 'Task was completed successfully.' if success else None) - # Get completion message if available - completion_message = _global_memory.get('completion_message', 'Task was completed successfully.' if success else None) - - # Clear completion state from global memory - _global_memory['completion_message'] = '' - _global_memory['task_completed'] = False + # Clear completion state from global memory + _global_memory['completion_message'] = '' + _global_memory['task_completed'] = False return { "completion_message": completion_message, diff --git a/ra_aid/tools/memory.py b/ra_aid/tools/memory.py index 3fe8933..c4a287c 100644 --- a/ra_aid/tools/memory.py +++ b/ra_aid/tools/memory.py @@ -33,7 +33,7 @@ _global_memory: Dict[str, Union[List[Any], Dict[int, str], Dict[int, SnippetInfo 'related_files': {}, # Dict[int, str] - ID to filepath mapping 'related_file_id_counter': 1, # Counter for generating unique file IDs 'plan_completed': False, - 'research_depth': 0, + 'agent_depth': 0, 'work_log': [] # List[WorkLogEntry] - Timestamped work events } @@ -340,6 +340,7 @@ def plan_implementation_completed(message: str) -> str: _global_memory['plan_completed'] = True _global_memory['completion_message'] = message _global_memory['tasks'].clear() # Clear task list when plan is completed + _global_memory['task_id_counter'] = 1 console.print(Panel(Markdown(message), title="✅ Plan Executed")) log_work_event(f"Plan execution completed: {message}") return "Plan completion noted and task list cleared."