Skip to content

.NET: Allow Simulating service stored ChatHistory to improve consistency#4974

Merged
westey-m merged 9 commits intomicrosoft:mainfrom
westey-m:service-stored-chathistory-simulation
Mar 30, 2026
Merged

.NET: Allow Simulating service stored ChatHistory to improve consistency#4974
westey-m merged 9 commits intomicrosoft:mainfrom
westey-m:service-stored-chathistory-simulation

Conversation

@westey-m
Copy link
Copy Markdown
Contributor

@westey-m westey-m commented Mar 30, 2026

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

  • Allow Simulating service stored ChatHistory to improve consistency

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.

Copilot AI review requested due to automatic review settings March 30, 2026 13:30
@markwallace-microsoft markwallace-microsoft added documentation Improvements or additions to documentation .NET labels Mar 30, 2026
@github-actions github-actions bot changed the title Allow Simulating service stored ChatHistory to improve consistency .NET: Allow Simulating service stored ChatHistory to improve consistency Mar 30, 2026
Copy link
Copy Markdown
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 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 ServiceStoredSimulatingChatClient decorator and inject it when ChatClientAgentOptions.SimulateServiceStoredChatHistory is enabled.
  • Replace/remove the previous ChatHistoryPersistingChatClient approach 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.

Co-authored-by: SergeyMenshykh <68852919+SergeyMenshykh@users.noreply.github.com>
@westey-m westey-m added this pull request to the merge queue Mar 30, 2026
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Mar 30, 2026
@westey-m westey-m added this pull request to the merge queue Mar 30, 2026
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Mar 30, 2026
@westey-m westey-m enabled auto-merge March 30, 2026 18:13
@westey-m westey-m added this pull request to the merge queue Mar 30, 2026
Merged via the queue into microsoft:main with commit 401e5dc Mar 30, 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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants