Skip to content

Commit 2240252

Browse files
.NET: address review — pin the failure-path invariant and correct the rationale comment
Two review comments from Copilot on microsoft#8532. The sentinel comment asserted that clients "wait for the connection to drop, which reads as a hang or a timeout". Live testing disproved that: the response is chunked, so the zero-length terminating chunk ends the body definitively and the official OpenAI client completes without the sentinel. The comment now states the actual interoperability problem -- consumers that treat the sentinel rather than end-of-body as the completion signal cannot recognize the stream as finished -- and names the one this repo ships, SseResponseIdCapture. The failure-path invariant is now covered. An earlier attempt at this test was discarded because a client-side assertion has no teeth: when the agent throws, the server aborts and the client receives no body, so the test passed even against a try/finally refactor that writes the terminator unconditionally. This version tees the response body into a buffer through test middleware, so it observes what the server actually wrote. Verified to fail against that refactor. The failing chat client and the tee stream are private to the test file rather than added to the shared TestHelpers, keeping the change to one test file.
1 parent 28a6916 commit 2240252

2 files changed

Lines changed: 149 additions & 7 deletions

File tree

‎dotnet/src/Microsoft.Agents.AI.Hosting.OpenAI/ChatCompletions/AIAgentChatCompletionsProcessor.cs‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,11 @@ public Task ExecuteAsync(HttpContext httpContext)
182182
yield return new(chunk);
183183
}
184184

185-
// OpenAI-compatible clients detect completion from a final "data: [DONE]" frame and
186-
// otherwise wait for the connection to drop, which reads as a hang or a timeout.
185+
// The OpenAI wire format ends a streaming chat completion with a literal
186+
// "data: [DONE]" frame, and consumers that treat that sentinel -- rather than
187+
// end-of-body -- as the completion signal cannot recognize the stream as finished
188+
// without it. This repo ships one such consumer: SseResponseIdCapture in the DevUI
189+
// Aspire integration matches "[DONE]"u8 explicitly.
187190
//
188191
// Emitted after the loop so it only follows a stream that ran to completion: if the
189192
// agent throws, or the caller aborts the request, the exception propagates out of the

‎dotnet/tests/Microsoft.Agents.AI.Hosting.OpenAI.UnitTests/OpenAIChatCompletionsConformanceTests.cs‎

Lines changed: 144 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
11
// Copyright (c) Microsoft. All rights reserved.
22

3+
using System;
34
using System.Collections.Generic;
5+
using System.IO;
46
using System.Linq;
57
using System.Net.Http;
8+
using System.Runtime.CompilerServices;
9+
using System.Text;
610
using System.Text.Json;
11+
using System.Threading;
712
using System.Threading.Tasks;
13+
using Microsoft.Agents.AI;
814
using Microsoft.Agents.AI.Hosting.OpenAI.Tests;
15+
using Microsoft.AspNetCore.Builder;
16+
using Microsoft.AspNetCore.Hosting.Server;
17+
using Microsoft.AspNetCore.TestHost;
918
using Microsoft.Extensions.AI;
19+
using Microsoft.Extensions.DependencyInjection;
20+
using Microsoft.Extensions.Hosting;
1021

1122
namespace Microsoft.Agents.AI.Hosting.OpenAI.UnitTests;
1223

@@ -253,18 +264,18 @@ public async Task StreamingResponseEndsWithDoneSentinelAsync()
253264
// trimmed body would pass even if the final frame were left unterminated.
254265
Assert.Equal("text/event-stream", httpResponse.Content.Headers.ContentType?.MediaType);
255266
Assert.Contains("data: [DONE]", responseSse);
256-
Assert.EndsWith("data: [DONE]\n\n", responseSse, System.StringComparison.Ordinal);
267+
Assert.EndsWith("data: [DONE]\n\n", responseSse, StringComparison.Ordinal);
257268

258269
var dataLines = responseSse.Split('\n')
259270
.Select(line => line.TrimEnd('\r'))
260-
.Where(line => line.StartsWith("data: ", System.StringComparison.Ordinal))
271+
.Where(line => line.StartsWith("data: ", StringComparison.Ordinal))
261272
.ToList();
262273
Assert.Single(dataLines, line => line == "data: [DONE]");
263274

264275
// Assert - the terminator follows the payload chunks rather than replacing them, so a
265276
// client that stops reading at [DONE] still receives the whole completion.
266277
Assert.True(dataLines.Count > 1, "the stream should carry chat completion chunks before the terminator");
267-
Assert.All(dataLines[..^1], line => Assert.StartsWith("data: {", line, System.StringComparison.Ordinal));
278+
Assert.All(dataLines[..^1], line => Assert.StartsWith("data: {", line, StringComparison.Ordinal));
268279
}
269280

270281
[Fact]
@@ -380,7 +391,7 @@ public async Task SystemMessageRequestResponseAsync()
380391
AssertJsonPropertyEquals(systemMessage, "role", "system");
381392
AssertJsonPropertyExists(systemMessage, "content");
382393
string systemContent = systemMessage.GetProperty("content").GetString()!;
383-
Assert.Contains("pirate", systemContent, System.StringComparison.OrdinalIgnoreCase);
394+
Assert.Contains("pirate", systemContent, StringComparison.OrdinalIgnoreCase);
384395

385396
var userMessage = messages[1];
386397
AssertJsonPropertyEquals(userMessage, "role", "user");
@@ -635,7 +646,7 @@ private static List<JsonElement> ParseChatCompletionChunksFromSse(string sseCont
635646
{
636647
var line = lines[i].TrimEnd('\r');
637648

638-
if (line.StartsWith("data: ", System.StringComparison.Ordinal))
649+
if (line.StartsWith("data: ", StringComparison.Ordinal))
639650
{
640651
var jsonData = line.Substring("data: ".Length);
641652

@@ -659,4 +670,132 @@ private static List<JsonElement> ParseChatCompletionChunksFromSse(string sseCont
659670

660671
return chunks;
661672
}
673+
674+
[Fact]
675+
public async Task StreamingResponseOmitsDoneSentinelWhenAgentStreamFailsAsync()
676+
{
677+
// The sentinel must only ever follow a stream that ran to completion; a truncated response
678+
// must never be marked complete. Asserting that from the client does not work: when the
679+
// agent throws, the server aborts and the client receives no body at all, so a client-side
680+
// check passes even against a try/finally refactor that writes the terminator
681+
// unconditionally. This captures what the server actually wrote to the response body, which
682+
// does distinguish the two.
683+
684+
// Arrange - a host whose response body is tee'd into a buffer we can inspect afterwards
685+
var recorded = new MemoryStream();
686+
using var failingChatClient = new FailingStreamChatClient(updatesBeforeFailure: 2);
687+
688+
WebApplicationBuilder builder = WebApplication.CreateBuilder();
689+
builder.WebHost.UseTestServer();
690+
builder.Services.AddKeyedSingleton<IChatClient>("chat-client", failingChatClient);
691+
builder.AddAIAgent("failing-agent", "You are a helpful assistant.", chatClientServiceKey: "chat-client");
692+
builder.AddOpenAIChatCompletions();
693+
694+
await using WebApplication app = builder.Build();
695+
app.Use(async (context, next) =>
696+
{
697+
Stream original = context.Response.Body;
698+
context.Response.Body = new TeeStream(original, recorded);
699+
try
700+
{
701+
await next(context);
702+
}
703+
finally
704+
{
705+
context.Response.Body = original;
706+
}
707+
});
708+
AIAgent agent = app.Services.GetRequiredKeyedService<AIAgent>("failing-agent");
709+
app.MapOpenAIChatCompletions(agent, path: null, PermissiveMapOptions.ChatCompletions());
710+
await app.StartAsync();
711+
712+
TestServer testServer = (TestServer)app.Services.GetRequiredService<IServer>();
713+
using HttpClient client = testServer.CreateClient();
714+
715+
// Act - the agent yields two updates and then throws partway through the body
716+
try
717+
{
718+
using HttpResponseMessage response = await this.SendChatCompletionRequestAsync(client, "failing-agent", LoadChatCompletionsTraceFile("streaming/request.json"));
719+
_ = await response.Content.ReadAsStringAsync();
720+
}
721+
catch (Exception ex) when (ex is HttpRequestException or IOException or InvalidOperationException)
722+
{
723+
// Expected: the server aborts the response rather than completing it.
724+
}
725+
726+
await app.StopAsync();
727+
728+
// Assert - the server wrote payload frames but never the terminator
729+
string written = Encoding.UTF8.GetString(recorded.ToArray());
730+
Assert.Contains("chat.completion.chunk", written);
731+
Assert.DoesNotContain("[DONE]", written);
732+
}
733+
734+
/// <summary>A chat client whose streaming response yields a few updates and then throws.</summary>
735+
private sealed class FailingStreamChatClient(int updatesBeforeFailure) : IChatClient
736+
{
737+
public Task<ChatResponse> GetResponseAsync(IEnumerable<ChatMessage> messages, ChatOptions? options = null, CancellationToken cancellationToken = default)
738+
=> throw new InvalidOperationException("mock streaming failure");
739+
740+
public async IAsyncEnumerable<ChatResponseUpdate> GetStreamingResponseAsync(
741+
IEnumerable<ChatMessage> messages,
742+
ChatOptions? options = null,
743+
[EnumeratorCancellation] CancellationToken cancellationToken = default)
744+
{
745+
for (int i = 0; i < updatesBeforeFailure; i++)
746+
{
747+
await Task.Delay(1, cancellationToken);
748+
yield return new ChatResponseUpdate { Contents = [new TextContent($"chunk{i} ")], Role = ChatRole.Assistant };
749+
}
750+
751+
throw new InvalidOperationException("mock streaming failure");
752+
}
753+
754+
public object? GetService(Type serviceType, object? serviceKey = null) => serviceType.IsInstanceOfType(this) ? this : null;
755+
756+
public void Dispose()
757+
{
758+
}
759+
}
760+
761+
/// <summary>
762+
/// Forwards writes to the real response body while copying them into a buffer. The copy happens
763+
/// first so that bytes the server attempted to write are recorded even if the real write fails
764+
/// because the response is already being torn down.
765+
/// </summary>
766+
private sealed class TeeStream(Stream inner, Stream capture) : Stream
767+
{
768+
public override bool CanRead => false;
769+
public override bool CanSeek => false;
770+
public override bool CanWrite => true;
771+
public override long Length => throw new NotSupportedException();
772+
public override long Position { get => throw new NotSupportedException(); set => throw new NotSupportedException(); }
773+
774+
public override void Write(byte[] buffer, int offset, int count)
775+
{
776+
capture.Write(buffer, offset, count);
777+
inner.Write(buffer, offset, count);
778+
}
779+
780+
public override void Write(ReadOnlySpan<byte> buffer)
781+
{
782+
capture.Write(buffer);
783+
inner.Write(buffer);
784+
}
785+
786+
public override async ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken = default)
787+
{
788+
await capture.WriteAsync(buffer, cancellationToken).ConfigureAwait(false);
789+
await inner.WriteAsync(buffer, cancellationToken).ConfigureAwait(false);
790+
}
791+
792+
public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
793+
=> this.WriteAsync(buffer.AsMemory(offset, count), cancellationToken).AsTask();
794+
795+
public override void Flush() => inner.Flush();
796+
public override Task FlushAsync(CancellationToken cancellationToken) => inner.FlushAsync(cancellationToken);
797+
public override int Read(byte[] buffer, int offset, int count) => throw new NotSupportedException();
798+
public override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException();
799+
public override void SetLength(long value) => throw new NotSupportedException();
800+
}
662801
}

0 commit comments

Comments
 (0)