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.
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.
deci committed
May 12, 2025 at 22:20 UTC
2283e445e23044941e65945fafb8238b9ef0ff79
1 file changed
+22
-1
python/tools/code_execution_tool.py
+22
-1
@@ -232,10 +232,13 @@ class CodeExecution(Tool):
232
last_output_time = start_time
233
full_output = ""
234
got_output = False
235
+ # --- Configuration for repetition detection ---
236
+ chunk_size = 128 # Size of the block to check for repetition
237
+ repeat_threshold = 5 # How many times the block must repeat consecutively
238
239
while True:
240
await asyncio.sleep(sleep_time)
238
- full_output, partial_output = await self.state.shells[session].read_output(
241
+ current_full_output, partial_output = await self.state.shells[session].read_output(
242
timeout=between_output_timeout, reset_full_output=reset_full_output
243
)
244
reset_full_output = False
@@ -245,10 +248,28 @@ class CodeExecution(Tool):
248
now = time.time()
249
if partial_output:
250
PrintStyle(font_color="#85C1E9").stream(partial_output)
251
+ full_output += partial_output # Append new output
252
self.log.update(content=full_output)
253
last_output_time = now
254
got_output = True
255
256
+ # --- Check for repeating output pattern ---
257
+ required_len = chunk_size * repeat_threshold
258
+ if len(full_output) >= required_len:
259
+ check_segment = full_output[-required_len:]
260
+ last_chunk = full_output[-chunk_size:]
261
+ expected_segment = last_chunk * repeat_threshold
262
+ if check_segment == expected_segment:
263
+ loop_detected_msg = f"Detected repeating output pattern (last {chunk_size} chars repeated {repeat_threshold} times), likely an infinite loop."
264
+ PrintStyle.error(f"{loop_detected_msg} Resetting session {session}.")
265
+ self.log.update(content=full_output + f"\n--- LOOP DETECTED --- Session {session} reset.")
266
+ # Automatically reset the problematic session
267
+ await self.reset_terminal(session=session)
268
+ # Return informative message to the agent
269
+ 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."
270
+ # --- End of repetition check ---
271
+
272
+
273
# Check for shell prompt at the end of output
274
last_lines = full_output.splitlines()[-3:] if full_output else []
275
for line in last_lines: