@@ -282,6 +282,18 @@ export function startServer(port?: number) {
282282 // (0.0.0.0/::) and specific hosts are left untouched so intentional exposure is preserved.
283283 const bindHost = / ^ l o c a l h o s t $ / i. test ( config . hostname ?? "" ) ? "127.0.0.1" : ( config . hostname ?? "127.0.0.1" ) ;
284284
285+ // Codex treats empty / non-JSON 503 bodies as "Unknown error" (#452). Keep Retry-After and
286+ // the server_is_overloaded code so clients can back off, but always return a JSON envelope.
287+ function drainingResponse ( req : Request ) : Response {
288+ const response = formatErrorResponse ( 503 , "server_error" , "Service shutting down" ) ;
289+ const headers = new Headers ( response . headers ) ;
290+ for ( const [ name , value ] of Object . entries ( corsHeaders ( req , config ) ) ) {
291+ headers . set ( name , value ) ;
292+ }
293+ headers . set ( "Retry-After" , "5" ) ;
294+ return new Response ( response . body , { status : 503 , headers } ) ;
295+ }
296+
285297 const server : Server < WsData > = Bun . serve < WsData > ( {
286298 port : listenPort ,
287299 hostname : bindHost ,
@@ -301,7 +313,7 @@ export function startServer(port?: number) {
301313 // handshake-time only, so capture inbound headers and thread them into the pipeline.
302314 if ( url . pathname === "/v1/responses" && req . headers . get ( "upgrade" ) ?. toLowerCase ( ) === "websocket" ) {
303315 if ( isDraining ( ) ) {
304- return new Response ( "Service shutting down" , { status : 503 , headers : { ... corsHeaders ( req , config ) , "Retry-After" : "5" } } ) ;
316+ return drainingResponse ( req ) ;
305317 }
306318 const apiAuthError = requireResponsesApiAuth ( req , config ) ;
307319 if ( apiAuthError ) return withCors ( apiAuthError , req , config ) ;
@@ -406,7 +418,7 @@ export function startServer(port?: number) {
406418 // before the /v1/* 404 guard below.
407419 if ( url . pathname === "/v1/responses/compact" && req . method === "POST" ) {
408420 if ( isDraining ( ) ) {
409- return new Response ( "Service shutting down" , { status : 503 , headers : { ... corsHeaders ( req , config ) , "Retry-After" : "5" } } ) ;
421+ return drainingResponse ( req ) ;
410422 }
411423 const apiAuthError = requireResponsesApiAuth ( req , config ) ;
412424 if ( apiAuthError ) return withCors ( apiAuthError , req , config ) ;
@@ -438,10 +450,7 @@ export function startServer(port?: number) {
438450 ) {
439451 disableResponsesRequestTimeout ( req , requestServer ) ;
440452 if ( isDraining ( ) ) {
441- return new Response ( "Service shutting down" , {
442- status : 503 ,
443- headers : { ...corsHeaders ( req , config ) , "Retry-After" : "5" } ,
444- } ) ;
453+ return drainingResponse ( req ) ;
445454 }
446455 const apiAuthError = requireApiAuth ( req , config , "data-plane" ) ;
447456 if ( apiAuthError ) return withCors ( apiAuthError , req , config ) ;
@@ -460,10 +469,7 @@ export function startServer(port?: number) {
460469 if ( url . pathname === "/v1/alpha/search" && req . method === "POST" ) {
461470 disableResponsesRequestTimeout ( req , requestServer ) ;
462471 if ( isDraining ( ) ) {
463- return new Response ( "Service shutting down" , {
464- status : 503 ,
465- headers : { ...corsHeaders ( req , config ) , "Retry-After" : "5" } ,
466- } ) ;
472+ return drainingResponse ( req ) ;
467473 }
468474 const apiAuthError = requireApiAuth ( req , config , "data-plane" ) ;
469475 if ( apiAuthError ) return withCors ( apiAuthError , req , config ) ;
@@ -487,7 +493,7 @@ export function startServer(port?: number) {
487493 if ( url . pathname === "/v1/responses" && req . method === "POST" ) {
488494 disableResponsesRequestTimeout ( req , requestServer ) ;
489495 if ( isDraining ( ) ) {
490- return new Response ( "Service shutting down" , { status : 503 , headers : { ... corsHeaders ( req , config ) , "Retry-After" : "5" } } ) ;
496+ return drainingResponse ( req ) ;
491497 }
492498 const apiAuthError = requireResponsesApiAuth ( req , config ) ;
493499 if ( apiAuthError ) return withCors ( apiAuthError , req , config ) ;
@@ -526,7 +532,7 @@ export function startServer(port?: number) {
526532 // Claude Code posts `/v1/messages?beta=true` — pathname match ignores the query (003 G9).
527533 if ( url . pathname === "/v1/messages/count_tokens" && req . method === "POST" ) {
528534 if ( isDraining ( ) ) {
529- return new Response ( "Service shutting down" , { status : 503 , headers : { ... corsHeaders ( req , config ) , "Retry-After" : "5" } } ) ;
535+ return drainingResponse ( req ) ;
530536 }
531537 if ( ! hasValidApiAuth ( req , config ) ) {
532538 return withCors ( anthropicErrorResponse ( 401 , "opencodex API key required" , "authentication_error" ) , req , config ) ;
@@ -541,7 +547,7 @@ export function startServer(port?: number) {
541547 if ( url . pathname === "/v1/messages" && req . method === "POST" ) {
542548 disableResponsesRequestTimeout ( req , requestServer ) ;
543549 if ( isDraining ( ) ) {
544- return new Response ( "Service shutting down" , { status : 503 , headers : { ... corsHeaders ( req , config ) , "Retry-After" : "5" } } ) ;
550+ return drainingResponse ( req ) ;
545551 }
546552 if ( ! hasValidApiAuth ( req , config ) ) {
547553 return withCors ( anthropicErrorResponse ( 401 , "opencodex API key required" , "authentication_error" ) , req , config ) ;
@@ -564,7 +570,7 @@ export function startServer(port?: number) {
564570 if ( url . pathname === "/v1/chat/completions" && req . method === "POST" ) {
565571 disableResponsesRequestTimeout ( req , requestServer ) ;
566572 if ( isDraining ( ) ) {
567- return new Response ( "Service shutting down" , { status : 503 , headers : { ... corsHeaders ( req , config ) , "Retry-After" : "5" } } ) ;
573+ return drainingResponse ( req ) ;
568574 }
569575 const apiAuthError = requireResponsesApiAuth ( req , config ) ;
570576 if ( apiAuthError ) return withCors ( apiAuthError , req , config ) ;
@@ -587,10 +593,7 @@ export function startServer(port?: number) {
587593 ) {
588594 disableResponsesRequestTimeout ( req , requestServer ) ;
589595 if ( isDraining ( ) ) {
590- return new Response ( "Service shutting down" , {
591- status : 503 ,
592- headers : { ...corsHeaders ( req , config ) , "Retry-After" : "5" } ,
593- } ) ;
596+ return drainingResponse ( req ) ;
594597 }
595598 const apiAuthError = requireApiAuth ( req , config , "data-plane" ) ;
596599 if ( apiAuthError ) return withCors ( apiAuthError , req , config ) ;
@@ -618,10 +621,7 @@ export function startServer(port?: number) {
618621 : null ;
619622 if ( liveSidebandTarget ) {
620623 if ( isDraining ( ) ) {
621- return new Response ( "Service shutting down" , {
622- status : 503 ,
623- headers : { ...corsHeaders ( req , config ) , "Retry-After" : "5" } ,
624- } ) ;
624+ return drainingResponse ( req ) ;
625625 }
626626 const apiAuthError = requireApiAuth ( req , config , "data-plane" ) ;
627627 if ( apiAuthError ) return withCors ( apiAuthError , req , config ) ;
0 commit comments