Skip to content

.NET: [BREAKING] Add session StateBag for state storage and support multiple providers on the Agent#3806

Merged
westey-m merged 16 commits intomainfrom
feature-session-statebag
Feb 13, 2026
Merged

.NET: [BREAKING] Add session StateBag for state storage and support multiple providers on the Agent#3806
westey-m merged 16 commits intomainfrom
feature-session-statebag

Conversation

@westey-m
Copy link
Contributor

Motivation and Context

Description

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.

westey-m and others added 2 commits February 10, 2026 12:03
…d of inside providers (#3737)

* Add a StateBag to AgentSession and pass Agent and AgentSession to AIContextProvider and ChatHistoryProviders

* Convert all AIContextProviders to use the statebag

* Update InMemoryChatHistoryProvider to use StateBag

* Update Comsos and Workflow ChatHistoryProviders

* Update 3rd party chat history storage sample.

* Remove serialize method from providers

* Replacing provider factories with properties

* Remove Providers from Session and flatten state bag serialization

* Update samples to use getservice on agent

* Updated additional session types to serialize statebag

* Fix regression

* Address PR comments

* Address PR comments.

* Fix formatting

* Fix unit tests

* Remove InMemoryAgentSession since it is not required anymore.

* Address PR comments

* Convert sessions for A2AAgent, ChatClientAgent, CopilotStudioAgent and GithubCopilotAgent to use regular json serialization.

* Fix durable agent session jso usgae

* Add jso to InMemory and Workflow ChatHistoryProviders

* Update InMemoryChatHistoryProvider to use an options class for it's many optional settings.

* Apply suggestions from code review

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Address PR feedback

* Fix verification bug.

* Improve state bag thread safety

* Address PR comments and fix unit tests

* Address PR comments

* Fix unit test

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
…pate in a pipeline (#3846)

* Make providers pipeline capable

* Fix unit tests

* Move source stamping to providers from base class

* Also update samples.

* Address PR comments

* Rename AsAgentRequestMessageSourcedMessage to WithAgentRequestMessageSource
@westey-m westey-m marked this pull request as ready for review February 12, 2026 20:35
Copilot AI review requested due to automatic review settings February 12, 2026 20:35
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 introduces a breaking redesign of .NET session state and provider configuration by adding a per-session StateBag for provider state storage and enabling multiple AIContextProviders on an agent. It updates core abstractions, providers, tests, and samples to align with the new model.

Changes:

  • Add AgentSession.StateBag and supporting serialization/converter infrastructure for session-scoped provider state.
  • Update ChatHistoryProvider/AIContextProvider and ChatClientAgentOptions to support agent-level providers (and multiple context providers) instead of per-session/provider factories/serialization.
  • Refactor built-in providers, workflows, tests, and samples to store state in StateBag and to use updated invocation context shapes (AIContext pipeline + revised invoked contexts).

Reviewed changes

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

Show a summary per file
File Description
dotnet/tests/OpenAIResponse.IntegrationTests/OpenAIResponseFixture.cs Read chat history via agent service provider lookup
dotnet/tests/OpenAIChatCompletion.IntegrationTests/OpenAIChatCompletionFixture.cs Read chat history via agent service provider lookup
dotnet/tests/Microsoft.Agents.AI.Workflows.UnitTests/WorkflowHostSmokeTests.cs Update custom sessions to new AgentSession + StateBag model
dotnet/tests/Microsoft.Agents.AI.Workflows.UnitTests/TestRequestAgent.cs Migrate session serialization to StateBag
dotnet/tests/Microsoft.Agents.AI.Workflows.UnitTests/TestReplayAgent.cs Replace InMemoryAgentSession with AgentSession
dotnet/tests/Microsoft.Agents.AI.Workflows.UnitTests/TestEchoAgent.cs Switch chat history storage to provider + StateBag
dotnet/tests/Microsoft.Agents.AI.Workflows.UnitTests/Sample/06_GroupChat_Workflow.cs Replace InMemoryAgentSession with AgentSession
dotnet/tests/Microsoft.Agents.AI.Workflows.UnitTests/RoleCheckAgent.cs Replace InMemoryAgentSession with AgentSession
dotnet/tests/Microsoft.Agents.AI.Workflows.UnitTests/InProcessExecutionTests.cs Replace InMemoryAgentSession with AgentSession
dotnet/tests/Microsoft.Agents.AI.Workflows.UnitTests/AgentWorkflowBuilderTests.cs Replace InMemoryAgentSession with AgentSession
dotnet/tests/Microsoft.Agents.AI.UnitTests/TestJsonSerializerContext.cs Add ChatClientAgentSession to STJ source gen context
dotnet/tests/Microsoft.Agents.AI.UnitTests/ChatClient/ChatClientAgent_DeserializeSessionTests.cs Remove obsolete tests for old session/provider factory design
dotnet/tests/Microsoft.Agents.AI.UnitTests/ChatClient/ChatClientAgent_CreateSessionTests.cs Remove obsolete tests for old session/provider factory design
dotnet/tests/Microsoft.Agents.AI.UnitTests/ChatClient/ChatClientAgent_ChatHistoryManagementTests.cs Update tests to agent-level provider + new invoked contexts
dotnet/tests/Microsoft.Agents.AI.UnitTests/ChatClient/ChatClientAgent_BackgroundResponsesTests.cs Update setup for multiple AI context providers + state keys
dotnet/tests/Microsoft.Agents.AI.UnitTests/ChatClient/ChatClientAgentSessionTests.cs Update serialization/deserialization tests to StateBag model
dotnet/tests/Microsoft.Agents.AI.UnitTests/ChatClient/ChatClientAgentOptionsTests.cs Update options tests to ChatHistoryProvider + AIContextProviders
dotnet/tests/Microsoft.Agents.AI.Mem0.IntegrationTests/Mem0ProviderTests.cs Update Mem0 tests to session-scoped state via StateBag
dotnet/tests/Microsoft.Agents.AI.Hosting.AGUI.AspNetCore.UnitTests/AGUIEndpointRouteBuilderExtensionsTests.cs Migrate test sessions to STJ + StateBag
dotnet/tests/Microsoft.Agents.AI.Hosting.AGUI.AspNetCore.IntegrationTests/SharedStateTests.cs Migrate test sessions to STJ + StateBag
dotnet/tests/Microsoft.Agents.AI.Hosting.AGUI.AspNetCore.IntegrationTests/ForwardedPropertiesTests.cs Migrate test sessions to STJ + StateBag
dotnet/tests/Microsoft.Agents.AI.Hosting.AGUI.AspNetCore.IntegrationTests/BasicStreamingTests.cs Migrate test sessions to STJ + StateBag
dotnet/tests/Microsoft.Agents.AI.DurableTask.UnitTests/DurableAgentSessionTests.cs Update expected durable session JSON + add StateBag roundtrip tests
dotnet/tests/Microsoft.Agents.AI.AzureAI.UnitTests/AzureAIProjectChatClientExtensionsTests.cs Update options preservation tests to new provider configuration
dotnet/tests/Microsoft.Agents.AI.Abstractions.UnitTests/TestJsonSerializerContext.cs Remove source-gen types tied to removed session models
dotnet/tests/Microsoft.Agents.AI.Abstractions.UnitTests/ServiceIdAgentSessionTests.cs Remove tests for deleted ServiceIdAgentSession
dotnet/tests/Microsoft.Agents.AI.Abstractions.UnitTests/InMemoryAgentSessionTests.cs Remove tests for deleted InMemoryAgentSession
dotnet/tests/Microsoft.Agents.AI.Abstractions.UnitTests/ChatMessageExtensionsTests.cs Update renamed message-stamping API usage
dotnet/tests/Microsoft.Agents.AI.Abstractions.UnitTests/ChatHistoryProviderTests.cs Update tests for revised invoked context API
dotnet/tests/Microsoft.Agents.AI.Abstractions.UnitTests/ChatHistoryProviderMessageFilterTests.cs Remove tests for deleted provider filter decorator
dotnet/tests/Microsoft.Agents.AI.Abstractions.UnitTests/ChatHistoryProviderExtensionsTests.cs Remove tests for deleted provider extensions
dotnet/tests/Microsoft.Agents.AI.Abstractions.UnitTests/AgentSessionTests.cs Add basic StateBag roundtrip test
dotnet/tests/Microsoft.Agents.AI.Abstractions.UnitTests/AgentRequestMessageSourceTypeTests.cs Add ToString() tests
dotnet/tests/Microsoft.Agents.AI.Abstractions.UnitTests/AgentRequestMessageSourceAttributionTests.cs Add ToString() tests
dotnet/tests/Microsoft.Agents.AI.Abstractions.UnitTests/AIContextTests.cs Update tests for IEnumerable-based Messages/Tools
dotnet/tests/Microsoft.Agents.AI.A2A.UnitTests/A2AAgentSessionTests.cs Update A2A session deserialize + add StateBag preservation test
dotnet/tests/AzureAI.IntegrationTests/AIProjectClientFixture.cs Read chat history via agent service provider lookup
dotnet/tests/AnthropicChatCompletion.IntegrationTests/AnthropicChatCompletionFixture.cs Read chat history via agent service provider lookup
dotnet/src/Microsoft.Agents.AI/TextSearchProviderOptions.cs Add StateKey + message filters for statebag-backed provider state
dotnet/src/Microsoft.Agents.AI/TextSearchProvider.cs Move provider state to StateBag, update message/tool accumulation
dotnet/src/Microsoft.Agents.AI/Memory/ChatHistoryMemoryProviderOptions.cs Add StateKey + message filters for statebag-backed provider state
dotnet/src/Microsoft.Agents.AI/ChatClient/ChatClientAgentSession.cs Convert session persistence to STJ + StateBag
dotnet/src/Microsoft.Agents.AI/ChatClient/ChatClientAgentOptions.cs Replace factories with ChatHistoryProvider + AIContextProviders
dotnet/src/Microsoft.Agents.AI/AgentJsonUtilities.cs Update STJ context types for new session/provider state model
dotnet/src/Microsoft.Agents.AI.Workflows/WorkflowSession.cs Store workflow session state in StateBag + update provider integration
dotnet/src/Microsoft.Agents.AI.Workflows/WorkflowHostAgent.cs Update workflow message storage path to pass session into provider
dotnet/src/Microsoft.Agents.AI.Workflows/WorkflowChatHistoryProvider.cs Migrate provider storage to StateBag
dotnet/src/Microsoft.Agents.AI.OpenAI/Extensions/OpenAIAssistantClientExtensions.cs Update options mapping to new provider configuration
dotnet/src/Microsoft.Agents.AI.Mem0/Mem0ProviderOptions.cs Add StateKey + message filters for statebag-backed provider state
dotnet/src/Microsoft.Agents.AI.Mem0/Mem0JsonUtilities.cs Update STJ context type names
dotnet/src/Microsoft.Agents.AI.GitHub.Copilot/GitHubCopilotJsonUtilities.cs Switch STJ context to session type
dotnet/src/Microsoft.Agents.AI.GitHub.Copilot/GitHubCopilotAgentSession.cs Convert session persistence to STJ + StateBag
dotnet/src/Microsoft.Agents.AI.GitHub.Copilot/GitHubCopilotAgent.cs Use new Deserialize session helper
dotnet/src/Microsoft.Agents.AI.DurableTask/DurableAgentSession.cs Add StateBag persistence to durable sessions
dotnet/src/Microsoft.Agents.AI.CosmosNoSql/CosmosDBChatExtensions.cs Replace factory with shared provider + session state initializer
dotnet/src/Microsoft.Agents.AI.CopilotStudio/CopilotStudioJsonUtilities.cs Add STJ utilities for Copilot Studio session
dotnet/src/Microsoft.Agents.AI.CopilotStudio/CopilotStudioAgentSession.cs Replace ServiceIdAgentSession with AgentSession + StateBag
dotnet/src/Microsoft.Agents.AI.CopilotStudio/CopilotStudioAgent.cs Use new Deserialize session helper
dotnet/src/Microsoft.Agents.AI.AzureAI/AzureAIProjectChatClientExtensions.cs Update options mapping to new provider configuration
dotnet/src/Microsoft.Agents.AI.AzureAI.Persistent/PersistentAgentsClientExtensions.cs Update options mapping to new provider configuration
dotnet/src/Microsoft.Agents.AI.Abstractions/ServiceIdAgentSession.cs Remove obsolete session base type
dotnet/src/Microsoft.Agents.AI.Abstractions/InMemoryChatHistoryProviderOptions.cs New options object for statebag-backed in-memory chat history
dotnet/src/Microsoft.Agents.AI.Abstractions/InMemoryAgentSession.cs Remove obsolete in-memory session type
dotnet/src/Microsoft.Agents.AI.Abstractions/ChatMessageExtensions.cs Rename stamping helper to WithAgentRequestMessageSource
dotnet/src/Microsoft.Agents.AI.Abstractions/ChatHistoryProviderMessageFilter.cs Remove obsolete decorator (relied on mutable invoked context)
dotnet/src/Microsoft.Agents.AI.Abstractions/ChatHistoryProviderExtensions.cs Remove obsolete extensions
dotnet/src/Microsoft.Agents.AI.Abstractions/ChatHistoryProvider.cs Shift state storage guidance to StateBag, revise invoked context API
dotnet/src/Microsoft.Agents.AI.Abstractions/AgentSessionStateBagValueJsonConverter.cs Add converter for state bag value storage
dotnet/src/Microsoft.Agents.AI.Abstractions/AgentSessionStateBagValue.cs Add cached JSON/deserialized representation for state bag values
dotnet/src/Microsoft.Agents.AI.Abstractions/AgentSessionStateBagJsonConverter.cs Add converter to serialize state bag as a JSON object
dotnet/src/Microsoft.Agents.AI.Abstractions/AgentSessionStateBag.cs Add thread-safe session-scoped state store with JSON persistence
dotnet/src/Microsoft.Agents.AI.Abstractions/AgentSession.cs Add StateBag to base session type + debugger display
dotnet/src/Microsoft.Agents.AI.Abstractions/AgentRequestMessageSourceType.cs Add ToString() implementation
dotnet/src/Microsoft.Agents.AI.Abstractions/AgentRequestMessageSourceAttribution.cs Add ToString() implementation
dotnet/src/Microsoft.Agents.AI.Abstractions/AgentAbstractionsJsonUtilities.cs Add source-gen support for state bag types
dotnet/src/Microsoft.Agents.AI.Abstractions/AIContextProvider.cs Remove message stamping + switch to AIContext pipeline
dotnet/src/Microsoft.Agents.AI.Abstractions/AIContext.cs Switch Messages/Tools to IEnumerable to support deferred pipelines
dotnet/src/Microsoft.Agents.AI.A2A/A2AJsonUtilities.cs Switch STJ context to session type
dotnet/src/Microsoft.Agents.AI.A2A/A2AAgentSession.cs Convert session persistence to STJ + StateBag
dotnet/src/Microsoft.Agents.AI.A2A/A2AAgent.cs Use new Deserialize session helper
dotnet/samples/GettingStarted/Agents/Agent_Step20_AdditionalAIContext/Program.cs Update sample to multiple providers + StateBag storage
dotnet/samples/GettingStarted/Agents/Agent_Step16_ChatReduction/Program.cs Update sample to new in-memory provider options + retrieval
dotnet/samples/GettingStarted/Agents/Agent_Step07_3rdPartyChatHistoryStorage/Program.cs Update sample provider to store state in StateBag
dotnet/samples/GettingStarted/AgentWithRAG/AgentWithRAG_Step03_CustomRAGDataSource/Program.cs Update to AIContextProviders list
dotnet/samples/GettingStarted/AgentWithRAG/AgentWithRAG_Step02_CustomVectorStoreRAG/Program.cs Update to AIContextProviders list
dotnet/samples/GettingStarted/AgentWithRAG/AgentWithRAG_Step01_BasicTextRAG/Program.cs Update to AIContextProviders + chat history storage filter
dotnet/samples/GettingStarted/AgentWithMemory/AgentWithMemory_Step03_CustomMemory/Program.cs Update memory sample to StateBag + agent-level provider access
dotnet/samples/GettingStarted/AgentWithMemory/AgentWithMemory_Step02_MemoryUsingMem0/Program.cs Update Mem0 sample to StateBag state initializer
dotnet/samples/GettingStarted/AgentWithMemory/AgentWithMemory_Step01_ChatHistoryMemory/Program.cs Update ChatHistoryMemoryProvider initialization to state initializer
dotnet/samples/GettingStarted/AgentProviders/Agent_With_CustomImplementation/Program.cs Update custom provider usage + STJ session serialization

* Address Feedback on statebag feature branch PR

* Update dotnet/src/Microsoft.Agents.AI.DurableTask/CHANGELOG.md

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Address PR comments

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@markwallace-microsoft markwallace-microsoft added the documentation Improvements or additions to documentation label Feb 13, 2026
@westey-m westey-m added this pull request to the merge queue Feb 13, 2026
Merged via the queue into main with commit 3168eb4 Feb 13, 2026
22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation .NET workflows Related to Workflows in agent-framework

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants