fix(voice): block new user turns immediately on update_agent() to prevent transition delay#5396
Open
Panmax wants to merge 3 commits intolivekit:mainfrom
Open
fix(voice): block new user turns immediately on update_agent() to prevent transition delay#5396Panmax wants to merge 3 commits intolivekit:mainfrom
Panmax wants to merge 3 commits intolivekit:mainfrom
Conversation
Previously, update_agent() only created an async transition task, leaving the old activity's scheduling task free to accept new user turns until drain() eventually called _pause_scheduling_task(). This caused the old agent to process stray voice input (including noise) during the transition window, delaying the new agent's on_enter() by seconds. Synchronously set _scheduling_paused = True and wake the scheduling task before creating the transition task, closing the race window. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
The previous approach directly set `_scheduling_paused = True` in update_agent(), which caused _pause_scheduling_task() in drain() to early-return and skip awaiting the scheduling task. This meant in-flight speech tasks were not properly waited on before the transition proceeded. Introduce `_new_turns_blocked` flag that only prevents new user turns from being accepted, without interfering with _pause_scheduling_task()'s lifecycle. drain() can now properly set _scheduling_paused and await the scheduling task to ensure all in-flight speech completes. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
longcw
reviewed
Apr 10, 2026
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
longcw
approved these changes
Apr 10, 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
Bug: When
update_agent()is called, the old agent can still accept and process new user turns (including ambient noise) during the transition window, delaying the new agent'son_enter()by seconds.Root cause: The async transition task (
drain()→_pause_scheduling_task()) doesn't run immediately, leaving a window where the old activity's scheduling task accepts new turns and triggers full LLM → TTS pipeline.Fix: Introduce a lightweight
_new_turns_blockedflag that is set synchronously inupdate_agent()to immediately block new user turns, while keeping_scheduling_pauseduntouched for_pause_scheduling_task()to function correctly.This avoids the issue where directly setting
_scheduling_paused = Truewould cause_pause_scheduling_task()to early-return and skipawait asyncio.shield(self._scheduling_atask), which is needed to wait for in-flight speech tasks to complete.Changes
agent_activity.py: Add_new_turns_blockedflag, checked at all user-turn entry points (_on_generation_created,on_preemptive_generation,on_end_of_turn,_on_user_turn_completed,_on_user_input_complete)agent_session.py:update_agent()sets_new_turns_blocked = Trueinstead of_scheduling_paused = TrueWhat is NOT changed (intentionally)
_pause_scheduling_task()early-return guard — must only check_scheduling_paused_schedule_speech(force=True)— tool responses during drain still work_scheduling_taskexit condition — only exits on formal pause signalsay()/generate_reply()routing — programmatic speech still routes to current activity during transitionTest plan
session.update_agent(new_agent)while there is ambient noise — new agent'son_enter()should fire promptly without the old agent processing a stray turndrain()properly awaits in-flight speech tasks before completing the transitionupdate_agent()complete without interruption🤖 Generated with Claude Code