@@ -72,6 +72,23 @@ describe("formatPassthroughUpstreamError (#452)", () => {
7272 expect ( json . error ?. message ?. toLowerCase ( ) ) . not . toBe ( "unknown error" ) ;
7373 } ) ;
7474
75+ test ( "empty body preserves validated Retry-After and forces application/json" , async ( ) => {
76+ const headers = new Headers ( { "retry-after" : "12" , "x-other" : "drop-me" } ) ;
77+ const response = formatPassthroughUpstreamError ( 429 , "" , { headers } ) ;
78+ expect ( response . status ) . toBe ( 429 ) ;
79+ expect ( response . headers . get ( "content-type" ) ) . toContain ( "application/json" ) ;
80+ expect ( response . headers . get ( "retry-after" ) ) . toBe ( "12" ) ;
81+ expect ( response . headers . get ( "x-other" ) ) . toBeNull ( ) ;
82+ } ) ;
83+
84+ test ( "empty body drops invalid Retry-After values" , async ( ) => {
85+ for ( const bad of [ "" , "nope" , "-1" , "0" , "1e6" , "not-a-delay" ] ) {
86+ const headers = new Headers ( { "retry-after" : bad } ) ;
87+ const response = formatPassthroughUpstreamError ( 503 , "" , { headers, now : Date . now ( ) } ) ;
88+ expect ( response . headers . get ( "retry-after" ) ) . toBeNull ( ) ;
89+ }
90+ } ) ;
91+
7592 test ( "JSON with error.message is preserved for Codex" , async ( ) => {
7693 const body = JSON . stringify ( { error : { message : "no healthy upstream" , type : "server_error" } } ) ;
7794 const response = formatPassthroughUpstreamError ( 503 , body ) ;
@@ -90,70 +107,134 @@ describe("formatPassthroughUpstreamError (#452)", () => {
90107 } ) ;
91108} ) ;
92109
110+ async function withPoolPassthrough (
111+ reply : ( request : Request ) => Response | Promise < Response > ,
112+ run : ( serverUrl : string ) => Promise < void > ,
113+ ) : Promise < void > {
114+ if ( existsSync ( TEST_DIR ) ) rmSync ( TEST_DIR , { recursive : true , force : true } ) ;
115+ mkdirSync ( TEST_DIR , { recursive : true } ) ;
116+ process . env . OPENCODEX_HOME = TEST_DIR ;
117+ delete process . env . OPENCODEX_API_AUTH_TOKEN ;
118+ clearCodexUpstreamHealth ( ) ;
119+ clearThreadAccountMap ( ) ;
120+ clearAccountQuota ( ) ;
121+ clearAccountNeedsReauth ( "pool-a" ) ;
122+
123+ const upstream = Bun . serve ( {
124+ port : 0 ,
125+ fetch ( request ) {
126+ return reply ( request ) ;
127+ } ,
128+ } ) ;
129+ redirectCanonicalCodexTo ( upstream . url . toString ( ) ) ;
130+
131+ saveConfig ( {
132+ port : 0 ,
133+ defaultProvider : "openai" ,
134+ openaiProviderTierVersion : 2 ,
135+ providers : {
136+ openai : {
137+ adapter : "openai-responses" ,
138+ baseUrl : "https://chatgpt.com/backend-api/codex" ,
139+ authMode : "forward" ,
140+ codexAccountMode : "pool" ,
141+ } ,
142+ } ,
143+ codexAccounts : [
144+ { id :
"main" , email :
"[email protected] " , isMain :
true } , 145+ { id :
"pool-a" , email :
"[email protected] " , isMain :
false , chatgptAccountId :
"acct-pool-a" } , 146+ ] ,
147+ activeCodexAccountId : "pool-a" ,
148+ } as OcxConfig ) ;
149+ saveCodexAccountCredential ( "pool-a" , {
150+ accessToken : "pool-a-token" ,
151+ refreshToken : "pool-a-refresh" ,
152+ expiresAt : Date . now ( ) + 10 * 60_000 ,
153+ chatgptAccountId : "acct-pool-a" ,
154+ } ) ;
155+ updateAccountQuota ( "pool-a" , 10 ) ;
156+
157+ const server = startServer ( 0 ) ;
158+ try {
159+ await run ( server . url . toString ( ) ) ;
160+ } finally {
161+ await server . stop ( true ) ;
162+ await upstream . stop ( true ) ;
163+ }
164+ }
165+
93166describe ( "passthrough empty 503 (#452)" , ( ) => {
94167 test ( "ChatGPT passthrough empty-body 503 becomes JSON Codex can parse" , async ( ) => {
95- if ( existsSync ( TEST_DIR ) ) rmSync ( TEST_DIR , { recursive : true , force : true } ) ;
96- mkdirSync ( TEST_DIR , { recursive : true } ) ;
97- process . env . OPENCODEX_HOME = TEST_DIR ;
98- delete process . env . OPENCODEX_API_AUTH_TOKEN ;
99- clearCodexUpstreamHealth ( ) ;
100- clearThreadAccountMap ( ) ;
101- clearAccountQuota ( ) ;
102- clearAccountNeedsReauth ( "pool-a" ) ;
103-
104- const upstream = Bun . serve ( {
105- port : 0 ,
106- fetch ( ) {
107- // Codex shows "Unknown error" when the body is empty — the historical passthrough bug.
108- return new Response ( null , { status : 503 } ) ;
168+ await withPoolPassthrough (
169+ ( ) => new Response ( null , { status : 503 } ) ,
170+ async ( serverUrl ) => {
171+ const response = await originalGlobalFetch ( new URL ( "/v1/responses" , serverUrl ) , {
172+ method : "POST" ,
173+ headers : { "content-type" : "application/json" , authorization : "Bearer inbound-token" } ,
174+ body : JSON . stringify ( { model : "gpt-5.6-sol" , input : "hi" , stream : false } ) ,
175+ } ) ;
176+ expect ( response . status ) . toBe ( 503 ) ;
177+ const text = await response . text ( ) ;
178+ expect ( text . trim ( ) . length ) . toBeGreaterThan ( 0 ) ;
179+ const json = JSON . parse ( text ) as { error ?: { message ?: string } } ;
180+ expect ( typeof json . error ?. message ) . toBe ( "string" ) ;
181+ expect ( json . error ! . message ! . trim ( ) . length ) . toBeGreaterThan ( 0 ) ;
182+ expect ( json . error ! . message ! . toLowerCase ( ) ) . not . toBe ( "unknown error" ) ;
109183 } ,
110- } ) ;
111- redirectCanonicalCodexTo ( upstream . url . toString ( ) ) ;
184+ ) ;
185+ } ) ;
112186
113- saveConfig ( {
114- port : 0 ,
115- defaultProvider : "openai" ,
116- openaiProviderTierVersion : 2 ,
117- providers : {
118- openai : {
119- adapter : "openai-responses" ,
120- baseUrl : "https://chatgpt.com/backend-api/codex" ,
121- authMode : "forward" ,
122- codexAccountMode : "pool" ,
187+ test ( "direct /v1/responses preserves Retry-After on empty-body 429 and 503" , async ( ) => {
188+ for ( const status of [ 429 , 503 ] as const ) {
189+ await withPoolPassthrough (
190+ ( ) => new Response ( null , { status, headers : { "Retry-After" : "1" } } ) ,
191+ async ( serverUrl ) => {
192+ const response = await originalGlobalFetch ( new URL ( "/v1/responses" , serverUrl ) , {
193+ method : "POST" ,
194+ headers : { "content-type" : "application/json" , authorization : "Bearer inbound-token" } ,
195+ body : JSON . stringify ( { model : "gpt-5.6-sol" , input : "hi" , stream : false } ) ,
196+ } ) ;
197+ expect ( response . status ) . toBe ( status ) ;
198+ expect ( response . headers . get ( "content-type" ) ) . toContain ( "application/json" ) ;
199+ expect ( response . headers . get ( "retry-after" ) ) . toBe ( "1" ) ;
123200 } ,
201+ ) ;
202+ }
203+ } ) ;
204+
205+ test ( "direct /v1/responses drops invalid Retry-After on empty-body 503" , async ( ) => {
206+ await withPoolPassthrough (
207+ ( ) => new Response ( null , { status : 503 , headers : { "Retry-After" : "not-a-delay" } } ) ,
208+ async ( serverUrl ) => {
209+ const response = await originalGlobalFetch ( new URL ( "/v1/responses" , serverUrl ) , {
210+ method : "POST" ,
211+ headers : { "content-type" : "application/json" , authorization : "Bearer inbound-token" } ,
212+ body : JSON . stringify ( { model : "gpt-5.6-sol" , input : "hi" , stream : false } ) ,
213+ } ) ;
214+ expect ( response . status ) . toBe ( 503 ) ;
215+ expect ( response . headers . get ( "retry-after" ) ) . toBeNull ( ) ;
124216 } ,
125- codexAccounts : [
126- { id :
"main" , email :
"[email protected] " , isMain :
true } , 127- { id :
"pool-a" , email :
"[email protected] " , isMain :
false , chatgptAccountId :
"acct-pool-a" } , 128- ] ,
129- activeCodexAccountId : "pool-a" ,
130- } as OcxConfig ) ;
131- saveCodexAccountCredential ( "pool-a" , {
132- accessToken : "pool-a-token" ,
133- refreshToken : "pool-a-refresh" ,
134- expiresAt : Date . now ( ) + 10 * 60_000 ,
135- chatgptAccountId : "acct-pool-a" ,
136- } ) ;
137- updateAccountQuota ( "pool-a" , 10 ) ;
217+ ) ;
218+ } ) ;
138219
139- const server = startServer ( 0 ) ;
140- try {
141- const response = await originalGlobalFetch ( new URL ( "/v1/responses" , server . url ) , {
142- method : "POST" ,
143- headers : { "content-type" : "application/json ", authorization : "Bearer inbound-token" } ,
144- body : JSON . stringify ( { model : "gpt-5.6-sol" , input : "hi" , stream : false } ) ,
145- } ) ;
146- expect ( response . status ) . toBe ( 503 ) ;
147- const text = await response . text ( ) ;
148- expect ( text . trim ( ) . length ) . toBeGreaterThan ( 0 ) ;
149- const json = JSON . parse ( text ) as { error ?: { message ?: string } } ;
150- expect ( typeof json . error ?. message ) . toBe ( "string" ) ;
151- expect ( json . error ! . message ! . trim ( ) . length ) . toBeGreaterThan ( 0 ) ;
152- expect ( json . error ! . message ! . toLowerCase ( ) ) . not . toBe ( "unknown error" ) ;
153- } finally {
154- await server . stop ( true ) ;
155- await upstream . stop ( true ) ;
156- }
220+ test ( "/v1/chat/completions forwards Retry-After from empty-body upstream 429" , async ( ) => {
221+ await withPoolPassthrough (
222+ ( ) => new Response ( null , { status : 429 , headers : { "Retry-After" : "3" } } ) ,
223+ async ( serverUrl ) => {
224+ const response = await originalGlobalFetch ( new URL ( "/v1/chat/completions ", serverUrl ) , {
225+ method : "POST" ,
226+ headers : { "content-type" : "application/json" , authorization : "Bearer inbound-token" } ,
227+ body : JSON . stringify ( {
228+ model : "gpt-5.6-sol" ,
229+ messages : [ { role : "user" , content : "hi" } ] ,
230+ stream : false ,
231+ } ) ,
232+ } ) ;
233+ expect ( response . status ) . toBe ( 429 ) ;
234+ expect ( response . headers . get ( "content-type" ) ) . toContain ( "application/json" ) ;
235+ expect ( response . headers . get ( "retry-after" ) ) . toBe ( "3" ) ;
236+ } ,
237+ ) ;
157238 } ) ;
158239} ) ;
159240
@@ -217,6 +298,35 @@ describe("openai-chat provider debug (#452)", () => {
217298 adapter . buildRequest ( parsed ) ;
218299 const lines = getDebugLogEntries ( ) . map ( e => e . line ) ;
219300 expect ( lines . some ( line => line . includes ( "[ocx:openai-chat:request]" ) ) ) . toBe ( true ) ;
301+ expect ( lines . join ( "\n" ) ) . toContain ( '"host":"api.xiaomimimo.com"' ) ;
220302 expect ( lines . join ( "\n" ) ) . not . toContain ( "sk-secret-xiaomi-key" ) ;
303+ expect ( lines . join ( "\n" ) ) . not . toContain ( "/v1/chat/completions" ) ;
304+ } ) ;
305+
306+ test ( "tenant-scoped baseUrl logs host only — account id never appears" , ( ) => {
307+ process . env . OCX_DEBUG = "1" ;
308+ resetDebugLogBufferForTests ( ) ;
309+ const accountId = "cf-account-abc123secret" ;
310+ const adapter = createOpenAIChatAdapter ( {
311+ adapter : "openai-chat" ,
312+ baseUrl : `https://api.cloudflare.com/client/v4/accounts/${ accountId } /ai/v1` ,
313+ apiKey : "cf-key-should-not-appear" ,
314+ } ) ;
315+ const parsed = {
316+ modelId : "@cf/meta/llama-3.3-70b-instruct-fp8-fast" ,
317+ stream : false ,
318+ context : {
319+ messages : [ { role : "user" , content : [ { type : "text" , text : "hi" } ] } ] ,
320+ } ,
321+ options : { } ,
322+ } as unknown as OcxParsedRequest ;
323+ adapter . buildRequest ( parsed ) ;
324+ const joined = getDebugLogEntries ( ) . map ( e => e . line ) . join ( "\n" ) ;
325+ expect ( joined ) . toContain ( "[ocx:openai-chat:request]" ) ;
326+ expect ( joined ) . toContain ( '"host":"api.cloudflare.com"' ) ;
327+ expect ( joined ) . not . toContain ( accountId ) ;
328+ expect ( joined ) . not . toContain ( "/accounts/" ) ;
329+ expect ( joined ) . not . toContain ( "cf-key-should-not-appear" ) ;
330+ expect ( joined ) . not . toContain ( "/ai/v1" ) ;
221331 } ) ;
222332} ) ;
0 commit comments