11// Copyright (c) Microsoft. All rights reserved.
22
3+ using System ;
34using System . Collections . Generic ;
5+ using System . IO ;
46using System . Linq ;
57using System . Net . Http ;
8+ using System . Runtime . CompilerServices ;
9+ using System . Text ;
610using System . Text . Json ;
11+ using System . Threading ;
712using System . Threading . Tasks ;
13+ using Microsoft . Agents . AI ;
814using Microsoft . Agents . AI . Hosting . OpenAI . Tests ;
15+ using Microsoft . AspNetCore . Builder ;
16+ using Microsoft . AspNetCore . Hosting . Server ;
17+ using Microsoft . AspNetCore . TestHost ;
918using Microsoft . Extensions . AI ;
19+ using Microsoft . Extensions . DependencyInjection ;
20+ using Microsoft . Extensions . Hosting ;
1021
1122namespace 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