fix(voice): skip end-of-turn metrics on stale/out-of-order speaking anchor#6098
Open
anshulkulhari7 wants to merge 2 commits into
Open
fix(voice): skip end-of-turn metrics on stale/out-of-order speaking anchor#6098anshulkulhari7 wants to merge 2 commits into
anshulkulhari7 wants to merge 2 commits into
Conversation
…nchor When the turn detector commits a user turn whose _last_speaking_time anchor was never refreshed for that segment (e.g. consecutive same-role turns split from one continuous utterance), the anchor can be left over from an earlier point in the session and predate the start of the current turn. The metric computation only guarded against None values, so it still produced transcription_delay / end_of_turn_delay on the order of hundreds of seconds and a stopped_speaking_at that precedes started_speaking_at. Treat an out-of-order anchor (last_speaking_time < speech_start_time) the same as unreliable VAD timing: skip the calculation and report the metrics as None rather than emitting a likely-wrong value. Extract the computation into a pure _compute_end_of_turn_metrics helper and add unit tests covering the normal, boundary, stale-anchor, and missing-anchor cases. Fixes livekit#6093
chenghao-mou
left a comment
Member
There was a problem hiding this comment.
Thanks for the PR! I have one small comment.
|
|
||
|
|
||
| @dataclass | ||
| class _EndOfTurnMetrics: |
Member
There was a problem hiding this comment.
since _EndOfTurnInfo is internal, I would go one step further to replace the four variables in _EndOfTurnInfo with this directly so we don't have to unpack or pass around those values individually.
Author
There was a problem hiding this comment.
Done — folded the four metric fields on _EndOfTurnInfo into a single metrics: _EndOfTurnMetrics. The computed object is now passed straight through; _user_turn_completed_task, _init_metrics_from_end_of_turn, and the turn span read info.metrics.*. mypy strict (593 files) and the unit tests pass.
… duplicated fields Per review on livekit#6098: _EndOfTurnInfo (internal) carried the same four metric fields as _EndOfTurnMetrics. Replace them with a single metrics field so the computed value is passed through directly instead of unpacked and repacked. Readers (_user_turn_completed_task, _init_metrics_from_end_of_turn) and the turn span now read info.metrics.*.
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 #6093.
For some user turns, the reported
transcription_delay/end_of_turn_delaymetrics onChatMessageare extremely large (often >200s) even though the session recordings show no real delay, andstopped_speaking_atcan precedestarted_speaking_at. In other turns the fields are missing entirely.Root cause
The metrics are computed in
_bounce_eou_task(audio_recognition.py) from three captured anchors:The guard around this block only checked that the three values are not
None. When the turn detector commits a user turn whose_last_speaking_timewas never refreshed for that segment — e.g. consecutive same-role turns split from one continuous utterance, with no VAD speech-stop/start cycle between them — the anchor is left over from an earlier point in the session and can predate the start of the current turn.In that case the not-
Noneguard still passes, soend_of_turn_delay = now - last_speaking_timebecomes ~200s andstopped_speaking_atends up beforestarted_speaking_at, exactly the payload reported in the issue.This is the same class of bug noted in #2361 / #5669 / #4388 (stale/
0anchor), now manifesting as an out-of-order anchor on adjacent turns within one long utterance.Fix
An anchor that predates the start of the turn (
last_speaking_time < speech_start_time) is logically impossible — you cannot stop speaking before the turn started. The existing code already has a policy for unreliable timing (see the in-code comment): skip the calculation and report the metrics asNone, because that is better than emitting a likely-wrong value. This change extends that same policy to the out-of-order case.The computation is extracted into a small pure helper,
_compute_end_of_turn_metrics, which:Nonefor all four metrics when any anchor is missing or whenlast_speaking_time < speech_start_time(stale/out-of-order), andend_of_turn_delaynow clamped to>= 0, consistent with the existingtranscription_delayclamp).This makes the behaviour directly unit-testable without audio/STT/VAD.
Testing
New unit test module
tests/test_end_of_turn_metrics.pyexercises the pure helper with crafted timestamps (no audio):test_normal_turn_produces_small_bounded_delays— well-ordered turn yields the expected sub-second delays.test_stale_anchor_predating_turn_start_is_skipped— regression for this issue, using the exact~220snumbers from the reported payload; all four metrics must beNone.test_anchor_equal_to_start_is_accepted— boundary (last_speaking_time == speech_start_time) stays valid.test_missing_anchor_is_skipped— any missing anchor skips the calculation.Confirmed RED before the fix (reverting the ordering guard):
test_stale_anchor_predating_turn_start_is_skippedfailed withstarted_speaking_at=1781342804.815377,end_of_turn_delay=220.28458189964294— i.e. the bogus >200s value. The existingtests/test_speech_start_time_persistence.pystill passes.ruff check,ruff format --check, andmypyare clean on the changed files.AI disclosure
This change was AI-assisted; all logic, tests, and verification were reviewed by the author.