fix: prevent duplicate LLM spans when multiple SDK instances coexist (BT-5139)#1973
Open
Stephen Belanger (Qard) wants to merge 3 commits intomainfrom
Open
fix: prevent duplicate LLM spans when multiple SDK instances coexist (BT-5139)#1973Stephen Belanger (Qard) wants to merge 3 commits intomainfrom
Stephen Belanger (Qard) wants to merge 3 commits intomainfrom
Conversation
…ces coexist
When the Braintrust SDK is loaded from two different module paths in the
same process (e.g. a cloud runner's bundled copy + the user's scorer
dependency), each PluginRegistry instance independently subscribed to the
same process-global diagnostics_channel, causing every OpenAI call to
produce two LLM spans (BT-5139).
Fix uses Symbol.for("braintrust.registry.enabled") on globalThis as a
cross-instance guard: only the first PluginRegistry to call enable() sets
up channel subscriptions; subsequent instances skip. disable() clears the
flag so test enable/disable cycles continue to work.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…ndalone global flag
The previous approach stored a boolean on globalThis[Symbol.for("braintrust.registry.enabled")],
which was not cleared when vi.resetModules() discards the module cache in tests —
causing the edge-runtime-bootstrap tests to fail on the second test in each
describe.each block.
The braintrust state (globalThis[Symbol.for("braintrust-state")]) is already
shared across all SDK instances loaded in the same process via
_internalSetInitialState, and is deleted by the test cleanup in
restoreRuntimeEnvironment. Storing the registry marker on that object naturally
handles both scenarios:
- BT-5139: two concurrent SDK instances share the same state → second instance
blocked by the marker left by the first.
- vi.resetModules(): test deletes the state before re-import → fresh state has
no marker → new module instance can subscribe.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
PluginRegistryinstance independently subscribed to the same process-globaldiagnostics_channel. Every OpenAI API call fired one channel event, but with two subscribers, two LLM spans were created — the duplicate spans reported in BT-5139.Symbol.for("braintrust.registry.enabled")guard onglobalThisso only the firstPluginRegistryto callenable()registers channel subscriptions.disable()clears the flag so test enable/disable cycles work correctly.isEnabled()returnstrue.Test plan
pnpm exec vitest run src/instrumentation/registry.test.ts— all 15 tests passinstanceB.isEnabled()returnstruewithout the fix) and passes with the fix🤖 Generated with Claude Code