fix: early return after agent handoff in realtime pipeline#1035
fix: early return after agent handoff in realtime pipeline#1035randombet wants to merge 1 commit intolivekit:mainfrom
Conversation
After updateAgent() triggers activity transition in the realtime tool output handler, the old code continued using this.realtimeSession for chat context updates and reply generation. Since updateAgent() calls drain() and close() on the old activity (including its realtime session), this caused errors and broken agent transfers. Added early return to skip post-handoff processing in the realtime pipeline, matching the equivalent fix in the Python agents SDK. The STT-LLM-TTS pipeline (line 1691) is unaffected - it uses a local chatCtx variable and does not reference this.realtimeSession.
|
📝 WalkthroughWalkthroughWhen a tool execution produces a new agent task, the code now sets a draining flag and returns early, preventing further updates to the chat context and reply creation. This defers subsequent processing to the new agent instead of the current realtime session. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
📜 Recent review detailsConfiguration used: Organization UI Review profile: CHILL Plan: Pro 📒 Files selected for processing (1)
🧰 Additional context used📓 Path-based instructions (3)**/*.{ts,tsx,js,jsx}📄 CodeRabbit inference engine (.cursor/rules/agent-core.mdc)
Files:
**/*.{ts,tsx}?(test|example|spec)📄 CodeRabbit inference engine (.cursor/rules/agent-core.mdc)
Files:
**/*.{ts,tsx}?(test|example)📄 CodeRabbit inference engine (.cursor/rules/agent-core.mdc)
Files:
🔇 Additional comments (1)
✏️ Tip: You can disable this entire section by setting Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Hey @randombet, thanks for the bug fix! Have you tested with above items? |
|
@toubatbrian Please check #1053 for the example to reproduce. |
|
Just curious, what are the exact error message you got. I can't seems to reproduce the error behavior consistently |
@toubatbrian I don't get the error message. I can handoff from main assistant to math expert, but can't handoff back to the main assistant from math expert. Transfer just hangs there |
Summary
updateAgent()triggers activity transition in the realtime tool output handler, the old code continued usingthis.realtimeSessionfor chat context updates and reply generation. SinceupdateAgent()callsdrain()andclose()on the old activity (including its realtime session), this caused errors and broken agent transfers (e.g. transferring back from a specialist agent to the main agent would fail silently).returnto skip post-handoff processing in the realtime pipeline, matching the equivalent fix in the Python agents SDK (fix: realtime agent handoff race conditions agents#4746).Context
The bug manifests when using multi-agent handoff with Gemini Realtime models:
llm.handoff({ agent }), triggeringupdateAgent()updateAgent()startsupdateActivity()which callsdrain()andclose()on the oldAgentActivitythis.realtimeSession.updateChatCtx()on the closing sessionThe STT-LLM-TTS pipeline (
agent_activity.ts:1691) is not affected — it uses a localchatCtxvariable and creates a newpipelineReplyTask, never touchingthis.realtimeSession.Test plan
Summary by CodeRabbit