Add voice interruption support via cancel() method#248
Merged
Conversation
In voice mode the agent spends most of its time listening or reading a reply back aloud — neither is a "running turn", so Ctrl-C/Escape skipped straight to the double-press quit hint and could not stop the in-flight listen or readback. A single Ctrl-C therefore appeared to do nothing. Add `VoiceSession.cancel()` so an in-flight `listen()`/`speak()` stops within a chunk (the mic gate and a readback-feed sentinel both check it), and in the TUI treat active voice as interruptible: the first Ctrl-C (or Escape) stops the listen/readback and pauses voice so the session goes idle (the text prompt returns, Ctrl-V to talk again); a second Ctrl-C then confirms the quit. Text mode's deepagents-style double-press is unchanged. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Hqg3RHzMaHjG15pkBXP9WB
| _abort_readback() | ||
| player.feed(pcm, sample_rate) | ||
|
|
||
| self.synth_fn(self.api_key, config, on_audio=feed) |
Collaborator
Author
There was a problem hiding this comment.
False positive — not actioning this. tts.session.synthesize is synthesize(api_key, config, *, connect=None, on_warning=None, on_audio=None) (session.py:234-241), so self.synth_fn(self.api_key, config, on_audio=feed) matches its arity exactly. synth_fn is the SynthFn Protocol attribute that defaults to synthesize; CodeQL appears to be misresolving it to a 1-arg target. The suggested self.synth_fn(config) would drop api_key and on_audio, breaking authentication and incremental playback. This call shape (previously on_audio=player.feed) predates this PR and is unchanged in behavior — only the callback was wrapped to honor cancel().
Generated by Claude Code
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.
Summary
Adds the ability to interrupt in-flight voice listening and readback operations via a new
cancel()method onVoiceSession. This enables Ctrl-C and Escape keys to stop active voice activity (listening or text-to-speech playback) promptly, improving responsiveness in voice mode.Key Changes
VoiceSession.cancel(): New method that sets a thread-safe flag to signal interruption. Each
listen()andspeak()call clears the flag on entry, so stale cancellations don't preempt subsequent operations.Listening interruption: The
listen()method now checks the cancel flag between microphone chunks via agated()iterator. When cancelled mid-capture, the stream stops immediately and returnsNoneinstead of waiting for a finalized turn.Readback interruption: The
speak()method wraps the TTS feed callback to check the cancel flag before each audio chunk. When cancelled, it raises an internal_ReadbackInterruptedsentinel that aborts the player (discarding buffered audio) and stops synthesis cleanly.TUI integration:
_stop_voice_activity()method inCodeAgentAppthat cancels active voice, pauses the session, and returns the text prompt.action_interrupt()(Escape) now stops active voice before falling back to turn cancellation.action_quit_or_interrupt()(Ctrl-C) now stops active voice and arms the quit hint for a second press, matching the double-press quit behavior.Voice protocol: Added
cancel()to theVoiceUIprotocol so all voice implementations (real and test) provide the method.Test coverage: Added comprehensive tests for:
Implementation Details
threading.Eventfor thread-safe cancellation signaling between the TUI thread and daemon voice legs._ReadbackInterruptedsentinel subclassesCLIErrorso streaming TTS passes it through unchanged, allowingspeak()to catch and suppress it cleanly.https://claude.ai/code/session_01Hqg3RHzMaHjG15pkBXP9WB