Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 22, 2026

  • Fix CurrentActivityIsInvokeAgent property to use StartsWith instead of equality check
  • Updated implementation to check for exact match OR starts with "invoke_agent " (space delimiter)
  • Refactored to avoid string allocation by checking length and character index instead of concatenation
  • Added positive tests for valid invoke_agent display names ("invoke_agent", "invoke_agent my_agent", "invoke_agent ")
  • Added negative tests for names that should NOT match ("invoke_agen", "invoke_agent_extra", "invoke_agentx")
  • Run targeted tests to validate the changes
  • Run code review

Summary

Fixed the FunctionInvokingChatClient to correctly detect invoke_agent spans. The implementation now checks for:

  1. Starts with "invoke_agent"
  2. Either exact match (length equals) OR followed by space character

This prevents false positives for span names like "invoke_agent_extra" while avoiding unnecessary string allocations.

Original prompt

This section details on the original issue you should resolve

<issue_title>FunctionInvokingChatClient doesn't validate parent span correctly</issue_title>
<issue_description>### Description

The FunctionInvokingChatClient is supposed to only create an orchestrate_tools span if the current span is not an invoke_agent span.

However, the check here: https://github.com/dotnet/extensions/blob/main/src/Libraries/Microsoft.Extensions.AI/ChatCompletion/FunctionInvokingChatClient.cs#L1115 will never return true because the display name of the current span, even when it's an invoke_agent, will have a postfix, something like "invoke_agent demo_app". The correct implementation should check if the display name starts with OpenTelemetryConsts.GenAI.InvokeAgentName instead of equality.

Reproduction Steps

Run Agent Framework sample: https://github.com/microsoft/agent-framework/tree/main/dotnet/samples/GettingStarted/AgentOpenTelemetry

Expected behavior

The ochestrate_tools span should not be present.

Actual behavior

The ochestrate_tools span is present.

Image

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response</issue_description>

<agent_instructions>Presumably this should have used Starts With. I also thought we had tests for this, but apparently not? Please ensure we do. </agent_instructions>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Microsoft Reviewers: Open in CodeFlow

Copilot AI changed the title [WIP] Fix validation of parent span in FunctionInvokingChatClient Fix FunctionInvokingChatClient invoke_agent span detection to use StartsWith Jan 22, 2026
Copilot AI requested a review from stephentoub January 22, 2026 01:42
@stephentoub stephentoub marked this pull request as ready for review January 22, 2026 01:56
@stephentoub stephentoub requested a review from a team as a code owner January 22, 2026 01:56
Copilot AI review requested due to automatic review settings January 22, 2026 01:56
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This pull request fixes a bug in FunctionInvokingChatClient where the span detection logic for invoke_agent spans was using exact string equality instead of a prefix check. The issue prevented the correct detection of invoke_agent spans with postfixed display names like "invoke_agent demo_app", causing spurious orchestrate_tools spans to be created.

Changes:

  • Modified CurrentActivityIsInvokeAgent property to use StartsWith instead of equality comparison
  • Added new theory test with three test cases to validate the fix handles postfixed display names

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
src/Libraries/Microsoft.Extensions.AI/ChatCompletion/FunctionInvokingChatClient.cs Changed span detection from exact equality to StartsWith check
test/Libraries/Microsoft.Extensions.AI.Tests/ChatCompletion/FunctionInvokingChatClientTests.cs Added parameterized test covering postfixed display name scenarios

Copilot AI changed the title Fix FunctionInvokingChatClient invoke_agent span detection to use StartsWith Fix FunctionInvokingChatClient invoke_agent span detection with exact match or space delimiter Jan 22, 2026
Copilot AI requested a review from stephentoub January 22, 2026 02:13
This was referenced Feb 11, 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.

FunctionInvokingChatClient doesn't validate parent span correctly

3 participants