dialog detection in code exec
frdel committed
Jun 17, 2025 at 14:31 UTC
1f33bfcd6a810f1c83a84c9f8e82c70dccb58e65
3 files changed
+40
-3
prompts/default/fw.code.pause_dialog.md
new
+1
@@ -0,0 +1 @@
1
+Potential dialog detected in output. Returning control to agent after {{timeout}} seconds since last output update. Decide whether dialog actually occurred and needs to be addressed, or if it was just a false positive and wait for more output.
\ No newline at end of file
python/tools/code_execution_tool.py
+38
-2
@@ -191,7 +191,8 @@ class CodeExecution(Tool):
191
reset_full_output=True,
192
first_output_timeout=30, # Wait up to x seconds for first output
193
between_output_timeout=15, # Wait up to x seconds between outputs
194
- max_exec_timeout=180, #hard cap on total runtime
194
+ dialog_timeout=5, # potential dialog detection timeout
195
+ max_exec_timeout=180, # hard cap on total runtime
196
sleep_time=0.1,
197
):
198
# Common shell prompt regex patterns (add more as needed)
@@ -201,6 +202,14 @@ class CodeExecution(Tool):
202
re.compile(r"[a-zA-Z0-9_.-]+@[^:]+:[^$#]+[$#] ?$"), # user@host:~$
203
]
204
205
+ # potential dialog detection
206
+ dialog_patterns = [
207
+ re.compile(r"Y/N", re.IGNORECASE), # Y/N anywhere in line
208
+ re.compile(r"yes/no", re.IGNORECASE), # yes/no anywhere in line
209
+ re.compile(r":\s*$"), # line ending with colon
210
+ re.compile(r"\?\s*$"), # line ending with question mark
211
+ ]
212
+
213
start_time = time.time()
214
last_output_time = start_time
215
full_output = ""
@@ -228,7 +237,9 @@ class CodeExecution(Tool):
237
got_output = True
238
239
# Check for shell prompt at the end of output
231
- last_lines = truncated_output.splitlines()[-3:] if truncated_output else []
240
+ last_lines = (
241
+ truncated_output.splitlines()[-3:] if truncated_output else []
242
+ )
243
for line in last_lines:
244
for pat in prompt_patterns:
245
if pat.search(line.strip()):
@@ -272,6 +283,31 @@ class CodeExecution(Tool):
283
self.log.update(content=response)
284
return response
285
286
+ # potential dialog detection
287
+ if now - last_output_time > dialog_timeout:
288
+ # Check for dialog prompt at the end of output
289
+ last_lines = (
290
+ truncated_output.splitlines()[-2:] if truncated_output else []
291
+ )
292
+ for line in last_lines:
293
+ for pat in dialog_patterns:
294
+ if pat.search(line.strip()):
295
+ PrintStyle.info(
296
+ "Detected dialog prompt, returning output early."
297
+ )
298
+
299
+ sysinfo = self.agent.read_prompt(
300
+ "fw.code.pause_dialog.md", timeout=dialog_timeout
301
+ )
302
+ response = self.agent.read_prompt(
303
+ "fw.code.info.md", info=sysinfo
304
+ )
305
+ if truncated_output:
306
+ response = truncated_output + "\n\n" + response
307
+ PrintStyle.warning(sysinfo)
308
+ self.log.update(content=response)
309
+ return response
310
+
311
async def reset_terminal(self, session=0, reason: str | None = None):
312
# Print the reason for the reset to the console if provided
313
if reason:
python/tools/input.py
+1
-1
@@ -8,7 +8,7 @@ class Input(Tool):
8
async def execute(self, keyboard="", **kwargs):
9
# normalize keyboard input
10
keyboard = keyboard.rstrip()
11
- keyboard += "\n"
11
+ # keyboard += "\n" # no need to, code_exec does that
12
13
# terminal session number
14
session = int(self.args.get("session", 0))