Edit: get_terminal_output function in code_execution_tool.py is now enhanced to detect common shell prompt patterns (like (venv) ...$, root@...:~#, etc.) using regex. When such a prompt is detected in the output, the function will immediately break and return the output, rather than waiting for the full timeout.

deci committed May 10, 2025 at 19:21 UTC 466741e4a09224617a0af51ba0a13d73496f5171
1 file changed +17
python/tools/code_execution_tool.py
+17
@@ -8,6 +8,7 @@ from python.helpers.print_style import PrintStyle
8 from python.helpers.shell_local import LocalInteractiveSession
9 from python.helpers.shell_ssh import SSHInteractiveSession
10 from python.helpers.docker import DockerContainerManager
11 +import re
12
13
14 @dataclass
@@ -218,7 +219,15 @@ class CodeExecution(Tool):
219 - Each new output resets the between_output_timeout timer.
220 - No hard cap on total runtime.
221 - If no output for between_output_timeout after last output, or no output at all for first_output_timeout, returns.
222 + - If a common shell prompt is detected, returns immediately.
223 """
224 + # Common shell prompt regex patterns (add more as needed)
225 + prompt_patterns = [
226 + re.compile(r"\\(venv\\).+[$#] ?$"), # (venv) ...$ or (venv) ...#
227 + re.compile(r"root@[^:]+:[^#]+# ?$"), # root@container:~#
228 + re.compile(r"[a-zA-Z0-9_.-]+@[^:]+:[^$#]+[$#] ?$"), # user@host:~$
229 + ]
230 +
231 start_time = time.time()
232 last_output_time = start_time
233 full_output = ""
@@ -240,6 +249,14 @@ class CodeExecution(Tool):
249 last_output_time = now
250 got_output = True
251
252 + # Check for shell prompt at the end of output
253 + last_lines = full_output.splitlines()[-3:] if full_output else []
254 + for line in last_lines:
255 + for pat in prompt_patterns:
256 + if pat.search(line.strip()):
257 + PrintStyle(font_color="#229954").print("Detected shell prompt, returning output early.")
258 + return full_output
259 +
260 if not got_output:
261 # Waiting for first output
262 if now - start_time > first_output_timeout: