Skip to content

fix(voice): preserve e2e latency across agent handoffs - #7161

Closed
mikemikimike wants to merge 1 commit into
livekit:mainfrom
mikemikimike:codex/issue-7157-preserve-e2e-latency
Closed

fix(voice): preserve e2e latency across agent handoffs#7161
mikemikimike wants to merge 1 commit into
livekit:mainfrom
mikemikimike:codex/issue-7157-preserve-e2e-latency

Conversation

@mikemikimike

Copy link
Copy Markdown
Contributor

Summary\n\nPreserve the originating user's speech metrics when a function tool triggers an agent handoff. The next agent's first generate_reply() can now report �2e_latency on its assistant message, span, and OpenTelemetry histogram.\n\nThe propagation uses a task-local ContextVar, so direct AgentSession.update_agent() handoffs and returned handoff tasks work without sharing mutable per-turn state.\n\n## Tests\n\n- ests/test_update_agent_long_on_enter.py — added a synthetic audio regression test for a handoff reply\n- 94 passed across ests/test_agent_session.py, ests/test_tool_output_per_call.py, and ests/test_update_agent_long_on_enter.py\n-

uff check and compileall pass\n\nCloses #7157.

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Devin Review found 1 potential issue.

1 flag not posted on this PR by your GitHub settings — view it in Devin Review. (Configure)

Devin Review

Comment on lines +1630 to +1631
handoff_user_metrics = self._session._handoff_user_metrics
self._session._handoff_user_metrics = None

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Handoff latency reaches the wrong reply

_generate_reply clears session-wide handoff metrics before validating the request or confirming the target activity. A failed, realtime, or concurrent reply can consume them first. The handoff reply then loses end-to-end latency, while an unrelated reply can inherit stale data.

Prompt for agents
The handoff metrics are stored on AgentSession and consumed by whichever AgentActivity._generate_reply call happens next. This is not scoped to the activity created by the handoff, and consumption occurs before tool validation and before selecting the pipeline or realtime path. Associate the metrics with the specific target activity or handoff generation instead of a session-wide next-call slot. Consume them only after a valid pipeline reply has been created, and define cleanup for failed or abandoned handoffs so stale metrics cannot reach later replies.
Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.

@longcw

longcw commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

closing in favor of #7167

@longcw longcw closed this Sep 9, 2026
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.

2 participants