Skip to content

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

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

fix(voice): read rootSpanContext live in AudioRecognition#1246
mrniket wants to merge 4 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: c4333ce

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 mrniket force-pushed the fix/otel-live-rootspancontext branch 3 times, most recently from 53f7c12 to a9f8057 Compare April 20, 2026 09:55
mrniket added 4 commits April 20, 2026 10:56
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. Stop calling
tracerProvider.register() in setupCloudTracer to avoid replacing the
global AsyncLocalStorageContextManager.

Fixes livekit#924
register() uses set-once semantics in the OTel API — if a user already
called NodeSDK.start(), both the context manager and global provider
registrations are safe no-ops. Removing it broke cloud-only users who
have no pre-existing OTel setup.
…ng provider

Adds contract tests confirming that setupCloudTracer's tracerProvider.register()
call is a safe no-op when a user has already registered their own OTel provider
via NodeSDK.start(): the user's global context manager and provider are
preserved, user spans continue exporting to the user's exporter, and LiveKit
spans correctly nest as children of user parent spans via the shared context
manager.
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 a9f8057 to c4333ce Compare April 20, 2026 09:57
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