@@ -1829,12 +1829,17 @@ async function handleResponsesInner(
18291829 // (Bun#32111 JS-sink segfault — text frames pass, the terminal block is
18301830 // lost). The eager single reader applies the same rewrites inline.
18311831 const win32EagerRewrite = isWin32EagerRewrite ( process . platform , needsClientRewrite ) ;
1832+ // A native tee branch cannot be closed at a Responses protocol terminal
1833+ // while its upstream HTTP connection remains open. Windows therefore
1834+ // needs the existing single-reader relay even without payload rewrites;
1835+ // otherwise Codex keeps thinking after response.completed.
1836+ const win32TerminalRelay = process . platform === "win32" ;
18321837 const eagerPath = selectEagerPath (
18331838 process . platform ,
18341839 needsClientRewrite ,
18351840 config . streamMode ?? "auto" ,
18361841 ) ;
1837- if ( eagerPath ?. useEagerRelay || win32EagerRewrite ) {
1842+ if ( eagerPath ?. useEagerRelay || win32EagerRewrite || win32TerminalRelay ) {
18381843 const turnAc = new AbortController ( ) ;
18391844 linkAbortSignal ( upstream , turnAc . signal ) ;
18401845 registerTurn ( turnAc , options . turnAdmissionLease ) ;
@@ -1870,7 +1875,9 @@ async function handleResponsesInner(
18701875 inspectChunk : chunk => inspector . feed ( chunk ) ,
18711876 finishInspection : ( ) => inspector . finish ( ) ,
18721877 disposeInspection : ( ) => inspector . dispose ( ) ,
1873- sawTerminal : ( ) => inspector . reported ( ) ,
1878+ // Stream lifetime follows the protocol terminal even when this request
1879+ // has no outcome callback configured (reported() would stay false).
1880+ sawTerminal : ( ) => inspector . terminalSeen ( ) ,
18741881 ...( win32EagerRewrite
18751882 ? { rewritePayload : composeSsePayloadRewrites ( ...payloadRewrites ) }
18761883 : { } ) ,
@@ -1888,9 +1895,9 @@ async function handleResponsesInner(
18881895 onClientCancel : ( ) => options . onNativePassthroughCancel ?.( ) ,
18891896 onDone : ( ) => unregisterTurn ( turnAc ) ,
18901897 } , win32EagerRewrite ? { rewriteBudget : translatorBudget } : undefined ) ;
1891- // selectEagerPath admits only no-rewrite traffic on both eligible platforms;
1892- // win32 rewrite traffic reaches this relay too, but with the payload rewrite
1893- // applied inline — never via an image/item-id JS pull wrapper (#32111, #864).
1898+ // Windows always reaches this relay so response.completed can close a
1899+ // keep-alive upstream. Rewrite traffic applies its payload transform
1900+ // inline — never via the Bun#32111-unsafe tee()+JS- pull chain ( #864).
18941901 if ( ! headers . has ( "content-type" ) ) headers . set ( "content-type" , "text/event-stream" ) ;
18951902 return markEagerRelaySseResponse (
18961903 markNativePassthroughSseResponse ( new Response ( eagerBody , {
@@ -1957,15 +1964,13 @@ async function handleResponsesInner(
19571964 ) ;
19581965 }
19591966 if ( ! headers . has ( "content-type" ) ) headers . set ( "content-type" , "text/event-stream" ) ;
1960- // win32 must keep the pure native relay (Bun#32111 JS-sink segfault); elsewhere a JS pull
1961- // relay is established practice (relayWithAbort, relaySseWithHeartbeat) and lets a
1962- // mid-stream reset end with a clean response.failed terminal instead of a raw socket error .
1967+ // Windows was handled by the eager terminal-aware branch above. Remaining
1968+ // tee traffic can use the JS relay to close on a protocol terminal and to
1969+ // convert a mid-stream reset into a clean response.failed event .
19631970 const rewrittenBody = payloadRewrites . length > 0
19641971 ? relaySseWithPayloadRewrite ( nativeBody , composeSsePayloadRewrites ( ...payloadRewrites ) , translatorBudget )
19651972 : nativeBody ;
1966- const clientBody = process . platform === "win32" && ! needsClientRewrite
1967- ? nativeBody
1968- : relaySseWithFailedTail ( rewrittenBody , upstream , reason => clientGone . abort ( reason ) ) ;
1973+ const clientBody = relaySseWithFailedTail ( rewrittenBody , upstream , reason => clientGone . abort ( reason ) ) ;
19691974 return markNativePassthroughSseResponse ( new Response ( clientBody , {
19701975 status : upstreamResponse . status ,
19711976 headers,
0 commit comments