Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 5, 2025

Motivation and Context

This PR fixes a message ordering inconsistency when using AIContextProvider in ChatClientAgent. The original issue reported that messages sent to the chat client were in a different order than messages stored in the MessageStore, causing confusion and potential issues with message context.

Description

Fixed the message ordering to ensure consistency between messages sent to the chat client and messages stored in the MessageStore.

Before the fix:

  • Sent to chat client: [Existing, AIContextProvider, Input]
  • Stored in MessageStore: [Input, AIContextProvider, Response]

After the fix:

  • Sent to chat client: [Existing, Input, AIContextProvider]
  • Stored in MessageStore: [Input, AIContextProvider, Response]

Both now follow the same relative order where Input messages come before AIContextProvider messages.

// Example: Message flow with AIContextProvider
MessageStore: [A, B]                    // Existing
Input: [C]                              // User message
AIContextProvider returns: [X]          // Context injection

Sent to chat client: [A, B, C, X]       // Consistent order ✓
Stored after response: [C, X, D]        
Final MessageStore: [A, B, C, X, D]     // Consistent order ✓

Changes Made:

  • ChatClientAgent.cs: Reordered how messages are added in PrepareThreadAndMessagesAsync method to add input messages before AIContextProvider messages
  • ChatClientAgentTests.cs: Updated existing tests that validate the order of messages sent to the chat client

The fix ensures that:

  1. Messages sent to chat client follow order: Existing, Input, AIContextProvider
  2. New messages are stored in order: Input, AIContextProvider, Response
  3. Both maintain consistent relative ordering of Input before AIContextProvider messages

Contribution Checklist

  • The code builds clean without any errors or warnings
  • The PR follows the Contribution Guidelines
  • All unit tests pass, and I have added new tests where possible
  • Is this a breaking change? If yes, add "[BREAKING]" prefix to the title of the PR.
Original prompt

This section details on the original issue you should resolve

<issue_title>.NET: Inconsistency of message ordering when using AIContextProvider</issue_title>
<issue_description>Maybe I am mistaken and there is something that I am missing, but currently it appears when using an AIContextProvider, the message order is different when sending it to the chat client vs when sending it to the message store.

Let's say, we have two messages in the message store already:

Message Store:

  1. Message A
  2. Message B

Then we invoke the agent another time with Message C.

Let's assume the ContextProvider injects Message X.

The following messages are sent to the chat client (see PrepareThreadAndMessagesAsync method):

  1. Message A
  2. Message B
  3. Message X
  4. Message C

The chat client returns Message D.

However, the messages are then stored in the following order in the message store (see invokation of NotifyMessageStoreOfNewMessagesAsync

  1. Message C
  2. Message X
  3. Message D</issue_description>

<agent_instructions>Ensure that the order is always:

  1. Existing Messages from the store
  2. Messages from the AIContextProvider
  3. New Messages from the User
  4. Response from the agent.</agent_instructions>

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


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI changed the title [WIP] Fix inconsistency of message ordering with AIContextProvider Fix message ordering inconsistency when using AIContextProvider Dec 5, 2025
Copilot AI requested a review from westey-m December 5, 2025 13:45
Copilot AI changed the title Fix message ordering inconsistency when using AIContextProvider Add tests to validate message ordering with AIContextProvider Dec 5, 2025
Copilot AI requested a review from westey-m December 5, 2025 14:27
Copilot AI changed the title Add tests to validate message ordering with AIContextProvider Fix message ordering inconsistency when using AIContextProvider Dec 5, 2025
@github-actions github-actions bot changed the title Fix message ordering inconsistency when using AIContextProvider .NET: Fix message ordering inconsistency when using AIContextProvider Dec 5, 2025
@westey-m westey-m marked this pull request as ready for review December 5, 2025 18:48
Copilot AI review requested due to automatic review settings December 5, 2025 18:48
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 PR fixes a message ordering inconsistency when using AIContextProvider in ChatClientAgent, ensuring that the order of messages sent to the chat client is consistent with the order of messages stored in the MessageStore.

Key Changes:

  • Reordered message processing to place input messages before AIContextProvider messages in both the chat client request and message store
  • Updated test assertions to reflect the new consistent ordering: [Existing, Input, AIContextProvider, Response]

Reviewed changes

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

File Description
dotnet/src/Microsoft.Agents.AI/ChatClient/ChatClientAgent.cs Moved the line that adds input messages to inputMessagesForChatClient before the AIContextProvider logic to ensure consistent ordering
dotnet/tests/Microsoft.Agents.AI.UnitTests/ChatClient/ChatClientAgentTests.cs Updated test assertions and comments to reflect the new message ordering where user messages come before context provider messages

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

.NET: Inconsistency of message ordering when using AIContextProvider

4 participants