fix: cancel current command/stream on Ctrl+C instead of exiting - #89
Draft
aryansk wants to merge 1 commit into
Draft
fix: cancel current command/stream on Ctrl+C instead of exiting#89aryansk wants to merge 1 commit into
aryansk wants to merge 1 commit into
Conversation
execute_bash now catches KeyboardInterrupt from subprocess.run and returns "Command execution cancelled by user."; the streaming loop stops on interrupt and keeps the partial reply so the session and history survive; tool calls interrupted mid-run cancel cleanly. A Ctrl+C at the prompt still exits with the goodbye screen. Closes shauryagangrade#51.
shauryagangrade
approved these changes
Aug 15, 2026
shauryagangrade
left a comment
Owner
There was a problem hiding this comment.
Well-scoped fix with solid tests. The three interrupt sites (stream, tool dispatch, subprocess) each get a sensible behavior: keep the session alive, persist whatever text accumulated, and drop half-formed tool_calls from history (tool_calls=[] on interrupt). The new tests cover the important cases, including the empty-stream interrupt and the subprocess-level interrupt.
Notes (non-blocking):
execute_bash: on Ctrl+C,subprocess.runmay leave the child process running on some platforms (the SIGINT usually hits the whole process group, but not always). Consider killing theresultprocess onKeyboardInterruptif you want to be airtight. Not blocking._run_toolnow swallowsKeyboardInterruptinto a tool result string; if the interrupt lands between tools in the loop it's fine, but double-check the agent loop incli.pystill sees aKeyboardInterruptescape for the outermost Ctrl+C-to-quit path.- Minor: the
(streaming stopped by user)info line is printed afterassistant_end()— verify it renders below the frozen live region rather than overwriting it (RichLive.stop()handles this, but worth a quick manual check).
Approve — ready once you mark the PR ready for review.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Pressing Ctrl+C while the assistant is streaming a reply, or while a bash
command is running, terminates the entire GCode session and prints the
goodbye screen. Any messages since the last save are lost — the turn in
progress is never persisted. Closes #51.
Change
Three interrupt-handling fixes:
execute_bash(gcode/tools.py): catchesKeyboardInterruptfromsubprocess.runand returnsCommand execution cancelled by user.instead of letting it kill the REPL. Applies both to the prompt phase
(already handled) and the running-command phase (new).
gcode/agent.py_stream): a Ctrl+C during streamingstops the stream, keeps whatever was accumulated so far as the turn's
assistant message (with no half-formed tool calls), and persists it —
the session stays alive and history is saved.
gcode/agent.py_run_tool): a Ctrl+C during any toolcall returns
Command execution cancelled by user.to the model insteadof propagating.
A Ctrl+C (or Ctrl+D) at the prompt still exits with the goodbye screen,
unchanged.
Why this approach
Minimal, targeted changes: the interrupt is caught at the two async
boundaries where it can arrive mid-work (streaming and subprocess), and the
tool-call boundary is hardened so a cancelled tool result is recorded in
history like any other tool result. No changes to the prompt loop or exit
paths.
Testing
New tests cover: partial reply retained on mid-stream interrupt, empty
reply when interrupted before any chunk, and a tool that raises
KeyboardInterrupt returning the cancelled result.
Documentation and release impact
Review notes
tool calls the model had begun to emit (they are half-formed), but keeps
the text.