.NET: Allow Simulating service stored ChatHistory to improve consistency#4974
Merged
westey-m merged 9 commits intomicrosoft:mainfrom Mar 30, 2026
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR introduces an opt-in mode for ChatClientAgent to simulate service-stored chat history while still using a ChatHistoryProvider, aiming to make framework-managed persistence behave like service-managed history during multi-call function/tool loops.
Changes:
- Add
ServiceStoredSimulatingChatClientdecorator and inject it whenChatClientAgentOptions.SimulateServiceStoredChatHistoryis enabled. - Replace/remove the previous
ChatHistoryPersistingChatClientapproach and update agent logic + tests to match the new default (end-of-run) vs opt-in (per-service-call) behavior. - Update samples and ADR documentation to reflect the new persistence model and configuration matrix.
Reviewed changes
Copilot reviewed 16 out of 16 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| dotnet/src/Microsoft.Agents.AI/ChatClient/ServiceStoredSimulatingChatClient.cs | New decorator that loads history before each call and persists after each call while returning a sentinel conversation id. |
| dotnet/src/Microsoft.Agents.AI/ChatClient/ChatHistoryPersistingChatClient.cs | Removed prior per-service-call/mark-only persistence decorator. |
| dotnet/src/Microsoft.Agents.AI/ChatClient/ChatClientExtensions.cs | Conditionally inject the new decorator when SimulateServiceStoredChatHistory is enabled. |
| dotnet/src/Microsoft.Agents.AI/ChatClient/ChatClientBuilderExtensions.cs | Replace old builder extension with UseServiceStoredChatHistorySimulation. |
| dotnet/src/Microsoft.Agents.AI/ChatClient/ChatClientAgentOptions.cs | Replace PersistChatHistoryAtEndOfRun with SimulateServiceStoredChatHistory and update docs/clone. |
| dotnet/src/Microsoft.Agents.AI/ChatClient/ChatClientAgentLogMessages.cs | Update warnings/messages to reference the new option/decorator. |
| dotnet/src/Microsoft.Agents.AI/ChatClient/ChatClientAgent.cs | Adjust history loading/persistence control flow to account for simulation mode and add shared LoadChatHistoryAsync. |
| dotnet/tests/Microsoft.Agents.AI.UnitTests/ChatClient/ServiceStoredSimulatingChatClientTests.cs | Rename/repurpose tests to cover the new decorator/option behavior. |
| dotnet/tests/Microsoft.Agents.AI.UnitTests/ChatClient/ChatClientAgent_ChatOptionsMergingTests.cs | Update expectations: inner client receives null ChatOptions when none are provided. |
| dotnet/tests/Microsoft.Agents.AI.UnitTests/ChatClient/ChatClientAgent_ChatHistoryManagementTests.cs | Update test options to use SimulateServiceStoredChatHistory. |
| dotnet/tests/Microsoft.Agents.AI.UnitTests/ChatClient/ChatClientAgent_ApprovalsTests.cs | Update approval-flow tests to reference new decorator/option. |
| dotnet/tests/Microsoft.Agents.AI.UnitTests/ChatClient/ChatClientAgentTests.cs | Update expectations around ChatOptions forwarding. |
| dotnet/tests/Microsoft.Agents.AI.UnitTests/ChatClient/ChatClientAgentTestHelper.cs | Update comments/refs to new decorator. |
| dotnet/samples/02-agents/Agents/Agent_Step19_InFunctionLoopCheckpointing/README.md | Update sample explanation to use SimulateServiceStoredChatHistory. |
| dotnet/samples/02-agents/Agents/Agent_Step19_InFunctionLoopCheckpointing/Program.cs | Enable SimulateServiceStoredChatHistory in the sample. |
| docs/decisions/0022-chat-history-persistence-consistency.md | Update ADR to reflect opt-in simulation approach and new configuration matrix. |
Comments suppressed due to low confidence (2)
dotnet/tests/Microsoft.Agents.AI.UnitTests/ChatClient/ServiceStoredSimulatingChatClientTests.cs:54
- This test’s summary/name say it verifies the default behavior when SimulateServiceStoredChatHistory is false, but the test explicitly sets SimulateServiceStoredChatHistory = true. Either update the wording/name to reflect the opt-in behavior, or remove the explicit setting and adjust assertions to match the real default (end-of-run persistence).
dotnet/tests/Microsoft.Agents.AI.UnitTests/ChatClient/ServiceStoredSimulatingChatClientTests.cs:115 - RunAsync_PersistsMessagesAtEndOfRun_WhenOptionEnabledAsync is now configured with SimulateServiceStoredChatHistory=true, but the test comment/assert description claims it’s verifying end-of-run persistence. With SimulateServiceStoredChatHistory enabled, ChatClientAgent skips end-of-run persistence unless forced (continuation/background), so this test no longer validates what its name suggests and could mask regressions. Consider rewriting it to cover the actual default end-of-run persistence case (SimulateServiceStoredChatHistory=false), or to explicitly assert the decorator is the component performing persistence when simulation is enabled.
dotnet/src/Microsoft.Agents.AI/ChatClient/ServiceStoredSimulatingChatClient.cs
Show resolved
Hide resolved
dotnet/src/Microsoft.Agents.AI/ChatClient/ServiceStoredSimulatingChatClient.cs
Show resolved
Hide resolved
rogerbarreto
approved these changes
Mar 30, 2026
dotnet/src/Microsoft.Agents.AI/ChatClient/ServiceStoredSimulatingChatClient.cs
Show resolved
Hide resolved
SergeyMenshykh
approved these changes
Mar 30, 2026
dotnet/src/Microsoft.Agents.AI/ChatClient/ChatClientBuilderExtensions.cs
Show resolved
Hide resolved
Co-authored-by: SergeyMenshykh <68852919+SergeyMenshykh@users.noreply.github.com>
SergeyMenshykh
approved these changes
Mar 30, 2026
SergeyMenshykh
approved these changes
Mar 30, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation and Context
When storing chat history via ChatHistoryProvider has some differences to storing chat history in the underlying AI service.
This PR introduces a way to use a ChatHistoryProvider but with a ChatClient decorator that simulates storing chat history in the underlying AI service, so that their behavior matches.
#4976
Description
Contribution Checklist