agents icon indicating copy to clipboard operation
agents copied to clipboard

Realtime: a context copied while a text turn's push is in flight omits the user message, and the next update deletes it from the server

Open Rehansanjay opened this issue 1 month ago • 2 comments

Bug Description

With a realtime model, a text user turn adds its user message to the agent's own chat context only after its push to the server returns. While that push is in flight, agent.chat_ctx does not contain the message, so anything that copies the context in that window produces a context that is missing an item the server is about to acknowledge — and the next diff deletes it from the server conversation.

_realtime_reply_task, livekit-agents 1.8.8, voice/agent_activity.py:

msg = chat_ctx.add_message(role="user", content=user_input)   # on a copy of the mirror
try:
    await self._rt_session.update_chat_ctx(chat_ctx)          # :4164  the push
except ...
self._agent._chat_ctx._upsert_item(msg)                       # :4198  only now is it local
self._session._conversation_item_added(msg)

The interleaving:

  1. The text turn pushes hello and waits for the server to acknowledge it.
  2. Application code calls session.update_chat_ctx(agent.chat_ctx.copy()). That copy has no hello in it, because of the ordering above. AgentActivity.update_chat_ctx assigns self._agent._chat_ctx = chat_ctx immediately and then calls RealtimeSession.update_chat_ctx, which blocks on the plugin's _update_chat_ctx_lock.
  3. The text turn's ack lands, the mirror now contains hello, and the turn upserts it locally.
  4. The replacement acquires the plugin lock and diffs its own context object — still without hello — against a mirror that has it. hello appears in to_remove, so a conversation.item.delete goes out for it.

The local context keeps the message and the server conversation loses it, so the reply is generated without the user's turn.

Both tool-output paths already do this the other way round — self._agent._chat_ctx.insert(interrupted_fnc_outputs) at :4759 and the two _upsert_item calls at :4807-4808 run before their pushes, so a context copied mid-push contains those items and no diff can delete them. The text turn at :4198 is the only push that updates the local context after the fact.

This is not the same race as #7880 (overlapping pushes diffing stale snapshots of the server mirror, fixed by serializing snapshot and push in #7904 / #7905). Serializing does not help here: the replacement's context object is captured before it blocks, and no ordering of the pushes refreshes it.

Expected Behavior

A context copied from agent.chat_ctx while a text turn's push is in flight contains that user message, so a replacement push cannot delete an item the server is about to acknowledge. The agent's context and the server conversation stay in agreement.

Reproduction Steps

# Needs a provider whose update_chat_ctx diffs against an acknowledged mirror
# (the OpenAI realtime plugin) and a non-instant ack, so there is a window:
1. AgentSession with openai.realtime.RealtimeModel and a realtime provider that acks after ~50 ms.
2. session.generate_reply(user_input="hello")  — the push at agent_activity.py:4164 is now in flight.
3. Before it returns, from another task: await session.update_chat_ctx(session.current_agent.chat_ctx.copy())
4. Watch the client events: after the conversation.item.create for the user message is acked,
   the replacement sends conversation.item.delete for that same item.
5. The reply is generated from a server conversation with no user turn.
# With an instant ack there is no window and step 4 does not happen.

Possible fix

Hoisting self._agent._chat_ctx._upsert_item(msg) above the push makes the text turn consistent with the two tool-output paths, and is one line. It is not free, so it is worth deciding deliberately rather than just moving it:

  • _conversation_item_added is emitted from the same place, so hoisting changes when that event fires relative to the push.
  • On the unexpected-exception path the turn currently returns without recording the message at all (speech_handle._mark_done(error=e); return). Hoisting would leave it in the local context for a turn that failed, unless it is removed there.

Happy to send a PR once there is a view on which of those two behaviours should win.

Operating System

Linux and Windows (read from the source; the window depends only on ack latency)

Models Used

OpenAI Realtime via openai.realtime.RealtimeModel; any realtime provider that diffs against an acknowledged server mirror

Package Versions

livekit-agents==1.8.8
# the ordering at agent_activity.py:4198 is the same on main at ceb3d598f

Session/Room/Call IDs

None: found while fixing #7880.

Rehansanjay avatar Oct 17 '26 16:10 Rehansanjay

AgentActivity.update_chat_ctx assigns its copy to self._agent._chat_ctx before it waits on the lock. So the text turn's _upsert_item(msg) writes into the object that the replacement pushes next. Nothing yields between the lock release and that upsert, and the plugin copies the context only after it gets the lock. The replacement's diff therefore contains hello, and no delete goes out.

If you can reproduce a lost message with a copy that is applied immediately, please reopen with the trace.

longcw avatar Oct 20 '26 11:10 longcw

Checked it against main and you're right — update_chat_ctx assigns the copy at agent_activity.py:798-799, before the lock at :803, so the text turn's _upsert_item writes into the very object the replacement pushes, and the plugin copies only after acquiring the lock. No trace to offer; the race does not exist once #7905 is in.

I should have got here without filing this. @Epsilon006 told me exactly this on #7905 two days ago — "the completing text task updates the local context object shared with the replacement before the provider copies it" — and I filed the issue anyway on the grounds that it was a separate question about when the message becomes locally visible. It wasn't separate; it was the same thing he had already ruled out. #7910 is closed and nothing in it is worth keeping.

One thing I'll leave as an observation rather than an argument, since this is closed: the safety here rests on two implicit orderings — the assignment preceding the lock, and nothing yielding between the push returning and the upsert. Both hold today. If either moves later the deletion comes back, and it would be silent. A line of comment at :799 saying the assignment must stay ahead of the lock might save someone the trip. Entirely your call, and not worth reopening for.

Rehansanjay avatar Oct 20 '26 12:10 Rehansanjay