From 2ace8cccc86f650747ef23ffbb2af2e791d75b4c Mon Sep 17 00:00:00 2001 From: Patrick Buckley Date: Sun, 29 Mar 2026 17:09:44 -0700 Subject: [PATCH] fix: distinguish user cancel from crash in bash tool results (#235) * fix: distinguish user cancel from crash in bash tool results When a user cancels a running bash command, the process is killed with SIGKILL (exit code -9). Previously this showed as an error, causing the model to retry. Now checks cancel.is_set() after proc exit and returns "Cancelled by user." as a non-error result so the model knows to stop rather than retry. * fix: use -signal.SIGKILL instead of magic -9 Copilot review: replace hard-coded -9 with -signal.SIGKILL for clarity. Popen.returncode is negative of signal number when killed. --- turnstone/core/session.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/turnstone/core/session.py b/turnstone/core/session.py index d5c2d224..73ac4c6e 100644 --- a/turnstone/core/session.py +++ b/turnstone/core/session.py @@ -4301,6 +4301,13 @@ class ChatSession: if timed_out.is_set(): raise subprocess.TimeoutExpired(cmd="bash", timeout=timeout) + # Distinguish user cancel from unexpected SIGKILL. + # Popen.returncode is negative of the signal number when killed. + if cancel.is_set() and proc.returncode == -signal.SIGKILL: + msg = "Cancelled by user." + self._report_tool_result(call_id, "bash", msg) + return call_id, msg + output = "".join(stdout_parts) if stderr_lines: tagged = "".join(f"[stderr] {line}" for line in stderr_lines)