Agent Zero AI framework
Edit: Code Exe Tool - The get_terminal_output function will now detect if the last 128 characters repeat 5 times consecutively and will stop execution if this pattern occurs, indicating a likely infinite loop. Now, when a repeating output pattern is detected: An error message will be printed to the console, indicating the loop and that the session is being reset. The log will be updated. self.reset_terminal(session=session) will be called to reset the specific session where the loop occurred. A detailed message will be returned to the agent, including the captured output, the loop detection confirmation, a notification that the session was reset, and advice to review the problematic code/command.
deci2283e445e230
diff --git a/python/tools/code_execution_tool.py b/python/tools/code_execution_tool.pyindex ff8357705..4a9a20871 100644--- a/python/tools/code_execution_tool.py+++ b/python/tools/code_execution_tool.py@@ -232,10 +232,13 @@ class CodeExecution(Tool):last_output_time = start_timefull_output = ""got_output = False+ # --- Configuration for repetition detection ---+ chunk_size = 128 # Size of the block to check for repetition+ repeat_threshold = 5 # How many times the block must repeat consecutivelywhile True:await asyncio.sleep(sleep_time)- full_output, partial_output = await self.state.shells[session].read_output(+ current_full_output, partial_output = await self.state.shells[session].read_output(timeout=between_output_timeout, reset_full_output=reset_full_output)reset_full_output = False@@ -245,10 +248,28 @@ class CodeExecution(Tool):now = time.time()if partial_output:PrintStyle(font_color="#85C1E9").stream(partial_output)+ full_output += partial_output # Append new outputself.log.update(content=full_output)last_output_time = nowgot_output = True+ # --- Check for repeating output pattern ---+ required_len = chunk_size * repeat_threshold+ if len(full_output) >= required_len:+ check_segment = full_output[-required_len:]+ last_chunk = full_output[-chunk_size:]+ expected_segment = last_chunk * repeat_threshold+ if check_segment == expected_segment:+ loop_detected_msg = f"Detected repeating output pattern (last {chunk_size} chars repeated {repeat_threshold} times), likely an infinite loop."+ PrintStyle.error(f"{loop_detected_msg} Resetting session {session}.")+ self.log.update(content=full_output + f"\n--- LOOP DETECTED --- Session {session} reset.")+ # Automatically reset the problematic session+ await self.reset_terminal(session=session)+ # Return informative message to the agent+ return f"{full_output}\n--- LOOP DETECTED & SESSION RESET ---\n{loop_detected_msg}\nThe terminal session {session} was automatically reset.\nPlease review the code or command that caused the loop to prevent recurrence."+ # --- End of repetition check ---++# Check for shell prompt at the end of outputlast_lines = full_output.splitlines()[-3:] if full_output else []for line in last_lines: