Edit: Loop detection now trunacates the looped info so we flood the LLM with less repeated context.
deci committed
May 12, 2025 at 23:52 UTC
e8f8050b0aeef5fe3a9a13bec009cf3f219e0cc3
1 file changed
+24
-6
python/tools/code_execution_tool.py
+24
-6
@@ -262,11 +262,23 @@ class CodeExecution(Tool):
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.")
265
+
266
+ # --- Truncate output for feedback ---
267
+ max_output_length = 2048
268
+ truncated_output = full_output[-max_output_length:]
269
+ if len(full_output) > max_output_length:
270
+ summary_prefix = f"[... Output truncated to last {max_output_length} characters ...]\n"
271
+ truncated_output = summary_prefix + truncated_output
272
+ else:
273
+ summary_prefix = ""
274
+ # --- End of truncation ---
275
+
276
+ self.log.update(content=truncated_output + f"\n--- LOOP DETECTED --- Session {session} reset.")
277
# 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."
278
+ reset_reason = f"Detected repeating output pattern (last {chunk_size} chars repeated {repeat_threshold} times)"
279
+ await self.reset_terminal(session=session, reason=reset_reason)
280
+ # Return informative message to the agent with truncated output and pattern
281
+ return f"{truncated_output}\n--- LOOP DETECTED & SESSION RESET ---\n{loop_detected_msg}\nRepeating pattern identified: '{last_chunk[:128]}{'...' if len(last_chunk)>128 else ''}'\nThe terminal session {session} was automatically reset.\nPlease review the code or command that caused the loop to prevent recurrence."
282
# --- End of repetition check ---
283
284
@@ -291,9 +303,15 @@ class CodeExecution(Tool):
303
304
return full_output
305
294
- async def reset_terminal(self, session=0):
306
+ async def reset_terminal(self, session=0, reason: str | None = None):
307
+ # Print the reason for the reset to the console if provided
308
+ if reason:
309
+ PrintStyle(font_color="#FFA500", bold=True).print(f"Resetting terminal session {session}... Reason: {reason}")
310
+ else:
311
+ PrintStyle(font_color="#FFA500", bold=True).print(f"Resetting terminal session {session}...")
312
+
313
# Only reset the specified session while preserving others
314
await self.prepare_state(reset=True, session=session)
297
- response = self.agent.read_prompt("fw.code_reset.md")
315
+ response = self.agent.read_prompt("fw.code_reset.md") # Standard message for agent history
316
self.log.update(content=response)
317
return response
\ No newline at end of file