@@ -16,6 +16,7 @@ import { createWarnLogCapture } from "../logging/test-helpers/warn-log-capture.j
1616import { setCurrentPluginMetadataSnapshot } from "../plugins/current-plugin-metadata-snapshot.js" ;
1717import { clearCurrentPluginMetadataSnapshot } from "../plugins/current-plugin-metadata-state.js" ;
1818import { loadPluginMetadataSnapshot } from "../plugins/plugin-metadata-snapshot.js" ;
19+ import { GatewayDrainingError } from "../process/gateway-work-admission.js" ;
1920import { AgentRunTerminalOutcomeError } from "./agent-run-terminal-outcome.js" ;
2021import { AUTH_STORE_VERSION } from "./auth-profiles/constants.js" ;
2122import type { AuthProfileStore } from "./auth-profiles/types.js" ;
@@ -2080,6 +2081,60 @@ describe("runWithModelFallback", () => {
20802081 ) ;
20812082 } ) ;
20822083
2084+ it . each ( [
2085+ [ "direct" , ( ) => new GatewayDrainingError ( ) ] ,
2086+ [ "cause" , ( ) => new Error ( "session send failed" , { cause : new GatewayDrainingError ( ) } ) ] ,
2087+ [
2088+ "aggregate" ,
2089+ ( ) =>
2090+ new AggregateError (
2091+ [ new Error ( "cleanup failed" ) , new GatewayDrainingError ( ) ] ,
2092+ "agent run failed" ,
2093+ ) ,
2094+ ] ,
2095+ ] ) ( "aborts fallback on %s gateway drain failures" , async ( _label , makeError ) => {
2096+ const error = makeError ( ) ;
2097+ const run = vi . fn ( ) . mockRejectedValueOnce ( error ) . mockResolvedValueOnce ( "too late" ) ;
2098+ const onError = vi . fn ( ) ;
2099+ const onFallbackStep = vi . fn ( ) ;
2100+
2101+ await expect (
2102+ runWithModelFallback ( {
2103+ cfg : undefined ,
2104+ provider : "openai" ,
2105+ model : "gpt-5.6-sol" ,
2106+ fallbacksOverride : [ "openai/gpt-5.4-mini" ] ,
2107+ skipAuthProfileRuntime : true ,
2108+ run,
2109+ onError,
2110+ onFallbackStep,
2111+ } ) ,
2112+ ) . rejects . toBe ( error ) ;
2113+ expect ( run ) . toHaveBeenCalledTimes ( 1 ) ;
2114+ expect ( onError ) . not . toHaveBeenCalled ( ) ;
2115+ expect ( onFallbackStep ) . not . toHaveBeenCalled ( ) ;
2116+ } ) ;
2117+
2118+ it ( "still advances after a genuine provider rate limit" , async ( ) => {
2119+ const rateLimit = Object . assign ( new Error ( "rate limit exceeded" ) , { status : 429 } ) ;
2120+ const run = vi . fn ( ) . mockRejectedValueOnce ( rateLimit ) . mockResolvedValueOnce ( "fallback ok" ) ;
2121+
2122+ const result = await runWithModelFallback ( {
2123+ cfg : undefined ,
2124+ provider : "openai" ,
2125+ model : "gpt-5.6-sol" ,
2126+ fallbacksOverride : [ "openai/gpt-5.4-mini" ] ,
2127+ skipAuthProfileRuntime : true ,
2128+ run,
2129+ } ) ;
2130+
2131+ expect ( run ) . toHaveBeenCalledTimes ( 2 ) ;
2132+ expect ( result . result ) . toBe ( "fallback ok" ) ;
2133+ expect ( result . provider ) . toBe ( "openai" ) ;
2134+ expect ( result . model ) . toBe ( "gpt-5.4-mini" ) ;
2135+ expect ( result . attempts [ 0 ] ) . toMatchObject ( { reason : "rate_limit" , status : 429 } ) ;
2136+ } ) ;
2137+
20832138 it ( "aborts the fallback chain on transcript continuation failures without candidate_failed attribution" , async ( ) => {
20842139 const cfg = makeCfg ( {
20852140 agents : {
0 commit comments