Fix Activity.Current nulled during streaming tool invocation#7321
Merged
stephentoub merged 4 commits intodotnet:mainfrom Feb 20, 2026
Merged
Fix Activity.Current nulled during streaming tool invocation#7321stephentoub merged 4 commits intodotnet:mainfrom
stephentoub merged 4 commits intodotnet:mainfrom
Conversation
Fixes dotnet#7320 When an invoke_agent span with a name suffix is the current activity (e.g. "invoke_agent MyAgent(id)"), CurrentActivityIsInvokeAgent returns true and the orchestrate_tools activity is not created. The local activity variable is null, and all Activity.Current = activity workaround sites for dotnet/runtime#47802 set Activity.Current to null after each yield return. This disconnects subsequent spans from the trace. Capture the current activity before it can be lost and restore that value instead of null after each yield. Co-authored-by: Copilot <[email protected]>
Contributor
There was a problem hiding this comment.
Pull request overview
This pull request fixes a regression introduced in PR #7224 where Activity.Current was being set to null during streaming tool invocation when an invoke_agent span with a name suffix is the parent activity. When CurrentActivityIsInvokeAgent matches (for names like "invoke_agent MyAgent(id)"), the orchestrate_tools activity is not created and the local activity variable becomes null. The workaround code Activity.Current = activity then disconnects all subsequent spans from the trace after each yield return.
Changes:
- Captures the current activity before entering the streaming loop:
Activity? activityToRestore = activity ?? Activity.Current; - Updates all 6
Activity.Current = activityworkaround sites to useactivityToRestoreinstead - Adds a streaming test that verifies trace context is preserved when an
invoke_agent MyAgent(agent-123)parent span is active
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/Libraries/Microsoft.Extensions.AI/ChatCompletion/FunctionInvokingChatClient.cs | Captures activityToRestore before streaming loop and uses it to restore Activity.Current after each yield return, preventing trace context loss when activity is null |
| test/Libraries/Microsoft.Extensions.AI.Tests/ChatCompletion/FunctionInvokingChatClientTests.cs | Adds StreamingPreservesTraceContextWhenInvokeAgentWithNameIsParent test that creates an invoke_agent parent span, triggers streaming with tool calls, and verifies all child spans share the same TraceId |
stephentoub
reviewed
Feb 19, 2026
src/Libraries/Microsoft.Extensions.AI/ChatCompletion/FunctionInvokingChatClient.cs
Outdated
Show resolved
Hide resolved
Contributor
Author
|
@dotnet-policy-service agree company="Microsoft" |
Use a null check around Activity.Current assignment instead of capturing a separate activityToRestore variable, per reviewer feedback. Co-authored-by: Copilot <[email protected]>
stephentoub
approved these changes
Feb 19, 2026
test/Libraries/Microsoft.Extensions.AI.Tests/ChatCompletion/FunctionInvokingChatClientTests.cs
Outdated
Show resolved
Hide resolved
…nctionInvokingChatClientTests.cs
Closed
4 tasks
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
Fix
Activity.Currentbeing set tonullduring streaming tool invocation when aninvoke_agentspan with a name suffix is the parent activity.Fixes #7320
Downstream impact: microsoft/agent-framework#4074
Problem
PR #7224 broadened
CurrentActivityIsInvokeAgentto match display names like"invoke_agent MyAgent(id)"(prefix + space). When it matches, theorchestrate_toolsactivity is not created and the localactivityvariable isnull. The sixActivity.Current = activityworkaround sites (for dotnet/runtime#47802) inGetStreamingResponseAsyncthen setActivity.Current = nullafter eachyield return, disconnecting all subsequent spans from the trace.Fix
Capture the current activity (the
invoke_agentparent) before it can be lost:Then restore that value instead of
nullafter eachyield return.Testing
Added
StreamingPreservesTraceContextWhenInvokeAgentWithNameIsParent— a streaming test that creates aninvoke_agent MyAgent(agent-123)parent span, triggers LLM call → tool call → second LLM call, and verifies all child spans share the sameTraceId. This test fails onmainand passes with the fix.Full M.E.AI test suite: 442 passed, 0 failed across net10.0, net9.0, net8.0, net462.