@@ -1906,6 +1906,107 @@ describe("codex account selection order", () => {
19061906 expect ( resolveCodexAccountForThread ( threadId , config , now + 6 , "shared" ) ) . toBe ( "b" ) ;
19071907 } ) ;
19081908
1909+ test ( "quota detour re-evaluation skips failover-ready cooler candidates" , ( ) => {
1910+ const now = 1_800_000_000_000 ;
1911+ const threadId = "quota-detour-failover-candidate" ;
1912+ const modelId = "gpt-daybreak-blue-latest" ;
1913+ const config = makeConfig ( {
1914+ accountPoolStrategy : "quota" ,
1915+ activeCodexAccountId : "c" ,
1916+ codexAccounts : [
1917+ { id : "a" , email : "a@test" , isMain : false } ,
1918+ { id : "b" , email : "b@test" , isMain : false } ,
1919+ { id : "c" , email : "c@test" , isMain : false } ,
1920+ ] ,
1921+ } ) ;
1922+ saveTestCredential ( "c" ) ;
1923+ updateAccountQuota ( "a" , 10 ) ;
1924+ updateAccountQuota ( "b" , 5 ) ;
1925+ updateAccountQuota ( "c" , 10 ) ;
1926+ resetCodexRoutingForManualSelection ( "c" ) ;
1927+ expect ( resolveCodexAccountForThread ( threadId , config , now , "shared" ) ) . toBe ( "c" ) ;
1928+ expect ( resolveCodexAccountForThreadDetailed (
1929+ threadId ,
1930+ config ,
1931+ now + 1 ,
1932+ "shared" ,
1933+ { modelEligibleAccountIds : new Set ( [ "a" ] ) } ,
1934+ modelId ,
1935+ ) ) . toEqual ( { status : "selected" , accountId : "a" } ) ;
1936+ // B is the highest tier after the detour exists. Filtering only after tier
1937+ // selection would drop B without ever exposing healthy C to the picker.
1938+ config . codexAccountPriorities = { b : 2 , c : 1 } ;
1939+
1940+ for ( let attempt = 0 ; attempt < 3 ; attempt += 1 ) {
1941+ recordCodexUpstreamOutcome ( config , "b" , 503 , {
1942+ fixedAccount : true ,
1943+ now : now + attempt + 2 ,
1944+ } ) ;
1945+ }
1946+ updateAccountQuota ( "a" , 90 ) ;
1947+ const resolveAt = now + CODEX_TRANSIENT_SOFT_AVOID_MS + 5 ;
1948+ const eligible = { modelEligibleAccountIds : new Set ( [ "a" , "b" , "c" ] ) } ;
1949+
1950+ expect ( previewCodexAccountForRequest (
1951+ threadId ,
1952+ config ,
1953+ resolveAt ,
1954+ "shared" ,
1955+ eligible ,
1956+ modelId ,
1957+ ) ) . toBe ( "c" ) ;
1958+ expect ( resolveCodexAccountForThreadDetailed (
1959+ threadId ,
1960+ config ,
1961+ resolveAt ,
1962+ "shared" ,
1963+ eligible ,
1964+ modelId ,
1965+ ) ) . toEqual ( { status : "selected" , accountId : "c" } ) ;
1966+ expect ( config . activeCodexAccountId ) . toBe ( "c" ) ;
1967+ expect ( config . activeCodexAccountPinned ) . toBeUndefined ( ) ;
1968+ expect ( getEffectiveActiveCodexAccountId ( config ) ) . toBe ( "c" ) ;
1969+ } ) ;
1970+
1971+ test ( "ordinary quota affinity re-evaluation skips a failover-ready higher tier" , ( ) => {
1972+ const now = 1_800_000_000_000 ;
1973+ const threadId = "ordinary-quota-failover-candidate" ;
1974+ const config = makeConfig ( {
1975+ accountPoolStrategy : "quota" ,
1976+ activeCodexAccountId : "a" ,
1977+ codexAccounts : [
1978+ { id : "a" , email : "a@test" , isMain : false } ,
1979+ { id : "b" , email : "b@test" , isMain : false } ,
1980+ { id : "c" , email : "c@test" , isMain : false } ,
1981+ ] ,
1982+ } ) ;
1983+ saveTestCredential ( "c" ) ;
1984+ updateAccountQuota ( "a" , 10 ) ;
1985+ updateAccountQuota ( "b" , 5 ) ;
1986+ updateAccountQuota ( "c" , 10 ) ;
1987+ expect ( resolveCodexAccountForThread ( threadId , config , now , "shared" ) ) . toBe ( "a" ) ;
1988+ config . codexAccountPriorities = { b : 2 , c : 1 } ;
1989+
1990+ for ( let attempt = 0 ; attempt < 3 ; attempt += 1 ) {
1991+ recordCodexUpstreamOutcome ( config , "b" , 503 , {
1992+ fixedAccount : true ,
1993+ now : now + attempt + 1 ,
1994+ } ) ;
1995+ }
1996+ updateAccountQuota ( "a" , 90 ) ;
1997+ const resolveAt = now + CODEX_TRANSIENT_SOFT_AVOID_MS + 4 ;
1998+
1999+ expect ( previewCodexAccountForRequest ( threadId , config , resolveAt , "shared" ) ) . toBe ( "c" ) ;
2000+ expect ( resolveCodexAccountForThreadDetailed (
2001+ threadId ,
2002+ config ,
2003+ resolveAt ,
2004+ "shared" ,
2005+ ) ) . toEqual ( { status : "selected" , accountId : "c" } ) ;
2006+ expect ( config . activeCodexAccountId ) . toBe ( "c" ) ;
2007+ expect ( getEffectiveActiveCodexAccountId ( config ) ) . toBe ( "c" ) ;
2008+ } ) ;
2009+
19092010 test ( "model preview and final keep a live detour after ordinary affinity cleanup" , ( ) => {
19102011 const now = 1_800_000_000_000 ;
19112012 const threadId = "detour-after-ordinary-cleanup" ;
0 commit comments