@@ -13,6 +13,7 @@ import { saveCodexAccountCredential } from "../src/codex/account-store";
1313import {
1414 CODEX_QUOTA_PROBE_INTERVAL_MS ,
1515 clearCodexUpstreamHealth ,
16+ getCodexUpstreamHealth ,
1617 recordCodexUpstreamOutcome ,
1718} from "../src/codex/routing" ;
1819import {
@@ -581,6 +582,34 @@ describe("compact alternate-account attempt (#913)", () => {
581582 expect ( res . status ) . toBe ( 200 ) ;
582583 } ) ;
583584 } ) ;
585+
586+ test ( `the alternate after a ${ rejection } gets one send even when it returns a transient 5xx` , async ( ) => {
587+ // Activation proof for the two recovery modes. The first account keeps
588+ // fetchWithTransientRetry (up to three status attempts); the alternate must run
589+ // as a single direct send. Without `recovery: "single"` the 503 below would be
590+ // retried and the alternate's share of the send count would be three.
591+ await withPoolEnv ( `ocx-compact-alt-${ rejection } -5xx-` , async config => {
592+ const bearers : string [ ] = [ ] ;
593+ globalThis . fetch = ( async ( _url : string , init ?: RequestInit ) => {
594+ const auth = new Headers ( init ?. headers ) . get ( "authorization" ) ?? "" ;
595+ bearers . push ( auth ) ;
596+ if ( bearers . length === 1 ) {
597+ return Response . json ( { error : { message : "pool exhausted" } } , { status : rejection } ) ;
598+ }
599+ return Response . json ( { error : { message : "upstream busy" } } , { status : 503 } ) ;
600+ } ) as typeof fetch ;
601+
602+ const res = await handleResponsesCompact (
603+ compactionRequest ( baseCompactionBody ( { } ) ) ,
604+ config ,
605+ { model : "" , provider : "" } ,
606+ ) ;
607+
608+ expect ( bearers ) . toHaveLength ( 2 ) ;
609+ expect ( bearers [ 0 ] ) . not . toBe ( bearers [ 1 ] ) ;
610+ expect ( res . status ) . toBe ( 503 ) ;
611+ } ) ;
612+ } ) ;
584613 }
585614
586615 test ( "with no eligible alternate the first rejection is returned with its backoff headers" , async ( ) => {
@@ -674,4 +703,78 @@ describe("compact alternate-account attempt (#913)", () => {
674703 expect ( sends ) . toBe ( 1 ) ;
675704 } ) ;
676705 } ) ;
706+
707+ test ( "the alternate sends once even against a transient 5xx" , async ( ) => {
708+ // The two-mode crux. Compact's normal send wraps fetchWithTransientRetry, which
709+ // retries a 5xx up to three times. The alternate must NOT inherit that ladder:
710+ // it is a last bounded try, not a second retry stack. Without the mode split this
711+ // reads four sends (one from A, three from B's ladder).
712+ await withPoolEnv ( "ocx-compact-alt-single-" , async config => {
713+ let sends = 0 ;
714+ globalThis . fetch = ( async ( ) => {
715+ sends += 1 ;
716+ if ( sends === 1 ) {
717+ return Response . json ( { error : { message : "pool exhausted" } } , { status : 429 } ) ;
718+ }
719+ return Response . json ( { error : { message : "upstream flaked" } } , { status : 503 } ) ;
720+ } ) as typeof fetch ;
721+
722+ const res = await handleResponsesCompact (
723+ compactionRequest ( baseCompactionBody ( { } ) ) ,
724+ config ,
725+ { model : "" , provider : "" } ,
726+ ) ;
727+
728+ expect ( sends ) . toBe ( 2 ) ;
729+ expect ( res . status ) . toBe ( 503 ) ;
730+ } ) ;
731+ } ) ;
732+
733+ test ( "the first account keeps its transient-retry ladder" , async ( ) => {
734+ // The control for the test above: A's recovery is unchanged, so a transient 5xx
735+ // on A is still retried in place rather than treated as a reason to fail over.
736+ await withPoolEnv ( "ocx-compact-alt-ladder-" , async config => {
737+ let sends = 0 ;
738+ globalThis . fetch = ( async ( ) => {
739+ sends += 1 ;
740+ if ( sends === 1 ) return Response . json ( { error : { message : "flake" } } , { status : 503 } ) ;
741+ return jsonResponse ( completedPayload ( "recovered on retry" ) ) ;
742+ } ) as typeof fetch ;
743+
744+ const res = await handleResponsesCompact (
745+ compactionRequest ( baseCompactionBody ( { } ) ) ,
746+ config ,
747+ { model : "" , provider : "" } ,
748+ ) ;
749+
750+ // Two sends, both to A — a 503 is not 429/402, so no alternate is involved.
751+ expect ( sends ) . toBe ( 2 ) ;
752+ expect ( res . status ) . toBe ( 200 ) ;
753+ } ) ;
754+ } ) ;
755+
756+ test ( "each account's health records its own outcome" , async ( ) => {
757+ // Attribution: A's rejection belongs to A and B's belongs to B. Recording B's
758+ // outcome against A would soft-avoid the wrong account and defeat the failover.
759+ await withPoolEnv ( "ocx-compact-alt-attrib-" , async config => {
760+ let sends = 0 ;
761+ globalThis . fetch = ( async ( ) => {
762+ sends += 1 ;
763+ return sends === 1
764+ ? Response . json ( { error : { message : "a exhausted" } } , { status : 429 } )
765+ : Response . json ( { error : { message : "b rejected" } } , { status : 402 } ) ;
766+ } ) as typeof fetch ;
767+
768+ await handleResponsesCompact (
769+ compactionRequest ( baseCompactionBody ( { } ) ) ,
770+ config ,
771+ { model : "" , provider : "" } ,
772+ ) ;
773+
774+ const health = ( id : string ) => getCodexUpstreamHealth ( id ) as { lastFailureStatus ?: number } | null ;
775+ // Whichever account routing picked first carries the 429; the other carries B's 402.
776+ const statuses = [ "pool-a" , "pool-b" ] . map ( id => health ( id ) ?. lastFailureStatus ) . sort ( ) ;
777+ expect ( statuses ) . toEqual ( [ 402 , 429 ] ) ;
778+ } ) ;
779+ } ) ;
677780} ) ;
0 commit comments