.NET: Fix ChatOptions mutation in AIContextProviderChatClient across calls (#4891)

* Fix UseAIContextProviders tool accumulation across calls (#4864)

Clone ChatOptions before mutating it in InvokeProvidersAsync to prevent
context provider tools from accumulating when the same ChatOptions
instance is reused across multiple API calls.

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

* Apply suggestions from code review

Co-authored-by: chetantoshniwal <chetantoshniwal@gmail.com>

* Apply suggestion from @westey-m

---------

Co-authored-by: MAF Dashboard Bot <maf-dashboard-bot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: westey <164392973+westey-m@users.noreply.github.com>
This commit is contained in:
chetantoshniwal
2026-03-25 20:02:16 +00:00
committed by GitHub
co-authored by Copilot MAF Dashboard Bot westey
parent 23921c0f6e
commit 0b2ccd6126
2 changed files with 163 additions and 0 deletions
@@ -161,6 +161,8 @@ internal sealed class AIContextProviderChatClient : DelegatingChatClient
}
// Materialize the accumulated context back into messages and options.
// Clone options to avoid mutating the caller's instance across calls.
options = options?.Clone();
var enrichedMessages = aiContext.Messages ?? [];
var tools = aiContext.Tools as IList<AITool> ?? aiContext.Tools?.ToList();