Skip to content

.NET: [Bug]: Duplicate user input message when workflow agent invokes prompt agent #3657

@mikhail

Description

@mikhail

Description

When a workflow agent (uses agent-framework) invokes a prompt agent, the user's input message appears twice in the prompt agent's input messages. This happens because ChatClientAgent.PrepareSessionAndMessagesAsync() retrieves chat history (which may already contain the input message) and then unconditionally appends the input messages again.

What happened?

  • User sends a single message (e.g., "test") to a workflow agent
  • Workflow agent calls CopyConversationMessages to persist the message before invoking a child agent
  • Workflow invokes the prompt agent with the same message as inputMessages
  • ChatClientAgent.PrepareSessionAndMessagesAsync retrieves history (now containing the message)
  • It then adds inputMessages unconditionally → duplicate message

What did you expect to happen?

The user's input message should appear exactly once in the agent's input, not duplicated.

Steps to reproduce the issue

  1. Create a workflow agent that invokes a prompt agent
  2. Send a single user message to the workflow
  3. Observe the prompt agent receives the input message twice

Code Sample

Bug location: `dotnet/src/Microsoft.Agents.AI/ChatClient/ChatClientAgent.cs`, method `PrepareSessionAndMessagesAsync()`, lines 711-723


   // Lines 714-720: Retrieve history from ChatHistoryProvider
   if (chatHistoryProvider is not null)
   {
       var providerMessages = await chatHistoryProvider.InvokingAsync(...);
       inputMessagesForChatClient.AddRange(providerMessages);  // History may contain input message
   }

   // Lines 722-723: Add input messages AGAIN
   inputMessagesForChatClient.AddRange(inputMessages);  // Duplicates if message was already in history!

  Proposed fix:

   // After getting chat history, filter out messages already present before adding inputMessages
   if (chatHistoryProvider is not null)
   {
       // ... existing code ...
   }

   // Only add input messages that are not already in history
   HashSet<string?> existingMessageIds = inputMessagesForChatClient
       .Where(m => !string.IsNullOrEmpty(m.MessageId))
       .Select(m => m.MessageId)
       .ToHashSet();

   inputMessagesForChatClient.AddRange(
       inputMessages.Where(m => string.IsNullOrEmpty(m.MessageId) || !existingMessageIds.Contains(m.MessageId)));

Error Messages / Stack Traces

none. just happens.

Package Versions

latest utilized by workflow agents

.NET Version

No response

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    .NETbugSomething isn't workingtriagev1.0Features being tracked for the version 1.0 GA

    Type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions