feat(voice): add AgentSession.claim_user_turn#5806
Open
longcw wants to merge 2 commits into
Open
Conversation
A public async context manager for declaring a programmatic user-driven
turn. While active, `wait_for_inactive` is held open and `user_state` is
pinned to "speaking". On release, `user_state` is re-derived from the
audio path's `_user_silence_event` ("speaking" if voice still considers
the user to be speaking, else "listening").
Fixes a race in the default `text_input_cb` (and any custom callback
doing `interrupt + generate_reply`): a deferred async-tool reply could
resolve `wait_for_inactive` in the gap between `interrupt()` and
`generate_reply()` and fire concurrently with the new turn, producing a
duplicate response. The default callback now uses `claim_user_turn`.
Reentrant via counter; held at the session level so it survives agent
handoff. Voice transitions during the claim are suppressed and
recoverable from `_user_silence_event` on release.
13420b4 to
2dd04ad
Compare
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
AgentSession.claim_user_turn(), a public async context manager that declares a programmatic user-driven turn. While active:wait_for_inactiveis held open via a session-level counter (_user_turn_claims) and event (_user_turn_released).user_stateis pinned to"speaking"— non-"speaking" updates from the audio path are dropped in_update_user_state.On release,
user_stateis re-derived from the audio path's_user_silence_event:"speaking"if voice still considers the user to be speaking, else"listening". Reentrant; held at the session level so it survives agent handoff.Why
The default
text_input_cb(and any custom callback that doesinterrupt + generate_reply) had a race: a deferred async-tool reply waiting onwait_for_inactivecould resolve in the gap betweenawait sess.interrupt()andsess.generate_reply(...), firing concurrently with the new turn and producing a duplicate response. The default callback now wraps the pair inclaim_user_turnto close the window.Behavior notes
AgentActivity._wait_for_inactive's loop body, so a claim acquired during the body's awaits (e.g. while waiting for ongoing speech) is still honored._user_silence_eventwhen the claim ends.