-
Notifications
You must be signed in to change notification settings - Fork 3.6k
fix(voice): preserve turns across activity handoffs #6844
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
IanCollection
wants to merge
2
commits into
livekit:main
Choose a base branch
from
IanCollection:agent/preserve-agent-task-handoff-turn
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+773
−25
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔴 After an agent handoff, the new agent answers the carried-over question using the previous agent's persona and history
The carried-over question is answered using the previous agent's conversation snapshot (
chat_ctx=user_turn.chat_ctxatlivekit-agents/livekit/agents/voice/agent_activity.py:2627) instead of the new agent's own, so the new agent replies with the old agent's personality and past conversation.Impact: The first reply after a handoff sounds like the old agent (wrong system prompt, wrong history), which can produce off-topic or incorrect answers and defeats the purpose of switching agents.
Mechanism: the source activity's `temp_mutable_chat_ctx` is replayed on the successor activity
In
_user_turn_completed_taskthe source activity buildstemp_mutable_chat_ctx = self._agent.chat_ctx.copy()(livekit-agents/livekit/agents/voice/agent_activity.py:2556). That copy contains the source agent's history plus the system message injected byupdate_instructions(self._agent._chat_ctx, instructions=self._agent.instructions, add_if_missing=True)in_start_session(livekit-agents/livekit/agents/voice/agent_activity.py:1065-1069).The copy is stored on the
_PreparedUserTurn(agent_activity.py:2573-2578), forwarded to the successor activity, and finally passed to_schedule_reply_after_user_turn(chat_ctx=user_turn.chat_ctx, ...)from_prepared_user_turn_completed_task._schedule_reply_after_user_turnhands it straight to_generate_reply(chat_ctx=chat_ctx, ...), which uses it verbatim as the LLM input (chat_ctx or self._agent._chat_ctxatagent_activity.py:1625)._pipeline_reply_task_implonly rewrites instructions whenself._agent.instructionsis anInstructionsobject (agent_activity.py:3223-3229); for the commonstrcase nothing removes the source agent's system message, so the successor's LLM call runs with the old instructions while receiving the new agent's tools (all_tools = self.tools.copy()atagent_activity.py:1576). TheAgentHandoffmarker inserted by_update_activity(livekit-agents/livekit/agents/voice/agent_session.py:1767) is also absent from that snapshot.Note the non-prepared (
_EndOfTurnInfo) forwarding path is unaffected: the successor re-runs_user_turn_completed_task, which rebuildstemp_mutable_chat_ctxfrom its own agent.tests/test_agent_task_handoff_turn.py:547-553actually asserts the source snapshot reaches the successor'sllm_node, so this leak is currently locked in by the tests.Prompt for agents
Was this helpful? React with 👍 or 👎 to provide feedback.