Skip to content

fix(voice): read rootSpanContext live in AudioRecognition#1246

Draft
mrniket wants to merge 2 commits intolivekit:mainfrom
lottiehq-oss:fix/otel-live-rootspancontext
Draft

fix(voice): read rootSpanContext live in AudioRecognition#1246
mrniket wants to merge 2 commits intolivekit:mainfrom
lottiehq-oss:fix/otel-live-rootspancontext

Conversation

@mrniket
Copy link
Copy Markdown
Contributor

@mrniket mrniket commented Apr 14, 2026

Depends on #1244 — review and merge that first. This PR's diff currently
includes #1244's changes; once merged, I will auto-update to show
only the AudioRecognition fix.

Summary

AudioRecognition was snapshotting rootSpanContext at construction time,
so any mutation of session.rootSpanContext after _startSession (e.g. in
an Agent.onEnter hook) was invisible to user_turn spans. This stores a
live reference to the AgentSession instead, and reads rootSpanContext
fresh on each span creation.

Fixes #924

Changes

  • audio_recognition.ts: Replace rootSpanContext snapshot field with a
    live agentSession reference. ensureUserTurnSpan and userTurnContext
    now read this.agentSession?.rootSpanContext on every call.
  • agent_activity.ts: Pass agentSession: this.agentSession to the
    AudioRecognition constructor instead of the snapshotted
    rootSpanContext: this.agentSession.rootSpanContext.

Test plan

  • Existing audio_recognition_span.test.ts (4 tests) passes — it constructs
    AudioRecognition without an agentSession, so the ?. fallback to
    undefined preserves current behavior.
  • pnpm test, pnpm lint, pnpm format:check all pass.
  • Manually verified on LiveKit Cloud with a downstream Langfuse integration.

@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Apr 14, 2026

⚠️ No Changeset found

Latest commit: fed1dae

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

mrniket added 2 commits April 14, 2026 14:24
Honour otelContext.active() when starting the agent_session span and
when building rootSpanContext, so spans the caller created before
session.start() become parents of LiveKit spans.

Add a comment to setupCloudTracer explaining that register() uses
set-once semantics in the OTel API, so it won't clobber a user's
existing context manager or tracer provider.

Fixes livekit#924
AudioRecognition was snapshotting rootSpanContext at construction time,
so any mutation of session.rootSpanContext after _startSession (e.g. in
an Agent.onEnter hook) was invisible to user_turn spans. Store a live
reference to the AgentSession instead, and read rootSpanContext fresh
on each span creation.

Fixes livekit#924
@mrniket mrniket force-pushed the fix/otel-live-rootspancontext branch from f4feb10 to fed1dae Compare April 14, 2026 13:24
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.

feat: Add trace context propagation API for Node.js SDK (parity with Python)

1 participant