@@ -2,6 +2,7 @@ import { afterEach, describe, expect, jest, test } from "bun:test";
22import { providerFetch } from "../src/server/responses/fetch-helpers" ;
33import { handleResponses } from "../src/server/responses" ;
44import { isEagerRelaySseResponse } from "../src/server/relay" ;
5+ import { isWin32EagerRewrite } from "../src/lib/bun-stream-caps" ;
56import {
67 bunSupportsBoundedCodexWsRelay ,
78 codexWsUpstreamFetch as rawCodexWsUpstreamFetch ,
@@ -17,6 +18,17 @@ import type { OcxConfig } from "../src/types";
1718const CODEX_URL = "https://chatgpt.com/backend-api/codex/responses" ;
1819const BOUNDED_WS_RUNTIME = "1.4.0" ;
1920
21+ // #864 keeps win32 rewrite traffic out of the tee()+JS-pull chain, so
22+ // `isWin32EagerRewrite(platform, needsClientRewrite)` sends it through the eager
23+ // single-reader relay instead. Since the annotations backfill became an
24+ // unconditional block rewrite (5a75e57f, `createResponsesFieldBackfillBlockRewrite()`),
25+ // `needsClientRewrite` is true for every Responses stream — so on win32 the eager
26+ // relay marker is set no matter which upstream transport was chosen. Cases below
27+ // that are about *not* taking the WebSocket path assert that directly through
28+ // `FakeWebSocket.instances`; they hold the marker to this rule rather than to a
29+ // constant that only held before the backfill landed.
30+ const EAGER_RELAY_FORCED_BY_PLATFORM = isWin32EagerRewrite ( process . platform , true ) ;
31+
2032function shouldUseCodexWsUpstream ( url : string , init ?: RequestInit ) : boolean {
2133 return rawShouldUseCodexWsUpstream ( url , init , BOUNDED_WS_RUNTIME ) ;
2234}
@@ -288,7 +300,7 @@ describe("handleResponses Codex WS relay selection", () => {
288300 } ) ;
289301
290302 expect ( FakeWebSocket . instances ) . toHaveLength ( 1 ) ;
291- expect ( isEagerRelaySseResponse ( response ) ) . toBe ( false ) ;
303+ expect ( isEagerRelaySseResponse ( response ) ) . toBe ( EAGER_RELAY_FORCED_BY_PLATFORM ) ;
292304 expect ( await response . text ( ) ) . toContain ( "response.completed" ) ;
293305 } ) ;
294306
@@ -330,10 +342,70 @@ describe("handleResponses Codex WS relay selection", () => {
330342 const response = await handleResponses ( request ( ) , forwardConfig ( ) , { model : "" , provider : "" } ) ;
331343
332344 expect ( FakeWebSocket . instances ) . toHaveLength ( 0 ) ;
333- expect ( isEagerRelaySseResponse ( response ) ) . toBe ( false ) ;
345+ expect ( isEagerRelaySseResponse ( response ) ) . toBe ( EAGER_RELAY_FORCED_BY_PLATFORM ) ;
334346 expect ( await response . text ( ) ) . toContain ( "response.completed" ) ;
335347 } ,
336348 ) ;
349+
350+ // The two cases above assert the marker against `EAGER_RELAY_FORCED_BY_PLATFORM`,
351+ // which is only the right expectation while `needsClientRewrite` is genuinely
352+ // `true`. Rather than assert that through the rewrite factory — which would stay
353+ // green if `handleResponses` stopped registering it — this drives a real Responses
354+ // stream through the handler and reads the rewrite's own effect off the client
355+ // bytes. `createResponsesFieldBackfillBlockRewrite()` is the unconditional entry in
356+ // `blockRewrites`, so observing its transformation is what proves
357+ // `clientBlockRewrite !== undefined`, hence `needsClientRewrite === true`.
358+ test ( "the registered rewrite chain transforms the client stream, so needsClientRewrite is true" , async ( ) => {
359+ const upstreamEvent = {
360+ type : "response.completed" ,
361+ response : {
362+ id : "r-backfill" ,
363+ status : "completed" ,
364+ // Deliberately spec-non-compliant: `annotations` is required on
365+ // `output_text` and this upstream omits it. Only the backfill rewrite
366+ // puts it back.
367+ output : [ {
368+ type : "message" ,
369+ role : "assistant" ,
370+ content : [ { type : "output_text" , text : "hi" } ] ,
371+ } ] ,
372+ } ,
373+ } ;
374+ globalThis . fetch = ( async ( ) => new Response (
375+ `event: response.completed\ndata: ${ JSON . stringify ( upstreamEvent ) } \n\n` ,
376+ { status : 200 , headers : { "content-type" : "text/event-stream" } } ,
377+ ) ) as typeof fetch ;
378+
379+ const response = await handleResponses ( request ( ) , forwardConfig ( ) , { model : "" , provider : "" } ) ;
380+ const text = await response . text ( ) ;
381+
382+ const payload = text
383+ . split ( "\n" )
384+ . filter ( line => line . startsWith ( "data: " ) && line !== "data: [DONE]" )
385+ . map ( line => JSON . parse ( line . slice ( "data: " . length ) ) )
386+ . find ( event => event . type === "response.completed" ) ;
387+
388+ expect ( payload ) . toBeDefined ( ) ;
389+ // Absent on the wire, present to the client: the chain ran.
390+ expect ( payload . response . output [ 0 ] . content [ 0 ] ) . toHaveProperty ( "annotations" ) ;
391+ expect ( payload . response . output [ 0 ] . content [ 0 ] . annotations ) . toEqual ( [ ] ) ;
392+
393+ // And with the chain proven non-empty, the marker is exactly the win32 rule.
394+ expect ( isEagerRelaySseResponse ( response ) ) . toBe ( EAGER_RELAY_FORCED_BY_PLATFORM ) ;
395+ } ) ;
396+ } ) ;
397+
398+ describe ( "isWin32EagerRewrite" , ( ) => {
399+ test ( "marks rewrite traffic on win32 only" , ( ) => {
400+ // #864 is a win32-only Bun sink defect, so the rule must not widen to other
401+ // platforms, and must not fire when there is nothing to rewrite.
402+ expect ( isWin32EagerRewrite ( "win32" , true ) ) . toBe ( true ) ;
403+ expect ( isWin32EagerRewrite ( "win32" , false ) ) . toBe ( false ) ;
404+ expect ( isWin32EagerRewrite ( "darwin" , true ) ) . toBe ( false ) ;
405+ expect ( isWin32EagerRewrite ( "linux" , true ) ) . toBe ( false ) ;
406+
407+ expect ( EAGER_RELAY_FORCED_BY_PLATFORM ) . toBe ( process . platform === "win32" ) ;
408+ } ) ;
337409} ) ;
338410
339411describe ( "codexWsUpstreamFetch" , ( ) => {
0 commit comments