Skip to content

fix(amd): settle uncertain when the callee hangs up while listening - #8242

Merged
chenghao-mou merged 6 commits into
livekit:mainfrom
sarankumar1325:fix/amd-settle-on-hangup
Nov 12, 2026
Merged

chenghao-mou merged 6 commits into
livekit:mainfrom
sarankumar1325:fix/amd-settle-on-hangup

Conversation

@sarankumar1325

Copy link
Copy Markdown
Contributor

Fixes #8241

Once AMD has started listening, a SIP callee who hangs up before a verdict leaves the classifier pending. The participant disconnect then tears the session down, aclose() closes the classifier, and execute() raises RuntimeError("amd closed before a result was available"). Pre-listen hangups already settle uncertain with reason="participant_missing". This one didn't.

Change

  • After _setup resolves the listened participant, AMD subscribes to the room's participant_disconnected. The handler is removed in aclose().
  • When that participant disconnects while a verdict is still pending, AMD settles uncertain / participant_missing and calls on_end_of_turn() on the classifier. With wait_until_finished and speech already heard, a settled verdict is otherwise held until end of turn, so that call releases it right away. If the classifier already holds a verdict that is waiting on the end-of-turn gate, that verdict is the one released.
  • The handler runs synchronously inside the room event. RoomIO closes the session from a task, so the verdict is set before teardown reaches aclose(), and execute() returns it.

Tests

  • New test test_execute_settles_when_participant_hangs_up_while_listening: listening starts, speech starts, the participant disconnects, then aclose() runs. Without the fix it fails with RuntimeError: amd closed before a result was available. With the fix, execute() returns uncertain / participant_missing.
  • The existing setup test's fake room now has on/off.
  • pytest tests/test_amd_detector.py tests/test_amd_classifier.py passes (44 tests). ruff format --check, ruff check and mypy --strict on voice/amd are clean.

AI disclosure: I wrote this fix with help from an AI assistant (Grok Bot). I reviewed the change and ran the tests above myself.

Once AMD started listening, a hangup before a verdict left the classifier
pending until session teardown closed it, so execute() raised
"amd closed before a result was available". AMD now watches the listened
participant and settles uncertain (reason=participant_missing) when it
disconnects, releasing the verdict right away.

Fixes livekit#8241

Assisted-by: Grok Bot
@sarankumar1325
sarankumar1325 requested a review from a team as a code owner October 29, 2026 08:31
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review 🔄 Running since 2026-10-29T08:32:02.319599Z 0b9bf92 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

devin-ai-integration[bot]

This comment was marked as resolved.

@kucharzyk-sebastian

Copy link
Copy Markdown

I really appreciate your effort in solving the issue. It feels like this PR is close to merging. Do you think we could merge it? 🙏🏼

chenghao-mou and others added 4 commits November 12, 2026 11:14
A hangup released any verdict the classifier already held, so execute()
interrupted the session and started IVR detection for a callee who had
left, and callers tried to leave a voicemail on a closing session.

A hangup while listening now settles uncertain with
reason=participant_disconnected and replaces a held human, machine-vm or
machine-ivr verdict. A held machine-unavailable verdict is still
returned: carriers hang up after that announcement, so the hangup
confirms it (case reported by kannas14 on livekit#8241). A hangup
before listening starts keeps reason=participant_missing.

Co-authored-by: kannas14 <[email protected]>
Co-Authored-By: Claude Fable 5.2 <[email protected]>
main folded tests/test_amd_detector.py into tests/test_amd.py
(livekit#8510). The hangup tests and their fake room move there.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
`_disconnect_room` read as a verb and `_listened_identity` did not say how
it differs from the configured `_participant_identity`. They are now
`_room`, as in the other room-holding classes, and `_publisher_identity`,
after the `publisher` that `_setup` resolves.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
A hangup before the SIP answer reached both the room handler and the
SIP-answer wait, so the "call ended before detection could run" line was
logged twice. _settle_participant_missing now returns when a result
already exists. The handler branch stays: when teardown follows the
hangup immediately, it cancels the SIP-answer wait before that wait can
settle.

Tests now cover a held uncertain verdict, a hangup after an emitted
verdict, and the pre-answer hangup against the real SIP-answer wait.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
@chenghao-mou
chenghao-mou merged commit 35df6b6 into livekit:main Nov 12, 2026
15 checks passed
chenghao-mou added a commit that referenced this pull request Nov 13, 2026
main folded per-PR test files (#8510) and fixed callee hangups in the old
AMD classifier (#8242). This branch replaces that classifier, and its
detector already completes with reason participant_disconnected, so the
old classifier.py and main's tests/test_amd.py are dropped. The branch's
edits to the folded test files move to tests/test_audio_recognition.py
and tests/test_agent_interruptions.py.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AMD.execute() raises "amd closed before a result was available" when the SIP callee hangs up after answering

3 participants