@@ -113,4 +113,81 @@ describe("Responses abort guards", () => {
113113 process . off ( "unhandledRejection" , onUnhandledRejection ) ;
114114 }
115115 } ) ;
116+
117+ test ( "terminal-guard continuation body is cancelled on a late client abort (#394 review)" , async ( ) => {
118+ // The terminal guard opens a SECOND upstream response (the continuation). Its body must be
119+ // bound to the abort signal exactly like the initial response, or the fetch-to-reader race
120+ // (#390/366e3053) reopens on the continuation path. First turn: an anthropic end_turn with no
121+ // tool call (triggers exactly one continuation). Continuation: a body whose reader attaches
122+ // only after the client has aborted; cancelBodyOnAbort must cancel it.
123+ const clientAbort = new AbortController ( ) ;
124+ const unhandledRejections : unknown [ ] = [ ] ;
125+ const onUnhandledRejection = ( reason : unknown ) => { unhandledRejections . push ( reason ) ; } ;
126+ let continuationBodyCancelled = false ;
127+ let fetches = 0 ;
128+
129+ process . on ( "unhandledRejection" , onUnhandledRejection ) ;
130+ try {
131+ adapterFactory = provider => ( {
132+ name : "anthropic" ,
133+ buildRequest : ( ) => ( { url : provider . baseUrl , method : "POST" , headers : { } , body : "" } ) ,
134+ async fetchResponse ( ) {
135+ fetches += 1 ;
136+ if ( fetches === 1 ) {
137+ // First turn: a complete anthropic-style stream that ends without a tool call.
138+ return new Response ( new ReadableStream < Uint8Array > ( {
139+ start ( controller ) {
140+ controller . enqueue ( new TextEncoder ( ) . encode (
141+ 'event: message_start\ndata: {"type":"message_start","message":{"usage":{"input_tokens":10,"output_tokens":1}}}\n\n' +
142+ 'event: content_block_start\ndata: {"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}}\n\n' +
143+ 'event: content_block_delta\ndata: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"我接下来会修改相关文件。"}}\n\n' +
144+ 'event: content_block_stop\ndata: {"type":"content_block_stop","index":0}\n\n' +
145+ 'event: message_delta\ndata: {"type":"message_delta","delta":{"stop_reason":"end_turn"},"usage":{"output_tokens":2}}\n\n' +
146+ 'event: message_stop\ndata: {"type":"message_stop"}\n\n' ,
147+ ) ) ;
148+ controller . close ( ) ;
149+ } ,
150+ } ) , { status : 200 , headers : { "content-type" : "text/event-stream" } } ) ;
151+ }
152+ // Continuation turn: reader attaches only after the client aborts.
153+ return new Response ( new ReadableStream < Uint8Array > ( {
154+ start ( controller ) { controller . enqueue ( new Uint8Array ( [ 1 ] ) ) ; } ,
155+ cancel ( ) { continuationBodyCancelled = true ; } ,
156+ } ) , { status : 200 , headers : { "content-type" : "text/event-stream" } } ) ;
157+ } ,
158+ async * parseStream ( response ) : AsyncGenerator < AdapterEvent > {
159+ if ( fetches >= 2 ) {
160+ // We are now parsing the continuation stream — abort before attaching the reader.
161+ clientAbort . abort ( new DOMException ( "client disconnected" , "AbortError" ) ) ;
162+ await Promise . resolve ( ) ;
163+ const reader = response . body ! . getReader ( ) ;
164+ try { await reader . read ( ) ; } finally { reader . releaseLock ( ) ; }
165+ return ;
166+ }
167+ // First turn: emit an end_turn with no tool call so the guard opens a continuation.
168+ yield { type : "text_delta" , text : "我接下来会修改相关文件。" } ;
169+ yield { type : "done" , usage : { inputTokens : 10 , outputTokens : 2 } } ;
170+ } ,
171+ } ) ;
172+
173+ const response = await handleResponses ( new Request ( "http://localhost/v1/responses" , {
174+ method : "POST" ,
175+ headers : { "content-type" : "application/json" } ,
176+ body : JSON . stringify ( {
177+ model : "fixture/model" ,
178+ input : "请检查这个问题并修复代码" ,
179+ stream : true ,
180+ tools : [ { type : "function" , name : "exec_command" , description : "run a command" , parameters : { type : "object" } } ] ,
181+ } ) ,
182+ } ) , config ( "anthropic" ) , { model : "" , provider : "" } , { abortSignal : clientAbort . signal } ) ;
183+ await response . text ( ) . catch ( ( ) => { } ) ;
184+ await new Promise < void > ( resolve => setImmediate ( resolve ) ) ;
185+
186+ expect ( fetches ) . toBe ( 2 ) ;
187+ expect ( continuationBodyCancelled ) . toBe ( true ) ;
188+ expect ( unhandledRejections ) . toEqual ( [ ] ) ;
189+ } finally {
190+ process . off ( "unhandledRejection" , onUnhandledRejection ) ;
191+ }
192+ } ) ;
116193} ) ;
0 commit comments