Skip to content

fix: snapshot manually committed user turns - #7811

Merged
chenghao-mou merged 8 commits into
mainfrom
chenghao/fix/AGT-3740-guard-manual-turn-clear
Nov 10, 2026
Merged

chenghao-mou merged 8 commits into
mainfrom
chenghao/fix/AGT-3740-guard-manual-turn-clear

Conversation

@chenghao-mou

@chenghao-mou chenghao-mou commented Oct 14, 2026 •

Copy link
Copy Markdown
Member

Manual commits snapshot text and trace data inside the end-of-turn task. Clear resets live input and STT while preserving the snapshot. Later FINALs stay live for the next commit.

await session.commit_user_turn()
session.clear_user_turn()  # Clears live input; preserves the snapshot.

Uses @Hugo-DDT’s #7808 snapshot idea and @ahmetTW’s #7806 race analysis. Canceled tasks preserve spans still owned by live data.

During pending manual delivery, clear preserves realtime latency with external STT.

Fixes #7806. Addresses AGT-3740.

Initial prompt and agent context

Model: GPT-6.1

Can you investigate https://linear.app/livekit/issue/AGT-3740/manual-turn-detection-clear-user-turn-after-commit-user-turn-resolves

take a look at the comments. I think they are valid. We should take a snapshot and commit the snapshot. Clear only affects the live data.

why do we need to keep manualturn_snapshot around? can the snapshot live within the eou task?

ideally we let those functions handle the span selection?

okay, let's commit and push the changes as refactor

Co-authored-by: Ahmet Sakar <56402843+ahmetTW@users.noreply.github.com>
devin-ai-integration[bot]

This comment was marked as resolved.

@ahmetTW

ahmetTW commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Thanks for picking this up so quickly! I tested this branch against the repro from #7806 and it does fix the dropped turn. I think the guard has two side effects, though, one of which the Devin review above also points out.

1. A refused clear also skips the STT reset. _clear_user_turn() does more than blank the transcript. It also rebuilds the STT pipeline (_update_stt(None) / _update_stt(stt)), so a late FINAL from the previous stream can't leak into the next turn. That rebuild is the reason to call it right after a push-to-talk commit. With this PR, a clear issued while delivery is pending returns early, so the rebuild never happens. The public AgentSession.clear_user_turn() still returns None, so the caller can't tell its clear was dropped.

2. Speech the app wanted discarded is merged into the committed turn. While a manual commit is pending, _on_stt_event still accepts FINALs, so the guard can't tell a late FINAL of the committed turn from new speech the app is trying to clear. I confirmed the Devin scenario: commit "weather", then "never mind" arrives during the endpointing delay and the app calls clear_user_turn(). The turn is delivered as "weather never mind" in both orders (clear before or after the new FINAL).

I applied this PR and #7808 to 1.8.7 (both apply cleanly). Results, min_delay=0.5:

Scenario 1.8.7 #7808 #7811
clear right after the commit future resolves delivered "" delivered ✅, STT rebuilt ✅ delivered ✅, STT not rebuilt
clear, then new FINAL "never mind" during the delay turn 1 = "never mind" turn 1 = "weather" ✅, "never mind" carried to the next turn ✅ turn 1 = "weather never mind"
new FINAL "never mind" during the delay, then clear turn 1 = "" turn 1 = "" turn 1 = "weather never mind"
clear issued after on_end_of_turn has run ✅ ✅ ✅

The check for (1), on top of the #7806 repro:

await rec._commit_user_turn(audio_detached=True, transcript_timeout=1.0, stt_flush_duration=2.0)
before = rec._stt_pipeline
rec._clear_user_turn()
assert rec._stt_pipeline is not before  # fails on this branch while delivery is pending

#7808 has a gap too (last-but-one row). A FINAL arriving during the delay calls _run_eou_detection(trigger="stt"), which replaces the manual task; the replacement carries no snapshot, so a clear after it still blanks the committed turn. That FINAL may equally be a genuine late FINAL of the committed turn. So the snapshot needs to survive the reschedule: while _user_turn_committed is set, a replacement task should inherit the manual commit's snapshot (committed text plus any finals appended before the clear), rather than only applying it when trigger == "manual".

Suggested resolution: land one fix, not both. With this guard in place, #7808's cleared-after-commit branch could never trigger, so the combination behaves like this PR alone. My preference is the snapshot approach, as the Devin review also recommends ("track which utterance each pending commit protects"), extended to cover the reschedule above, either by reworking this PR along those lines or by going with #7808 once its CLA is signed and the gap is closed. Happy to test whichever branch you pick; the four rows above make a reasonable regression set.

Co-authored-by: DDT <141296842+Hugo-DDT@users.noreply.github.com>
Co-authored-by: Ahmet Sakar <56402843+ahmetTW@users.noreply.github.com>
@chenghao-mou chenghao-mou added review_effort:high Needs broad context and substantial validation and removed review_effort:low labels Oct 14, 2026
@chenghao-mou chenghao-mou changed the title fix: guard clear_user_turn during manual commits fix: snapshot manually committed user turns Oct 14, 2026
devin-ai-integration[bot]

This comment was marked as resolved.

@ahmetTW

ahmetTW commented Oct 14, 2026

Copy link
Copy Markdown
Contributor

Tested f8febb5 applied to 1.8.7 against the scenarios from my earlier comment: all four rows now pass, including the "new FINAL during the delay, then clear" case (turn delivered as "weather", STT rebuilt). A downstream push-to-talk suite that drives commit_user_turn() → clear_user_turn() also passes, except the test that asserts the old behaviour. Thanks for turning this around so quickly!

devin-ai-integration[bot]

This comment was marked as resolved.

@bml1g12

bml1g12 commented Oct 16, 2026

Copy link
Copy Markdown
Contributor

Maybe related work to #7585 and #7702

@kcncb

kcncb commented Oct 27, 2026

Copy link
Copy Markdown

Not trying to talk you out of this one — #7811 is the approved fix here — but two things from putting #8160 together that may help it land:

  1. It currently reports CONFLICTING against main (approved on 2026-10-19), so it needs a rebase before it can merge.
  2. Worth re-checking the _clear_user_turn path: on main it now cancels the pending end-of-turn task (self._end_of_turn_task.cancel()) before blanking _audio_transcript. If the snapshot is taken before that cancellation, the guard has to survive the cancel as well as the clear — my branch ends up passing the committed transcript into _run_eou_detection rather than snapshotting it, which sidesteps the ordering entirely.

I'm closing #8160 as the duplicate it is so it doesn't split review attention; the test file (tests/test_manual_commit_transcript_survives_clear.py) covers the "clear during the delay" case end-to-end if you want to borrow it.

@ahmetTW

ahmetTW commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

Hello

what’s next on this?

thank you

@Hugo-DDT

Hugo-DDT commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

@chenghao-mou @longcw — #7811 is already APPROVED, but GitHub currently shows it as CONFLICTING against main (last commit still from Oct 14).

Could you rebase this branch onto current main and re-check the _clear_user_turn path? On main that path now cancels the pending end-of-turn task before blanking the transcript, so the manual-commit snapshot needs to survive both the clear and that cancel (as noted in the Oct 27 comment). Happy to help re-test once the conflicts are resolved.

Thanks — and thanks @ahmetTW for the nudge.

@bml1g12

bml1g12 commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

@longcw @chenghao-mou much appreciated if you can take a look 🙇

…-guard-manual-turn-clear

# Conflicts:
#	livekit-agents/livekit/agents/voice/audio_recognition.py
@chenghao-mou
chenghao-mou merged commit a77605e into main Nov 10, 2026
21 of 23 checks passed
@chenghao-mou
chenghao-mou deleted the chenghao/fix/AGT-3740-guard-manual-turn-clear branch November 10, 2026 13:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

review_effort:high Needs broad context and substantial validation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Manual turn detection: clear_user_turn() after commit_user_turn() resolves discards the committed turn

6 participants