fix(warm-transfer): capture job context for post-merge caller-room cleanup#1898
Open
toubatbrian wants to merge 1 commit into
Open
fix(warm-transfer): capture job context for post-merge caller-room cleanup#1898toubatbrian wants to merge 1 commit into
toubatbrian wants to merge 1 commit into
Conversation
🦋 Changeset detectedLatest commit: a1cce94 The changes in this PR will be included in the next version bump. This PR includes changesets to release 35 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
WarmTransferTask's post-merge ParticipantDisconnected handler called getJobContext() from an rtc-node FFI callback whose AsyncLocalStorage context is not the job's, so it threw and left the 2-party SIP room undeleted. Capture the JobContext eagerly in onEnter() and use jobCtx.deleteRoom() in the late handler. Also refactor the task through AgentTask.create, move waitUntilAborted into utils.ts, align human-agent naming with Python, and keep the example on explicit dispatch to avoid auto-dispatching a second agent into the human-agent room. Co-authored-by: Cursor <cursoragent@cursor.com>
e19da67 to
a1cce94
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
Fixes #1895:
WarmTransferTask's post-merge cleanup throws an unhandled rejection and leaves the 2-party SIP room undeleted.After
mergeCalls(), the task registers aRoomEvent.ParticipantDisconnectedlistener on the caller room to delete that room once a party hangs up. The handler calledgetJobContext(), but it runs from a native@livekit/rtc-nodeFFI callback whoseAsyncLocalStoragecontext is pinned toFfiClient-singleton creation — not the job's context. So by the time the late event fires (the agent has already left, the session is closed),getJobContext()reads an empty/stale store and throws. The caller room is never deleted.The fix
JobContexteagerly inonEnter()(while the live context is still available) and usejobCtx.deleteRoom()in the late handler. This avoids the deadgetJobContext()lookup and uses the job's own API credentials instead of relying on process env vars.Also in this PR
AgentTask.create.WarmTransferTaskis rebuilt with the functionalcreateWarmTransferTaskfactory (now exported) for a more JS-native shape, keeping parity with the Pythonwarm_transfer.pybehavior without re-introducing the context bug.WarmTransferResult.humanAgentIdentity, participant identityhuman-agent-sip, and room suffix-human-agent.waitUntilAbortedmoved toutils.ts(out ofgeneration.ts) so the task can reuse it, plus a pre-aborted short-circuit that swallows the abandoned promise's late rejection.examples/src/warm_transfer.tspinsagentNameto force explicit dispatch. Without an explicit name the worker auto-dispatches into every new room — including the human agent room the task creates — putting a second agent on the line with the human agent and producing overlapping voices.Test plan
transfer_to_human→ human agent dialed via SIP →connect_to_caller→mergeCalls().getJobContext()rejection appears in worker logs.pnpm build:agents.