fix(voice): skip stale end-of-turn metrics#1803
Merged
Merged
Conversation
|
chenghao-mou
approved these changes
Jun 16, 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
Testing
Notes
pnpm --filter @livekit/agents api:checkis blocked by an existing API Extractor parser error onexport * as ___inagents/dist/index.d.ts.Ported from livekit/agents#6098
Original PR description
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.