From af957cf2a73af39d17f72734b66185a2b5e8856b Mon Sep 17 00:00:00 2001 From: ridlog <2539792752@qq.com> Date: Mon, 16 Nov 2026 23:14:59 +0800 Subject: [PATCH] fix(agents): keep completed tool turns out of the final history message The streaming agent copies completed assistant and tool messages from the chat service into its thread, but it also kept their text in the buffer used for the final message. Reset that buffer at each completed turn before appending the next update, so the next turn keeps its first token. Constraint: Preserve streamed updates and intermediate message callbacks Confidence: high Scope-risk: narrow Tested: Baseline regression 2 failed/2 passed; fixed Agent project 508 passed/1 skipped; dotnet format verification Not-tested: Credentialed live provider integration tests and the full repository platform matrix Related: https://github.com/microsoft/semantic-kernel/issues/12485 --- dotnet/src/Agents/Core/ChatCompletionAgent.cs | 5 +- .../Core/ChatCompletionAgentTests.cs | 89 +++++++++++++++++++ 2 files changed, 92 insertions(+), 2 deletions(-) diff --git a/dotnet/src/Agents/Core/ChatCompletionAgent.cs b/dotnet/src/Agents/Core/ChatCompletionAgent.cs index b02ee66cb4b8..c5677cb73789 100644 --- a/dotnet/src/Agents/Core/ChatCompletionAgent.cs +++ b/dotnet/src/Agents/Core/ChatCompletionAgent.cs @@ -423,11 +423,11 @@ private async IAsyncEnumerable InternalInvokeStream message.Role ??= AuthorRole.Assistant; message.AuthorName = this.Name; - builder.Append(message.ToString()); - // Capture mutated messages related function calling / tools for (; messageIndex < chat.Count; messageIndex++) { + // The completed turn is already stored below; only accumulate text from the next turn. + builder.Clear(); ChatMessageContent chatMessage = chat[messageIndex]; chatMessage.AuthorName = this.Name; @@ -436,6 +436,7 @@ private async IAsyncEnumerable InternalInvokeStream history.Add(chatMessage); } + builder.Append(message.ToString()); streamedContents?.Add(message); yield return message; } diff --git a/dotnet/src/Agents/UnitTests/Core/ChatCompletionAgentTests.cs b/dotnet/src/Agents/UnitTests/Core/ChatCompletionAgentTests.cs index def132c60102..483aa94019a7 100644 --- a/dotnet/src/Agents/UnitTests/Core/ChatCompletionAgentTests.cs +++ b/dotnet/src/Agents/UnitTests/Core/ChatCompletionAgentTests.cs @@ -298,6 +298,95 @@ public async Task VerifyChatCompletionAgentStreamingAsync() Times.Once); } + /// + /// Verify that completed tool turns are not repeated in the final streamed message stored in the thread. + /// + [Theory] + [InlineData(0, false)] + [InlineData(1, false)] + [InlineData(2, false)] + [InlineData(1, true)] + public async Task VerifyChatCompletionAgentStreamingHistoryAcrossToolTurnsAsync(int toolTurns, bool terminate) + { + List intermediateMessages = []; + List toolMessages = []; + List serviceUpdates = []; + Mock mockService = new(); + mockService.Setup( + s => s.GetStreamingChatMessageContentsAsync( + It.IsAny(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns((history, _, _, _) => StreamResponses(history)); + + ChatCompletionAgent agent = new() { Name = "TestAgent", Kernel = CreateKernel(mockService.Object) }; + ChatHistoryAgentThread thread = new(); + ChatMessageContent userMessage = new(AuthorRole.User, "Use the tool, then answer."); + AgentInvokeOptions options = new() + { + OnIntermediateMessage = message => + { + intermediateMessages.Add(message); + return Task.CompletedTask; + } + }; + + var updates = await agent.InvokeStreamingAsync([userMessage], thread, options).ToArrayAsync(); + var history = await thread.GetMessagesAsync().ToArrayAsync(); + + Assert.Equal(serviceUpdates, updates.Select(update => update.Message)); + Assert.Equal( + string.Concat(Enumerable.Range(0, toolTurns).Select(turn => $"Planning {turn}.")) + (terminate ? "Result 0." : "Final answer."), + string.Concat(updates.Select(update => update.Message.Content))); + Assert.Equal((2 * toolTurns) + (terminate ? 0 : 1), intermediateMessages.Count); + Assert.Equal(intermediateMessages.Count + 1, history.Length); + Assert.Same(userMessage, history[0]); + Assert.Equal(toolMessages, intermediateMessages.Take(2 * toolTurns)); + Assert.All(intermediateMessages, message => Assert.Equal(agent.Name, message.AuthorName)); + Assert.Equal(intermediateMessages, history.Skip(1)); + if (!terminate) + { + Assert.Equal("Final answer.", history[^1].Content); + Assert.Equal(AuthorRole.Assistant, history[^1].Role); + } + + async IAsyncEnumerable StreamResponses(ChatHistory chat) + { + for (int turn = 0; turn < toolTurns; turn++) + { + StreamingChatMessageContent planning = new(AuthorRole.Assistant, $"Planning {turn}."); + serviceUpdates.Add(planning); + yield return planning; + + StreamingChatMessageContent toolUpdate = new(AuthorRole.Assistant, content: null); + toolUpdate.Items.Add(new StreamingFunctionCallUpdateContent(callId: $"call-{turn}", name: "tools-test")); + serviceUpdates.Add(toolUpdate); + yield return toolUpdate; + + // The connector stores a completed assistant/tool pair before yielding the next model turn. + ChatMessageContent assistant = new(AuthorRole.Assistant, planning.Content); + Microsoft.SemanticKernel.FunctionCallContent call = new("test", "tools", $"call-{turn}"); + assistant.Items.Add(call); + ChatMessageContent result = new(AuthorRole.Tool, $"Result {turn}."); + result.Items.Add(new Microsoft.SemanticKernel.FunctionResultContent(call, $"Result {turn}.")); + chat.Add(assistant); + chat.Add(result); + toolMessages.Add(assistant); + toolMessages.Add(result); + await Task.Yield(); + } + + string[] finalParts = terminate ? ["Result 0."] : ["Final ", "answer."]; + foreach (string part in finalParts) + { + StreamingChatMessageContent update = new(terminate ? AuthorRole.Tool : AuthorRole.Assistant, part); + serviceUpdates.Add(update); + yield return update; + } + } + } + /// /// Verify the streaming invocation and response of . ///