@@ -250,6 +250,134 @@ describe("shadow call intercept request path (issue #311)", () => {
250250 } ) ;
251251} ) ;
252252
253+ /**
254+ * A shadow-call replacement naming a COMBO used to run exactly one attempt and never enter
255+ * the failover loop (#4129). Two cooperating causes: the combo gate reads the UN-rewritten
256+ * body, where the model is still the bare helper slug, and the late intercept resolved the
257+ * replacement through routeModel/tryPickComboModel, which collapses the combo table to a
258+ * single target while still tagging routeKind "combo" — so the reported "combo route, one
259+ * attempt" was a collapsed native pick, and 429/5xx hops (which only exist inside
260+ * handleComboResponses) were unreachable.
261+ */
262+ function comboInterceptConfig (
263+ targets : Array < { provider : string ; model : string } > ,
264+ shadowCallIntercept : Record < string , unknown > = { enabled : true , model : "combo/shadow" } ,
265+ ) : OcxConfig {
266+ return {
267+ port : 0 ,
268+ defaultProvider : "xai" ,
269+ providers : {
270+ xai : {
271+ adapter : "openai-chat" ,
272+ baseUrl : "https://api.x.ai/v1" ,
273+ authMode : "key" ,
274+ apiKey : "test-xai-key" ,
275+ } ,
276+ alt : {
277+ adapter : "openai-chat" ,
278+ baseUrl : "https://alt.example/v1" ,
279+ authMode : "key" ,
280+ apiKey : "test-alt-key" ,
281+ } ,
282+ } ,
283+ combos : {
284+ shadow : { strategy : "failover" , targets } ,
285+ } ,
286+ shadowCallIntercept,
287+ } as unknown as OcxConfig ;
288+ }
289+
290+ function chatOk ( text : string ) : Response {
291+ return Response . json ( {
292+ choices : [ { index : 0 , message : { role : "assistant" , content : text } , finish_reason : "stop" } ] ,
293+ usage : { prompt_tokens : 1 , completion_tokens : 1 } ,
294+ } ) ;
295+ }
296+
297+ describe ( "a combo shadow-call target enters the failover loop (#4129)" , ( ) => {
298+ test ( "a helper call rewritten to a combo hops past a 429 to the second target" , async ( ) => {
299+ const urls : string [ ] = [ ] ;
300+ const logCtx : RequestLogContext = { model : "" , provider : "" } ;
301+ globalThis . fetch = ( async ( url : unknown ) => {
302+ urls . push ( String ( url ) ) ;
303+ return urls . length === 1
304+ ? Response . json ( { error : { message : "rate limited" } } , { status : 429 } )
305+ : chatOk ( "ok" ) ;
306+ } ) as typeof fetch ;
307+
308+ const config = comboInterceptConfig ( [
309+ { provider : "xai" , model : "grok-4.5" } ,
310+ { provider : "alt" , model : "grok-4.5" } ,
311+ ] ) ;
312+ const response = await post ( config , "gpt-5.6-luna" , "turn" , logCtx ) ;
313+
314+ expect ( response . ok ) . toBe ( true ) ;
315+ // The whole point: two upstream attempts, in configured order.
316+ expect ( urls ) . toHaveLength ( 2 ) ;
317+ expect ( urls [ 0 ] ) . toContain ( "api.x.ai" ) ;
318+ expect ( urls [ 1 ] ) . toContain ( "alt.example" ) ;
319+ expect ( logCtx . provider ) . toBe ( "combo" ) ;
320+ expect ( logCtx . comboId ) . toBe ( "shadow" ) ;
321+ expect ( logCtx . routeDecision ?. routeKind ) . toBe ( "combo" ) ;
322+ expect ( logCtx . shadowCallRewrittenFrom ) . toBe ( "gpt-5.6-luna" ) ;
323+ const attempts = ( logCtx . attempts ?? [ ] ) as Array < { provider ?: string ; model ?: string } > ;
324+ expect ( attempts ) . toHaveLength ( 2 ) ;
325+ expect ( attempts . map ( a => `${ a . provider } /${ a . model } ` ) )
326+ . toEqual ( [ "xai/grok-4.5" , "alt/grok-4.5" ] ) ;
327+ } ) ;
328+
329+ test ( "a combo whose first target intersects the source still routes as a combo" , async ( ) => {
330+ const urls : string [ ] = [ ] ;
331+ const logCtx : RequestLogContext = { model : "" , provider : "" } ;
332+ globalThis . fetch = ( async ( url : unknown ) => {
333+ urls . push ( String ( url ) ) ;
334+ return chatOk ( "ok" ) ;
335+ } ) as typeof fetch ;
336+
337+ // The #2706 self-target shape: the source model routes to xai, and the combo's FIRST
338+ // target is that same provider+model. shadowCallTargetsIntersect is therefore true for
339+ // the collapsed one-candidate pick, which is what used to suppress the intercept
340+ // outright and leave the request on a plain native route.
341+ const config = comboInterceptConfig (
342+ [
343+ { provider : "xai" , model : "custom-helper" } ,
344+ { provider : "alt" , model : "grok-4.5" } ,
345+ ] ,
346+ { enabled : true , model : "combo/shadow" , sourceModels : [ "custom-helper" ] } ,
347+ ) ;
348+ const response = await post ( config , "custom-helper" , "turn" , logCtx ) ;
349+
350+ expect ( response . ok ) . toBe ( true ) ;
351+ // A healthy first target still costs exactly one upstream call.
352+ expect ( urls ) . toHaveLength ( 1 ) ;
353+ expect ( urls [ 0 ] ) . toContain ( "api.x.ai" ) ;
354+ expect ( logCtx . provider ) . toBe ( "combo" ) ;
355+ expect ( logCtx . comboId ) . toBe ( "shadow" ) ;
356+ expect ( logCtx . routeDecision ?. routeKind ) . toBe ( "combo" ) ;
357+ // Red before the fix: shouldInterceptShadowCall saw the collapsed pick as a self-target,
358+ // skipped the rewrite, and the request left as a plain native route with no marker.
359+ expect ( logCtx . shadowCallRewrittenFrom ) . toBe ( "custom-helper" ) ;
360+ } ) ;
361+
362+ test ( "a non-combo replacement still takes the ordinary late intercept" , async ( ) => {
363+ const urls : string [ ] = [ ] ;
364+ const logCtx : RequestLogContext = { model : "" , provider : "" } ;
365+ globalThis . fetch = ( async ( url : unknown ) => {
366+ urls . push ( String ( url ) ) ;
367+ return chatOk ( "ok" ) ;
368+ } ) as typeof fetch ;
369+
370+ const config = comboInterceptConfig ( [ { provider : "xai" , model : "grok-4.5" } ] ) ;
371+ config . shadowCallIntercept = { enabled : true , model : "xai/grok-4.5" } ;
372+ const response = await post ( config , "gpt-5.6-luna" , "turn" , logCtx ) ;
373+
374+ expect ( response . ok ) . toBe ( true ) ;
375+ expect ( urls ) . toHaveLength ( 1 ) ;
376+ expect ( logCtx . comboId ) . toBeUndefined ( ) ;
377+ expect ( logCtx . shadowCallRewrittenFrom ) . toBe ( "gpt-5.6-luna" ) ;
378+ } ) ;
379+ } ) ;
380+
253381/**
254382 * The GUI badge/tooltip used to hard-code "5.4-mini", so it kept naming a model
255383 * Codex no longer sends. The management API is the single source of truth for
0 commit comments