Skip to content

.NET: Add AIContextProvider message event and fix internal agent issue#6505

Open
XiongHaoTrigger wants to merge 3 commits into
microsoft:mainfrom
XiongHaoTrigger:fix/WorkflowHostAgent
Open

.NET: Add AIContextProvider message event and fix internal agent issue#6505
XiongHaoTrigger wants to merge 3 commits into
microsoft:mainfrom
XiongHaoTrigger:fix/WorkflowHostAgent

Conversation

@XiongHaoTrigger

@XiongHaoTrigger XiongHaoTrigger commented Jun 13, 2026

Copy link
Copy Markdown
Contributor

Motivation and Context

When a ChatClientAgent is used directly, messages injected by AIContextProvider are included in the agent session history and are preserved when the session is serialized.

However, when the same agent is wrapped with AgentWorkflowBuilder.BuildSequential(...).AsAIAgent(), the workflow session only persisted the original user input and the final workflow response. The request-side messages injected by the inner agent's AIContextProvider stayed inside the inner agent session and were not copied into the outer WorkflowSession.

This caused the workflow-as-agent session history and serialized state to lose important contextual messages.

Fixes #6344.

Description

This change adds an internal workflow event for AIContextProvider-injected request messages.

AIAgentHostExecutor now captures the inner agent session history before and after invoking the hosted agent. Any newly added request messages marked with AgentRequestMessageSourceType.AIContextProvider are emitted as an internal workflow event.

WorkflowSession handles this internal event by adding those messages to the workflow chat history. The messages are persisted into the outer workflow session, but they are not yielded as user-visible output and are not forwarded to downstream agents in the workflow.

New regression tests verify that:

  • AIContextProvider-injected messages are persisted in workflow-as-agent chat history.
  • The messages survive workflow session serialization.

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.

…e issue where the internal agent in Workflow is unable to pass the content of AIContextProvider to the Workflow.
Copilot AI review requested due to automatic review settings June 13, 2026 12:05
@moonbox3 moonbox3 added .NET workflows Related to Workflows in agent-framework labels Jun 13, 2026
@github-actions github-actions Bot changed the title Add AIContextProvider message event and fix internal agent issue .NET: Add AIContextProvider message event and fix internal agent issue Jun 13, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Adds support for persisting AIContextProvider-injected request messages into workflow chat history (and session serialization) while preventing those injected messages from being forwarded to downstream agents.

Changes:

  • Emit a workflow event for AIContextProvider-injected request messages detected during agent invocation.
  • Persist injected request messages into WorkflowSession chat history when the new event is observed.
  • Add unit tests covering serialization persistence and non-forwarding to downstream agents.

Reviewed changes

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

File Description
dotnet/tests/Microsoft.Agents.AI.Workflows.UnitTests/AIContextProviderWorkflow.cs Adds regression tests for persisting injected context into workflow history/serialization and preventing downstream leakage
dotnet/src/Microsoft.Agents.AI.Workflows/WorkflowSession.cs Handles a new workflow event to persist AIContextProvider request messages into workflow chat history
dotnet/src/Microsoft.Agents.AI.Workflows/Specialized/AIAgentHostExecutor.cs Detects injected request messages by diffing stored chat history and emits an event to the workflow
dotnet/src/Microsoft.Agents.AI.Workflows/AgentAIContextProviderMsgEvent.cs Introduces an internal workflow event carrying injected request messages

Comment thread dotnet/src/Microsoft.Agents.AI.Workflows/WorkflowSession.cs
Comment thread dotnet/src/Microsoft.Agents.AI.Workflows/Specialized/AIAgentHostExecutor.cs Outdated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

.NET workflows Related to Workflows in agent-framework

Projects

None yet

Development

Successfully merging this pull request may close these issues.

.NET: [Bug]: AIContextProvider messages are automatically removed when using a Workflow-as-Agent

3 participants