fix(voice): scope forwardAudio playback-started listener to its own segment - #1760
Merged
Merged
Conversation
…egment When a speech is interrupted, the scheduling loop immediately authorizes the next speech, so the new segment's forwardAudio registers its playback_started listener on the shared audio output while the interrupted segment is still emitting events during teardown. The stray event resolved the new segment's firstFrameFut before its first frame was captured, skipping resampler creation and pushing an unresampled frame to the AudioSource (RtcError: sample_rate and num_channels don't match) and corrupting playback bookkeeping. Only resolve firstFrameFut after the segment captures its own first frame. Co-authored-by: Cursor <cursoragent@cursor.com>
theomonnom
approved these changes
Jun 11, 2026
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
Fixes an interruption-time crash where the interrupted speech and the new response speech got cross-wired through the shared audio output:
RtcError: InvalidState - sample_rate and num_channels don't matchthrown fromAudioSource.captureFrameduringforwardAudio.playback finished before speech startedandplayback_finished called more times than playback segments were captured.Root cause
When a speech is interrupted, the scheduling loop returns from
waitIfNotInterruptedimmediately and authorizes the next speech while the interrupted segment'spipelineReplyis still tearing down (cancelAndWait→clearBuffer→waitForPlayout) on the sharedaudioOutput.The new segment's
forwardAudioregisters itsplayback_startedlistener on that shared output before reading its first frame. A strayPLAYBACK_STARTEDemitted by the interrupted segment's teardown then resolves the new segment'sfirstFrameFutprematurely. Because resampler creation is gated on!firstFrameFut.done, the resampler is skipped and an unresampled 16 kHz TTS frame is pushed to the 24 kHzAudioSource→RtcError.Fix
Scope the listener to the segment:
forwardAudioonly resolvesfirstFrameFutafter it has captured its own first frame (hasCapturedOwnFrame). Stray events from an interrupted overlapping segment are ignored, so segments no longer cross-wire their first-frame / playback bookkeeping.Test plan
pnpm build:agentspnpm test -- --run agents/src/voice/generation_tts_timeout.test.ts(added 2 regression tests: a strayPLAYBACK_STARTEDbefore the first capture must not resolvefirstFrameFut/ skip resampling)pnpm test -- --run agents/src/voice/generation_tools.test.ts agents/src/voice/agent_activity.test.tsRtcErrorMade with Cursor