Skip to content

.NET: fix: avoid local history when service returns conversation id#6141

Open
he-yufeng wants to merge 1 commit into
microsoft:mainfrom
he-yufeng:fix/service-history-provider-conflict
Open

.NET: fix: avoid local history when service returns conversation id#6141
he-yufeng wants to merge 1 commit into
microsoft:mainfrom
he-yufeng:fix/service-history-provider-conflict

Conversation

@he-yufeng

Copy link
Copy Markdown
Contributor

Summary

To verify

  • dotnet run --project dotnet/tests/Microsoft.Agents.AI.UnitTests/Microsoft.Agents.AI.UnitTests.csproj --framework net10.0 -- --filter-method "Microsoft.Agents.AI.UnitTests.ChatClientAgent_ChatHistoryManagementTests.RunAsync_DoesNotThrow_WhenNoChatHistoryProviderInOptionsAndConversationIdReturnedAsync"
  • dotnet run --project dotnet/tests/Microsoft.Agents.AI.UnitTests/Microsoft.Agents.AI.UnitTests.csproj --framework net10.0 -- --filter-class "Microsoft.Agents.AI.UnitTests.ChatClientAgent_ChatHistoryManagementTests"
  • dotnet build dotnet/src/Microsoft.Agents.AI/Microsoft.Agents.AI.csproj --no-restore
  • dotnet build dotnet/tests/Microsoft.Agents.AI.UnitTests/Microsoft.Agents.AI.UnitTests.csproj --no-restore
  • git diff --check

Notes: full solution dotnet build dotnet/agent-framework-dotnet.slnx --no-restore still fails locally on unrelated projects that have no restored assets and an existing MCP unit-test reference error.

Copilot AI review requested due to automatic review settings May 28, 2026 10:27
@moonbox3 moonbox3 added the .NET label May 28, 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.

Updates ChatClientAgent to propagate the service-returned conversation id into ChatOptions during a run, and strengthens a unit test to ensure local history is not persisted when no provider is configured in options.

Changes:

  • Set ChatOptions.ConversationId from the chat response conversation id in both non-streaming and streaming paths.
  • Add an assertion in unit tests that local in-memory history remains empty when no provider is configured via options.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
dotnet/tests/Microsoft.Agents.AI.UnitTests/ChatClient/ChatClientAgent_ChatHistoryManagementTests.cs Extends a test to verify no local history persistence occurs in the “no provider in options” scenario.
dotnet/src/Microsoft.Agents.AI/ChatClient/ChatClientAgent.cs Introduces SetResponseConversationId and applies it so the response conversation id is reflected in ChatOptions.

Comment on lines +1011 to +1021
private static ChatOptions? SetResponseConversationId(ChatOptions? chatOptions, string? responseConversationId)
{
if (string.IsNullOrWhiteSpace(responseConversationId))
{
return chatOptions;
}

chatOptions ??= new();
chatOptions.ConversationId ??= responseConversationId;
return chatOptions;
}
Comment on lines +412 to +414
var inMemoryProvider = agent.ChatHistoryProvider as InMemoryChatHistoryProvider;
Assert.NotNull(inMemoryProvider);
Assert.Empty(inMemoryProvider.GetMessages(session));
Comment on lines +1011 to +1021
private static ChatOptions? SetResponseConversationId(ChatOptions? chatOptions, string? responseConversationId)
{
if (string.IsNullOrWhiteSpace(responseConversationId))
{
return chatOptions;
}

chatOptions ??= new();
chatOptions.ConversationId ??= responseConversationId;
return chatOptions;
}
@github-actions github-actions Bot changed the title fix: avoid local history when service returns conversation id .NET: fix: avoid local history when service returns conversation id May 28, 2026
@he-yufeng he-yufeng force-pushed the fix/service-history-provider-conflict branch from 33e194b to 3a27cac Compare May 28, 2026 12:37
@he-yufeng he-yufeng force-pushed the fix/service-history-provider-conflict branch from 3a27cac to b11fa10 Compare June 12, 2026 03:40
@he-yufeng

Copy link
Copy Markdown
Contributor Author

Rebased this branch onto current main and pushed the updated head b11fa102.

I also fixed a rebase fallout in the new SetResponseConversationId helper: it now clones ChatOptions before adding a returned service conversation id, and it does not treat the local-history sentinel as a real service id. Without that, the current main branch's per-service-call persistence tests observed the sentinel through the same mutable ChatOptions reference.

Validation run locally from dotnet/:

  • dotnet build .\src\Microsoft.Agents.AI\Microsoft.Agents.AI.csproj -f net10.0 --tl:off -> passed, 0 warnings/errors
  • dotnet run --project .\tests\Microsoft.Agents.AI.UnitTests\Microsoft.Agents.AI.UnitTests.csproj -f net10.0 -> passed, 1647 tests
  • git diff --check upstream/main..HEAD -> passed

I used dotnet run --project for the xUnit v3/MTP test project because dotnet test is not usable with this checkout's current runner configuration on my Windows machine.

@he-yufeng

Copy link
Copy Markdown
Contributor Author

Rebased this branch onto current main and pushed the updated head $head.

I also addressed the open Copilot review threads:

  • SetResponseConversationId now treats the service-returned conversation id as authoritative for follow-up calls by writing it into the cloned ChatOptions.
  • The no-local-history assertion no longer depends on InMemoryChatHistoryProvider as a concrete type.
  • Added a regression test covering a stale request ConversationId followed by a service-returned id inside a function-calling loop.

Validation run locally from dotnet/:

  • dotnet build .\src\Microsoft.Agents.AI\Microsoft.Agents.AI.csproj -f net10.0 --tl:off -> passed, 0 warnings/errors
  • dotnet run --project .\tests\Microsoft.Agents.AI.UnitTests\Microsoft.Agents.AI.UnitTests.csproj -f net10.0 -> passed, 1648 tests
  • git diff --check upstream/main..HEAD -> passed

@he-yufeng he-yufeng force-pushed the fix/service-history-provider-conflict branch 3 times, most recently from fdcc011 to f4db562 Compare June 12, 2026 16:56
@he-yufeng he-yufeng force-pushed the fix/service-history-provider-conflict branch from f4db562 to 2bd57bd Compare June 12, 2026 20:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

.NET: [Bug]: InMemoryChatHistoryProvider used even when service stores history (OpenAI Responses)

3 participants