@@ -913,6 +913,94 @@ describe("google transport stream", () => {
913913 expect ( new Headers ( guardedInit . headers ) . has ( "x-goog-api-key" ) ) . toBe ( false ) ;
914914 } ) ;
915915
916+ it ( "retries retryable Google Vertex pre-stream HTTP failures before parsing SSE" , async ( ) => {
917+ const tempDir = await mkdtemp ( path . join ( os . tmpdir ( ) , "openclaw-google-vertex-stream-retry-" ) ) ;
918+ vi . stubEnv ( "GOOGLE_APPLICATION_CREDENTIALS" , "" ) ;
919+ vi . stubEnv ( "HOME" , path . join ( tempDir , "home" ) ) ;
920+ vi . stubEnv ( "APPDATA" , "" ) ;
921+ vi . stubEnv ( "GOOGLE_CLOUD_PROJECT" , "vertex-project" ) ;
922+ vi . stubEnv ( "GOOGLE_CLOUD_LOCATION" , "us-central1" ) ;
923+ vi . stubEnv ( "OPENCLAW_GOOGLE_VERTEX_STREAM_RETRY_BASE_DELAY_MS" , "0" ) ;
924+ vi . stubEnv ( "OPENCLAW_GOOGLE_VERTEX_STREAM_RETRY_MAX_DELAY_MS" , "0" ) ;
925+ googleAuthGetAccessTokenMock . mockResolvedValueOnce ( "ya29.retry-token" ) ;
926+ guardedFetchMock
927+ . mockResolvedValueOnce (
928+ new Response ( JSON . stringify ( { error : { message : "temporarily unavailable" } } ) , {
929+ status : 503 ,
930+ headers : { "content-type" : "application/json" , "retry-after" : "0" } ,
931+ } ) ,
932+ )
933+ . mockResolvedValueOnce (
934+ new Response (
935+ JSON . stringify ( { error : { message : "quota exceeded" , status : "RESOURCE_EXHAUSTED" } } ) ,
936+ { status : 429 , headers : { "content-type" : "application/json" } } ,
937+ ) ,
938+ )
939+ . mockResolvedValueOnce (
940+ buildSseResponse ( [
941+ {
942+ candidates : [ { content : { parts : [ { text : "recovered" } ] } , finishReason : "STOP" } ] ,
943+ } ,
944+ ] ) ,
945+ ) ;
946+
947+ const streamFn = createGoogleVertexTransportStreamFn ( ) ;
948+ const stream = await Promise . resolve (
949+ streamFn (
950+ buildGoogleVertexModel ( ) ,
951+ {
952+ messages : [ { role : "user" , content : "hello" , timestamp : 0 } ] ,
953+ } as Parameters < typeof streamFn > [ 1 ] ,
954+ {
955+ apiKey : "gcp-vertex-credentials" ,
956+ } as Parameters < typeof streamFn > [ 2 ] ,
957+ ) ,
958+ ) ;
959+ const result = await stream . result ( ) ;
960+
961+ expect ( guardedFetchMock ) . toHaveBeenCalledTimes ( 3 ) ;
962+ expect ( result . stopReason ) . toBe ( "stop" ) ;
963+ expect ( result . content ) . toEqual ( [ { type : "text" , text : "recovered" } ] ) ;
964+ } ) ;
965+
966+ it ( "does not retry Google Vertex stream errors after an SSE chunk arrives" , async ( ) => {
967+ const tempDir = await mkdtemp (
968+ path . join ( os . tmpdir ( ) , "openclaw-google-vertex-partial-stream-error-" ) ,
969+ ) ;
970+ vi . stubEnv ( "GOOGLE_APPLICATION_CREDENTIALS" , "" ) ;
971+ vi . stubEnv ( "HOME" , path . join ( tempDir , "home" ) ) ;
972+ vi . stubEnv ( "APPDATA" , "" ) ;
973+ vi . stubEnv ( "GOOGLE_CLOUD_PROJECT" , "vertex-project" ) ;
974+ vi . stubEnv ( "GOOGLE_CLOUD_LOCATION" , "us-central1" ) ;
975+ vi . stubEnv ( "OPENCLAW_GOOGLE_VERTEX_STREAM_RETRY_BASE_DELAY_MS" , "0" ) ;
976+ vi . stubEnv ( "OPENCLAW_GOOGLE_VERTEX_STREAM_RETRY_MAX_DELAY_MS" , "0" ) ;
977+ googleAuthGetAccessTokenMock . mockResolvedValueOnce ( "ya29.partial-token" ) ;
978+ guardedFetchMock . mockResolvedValueOnce (
979+ buildRawSseResponse (
980+ 'data: {"candidates":[{"content":{"parts":[{"text":"partial"}]}}]}\n\ndata: {bad json\n\n' ,
981+ ) ,
982+ ) ;
983+
984+ const streamFn = createGoogleVertexTransportStreamFn ( ) ;
985+ const stream = await Promise . resolve (
986+ streamFn (
987+ buildGoogleVertexModel ( ) ,
988+ {
989+ messages : [ { role : "user" , content : "hello" , timestamp : 0 } ] ,
990+ } as Parameters < typeof streamFn > [ 1 ] ,
991+ {
992+ apiKey : "gcp-vertex-credentials" ,
993+ } as Parameters < typeof streamFn > [ 2 ] ,
994+ ) ,
995+ ) ;
996+ const result = await stream . result ( ) ;
997+
998+ expect ( guardedFetchMock ) . toHaveBeenCalledTimes ( 1 ) ;
999+ expect ( result . stopReason ) . toBe ( "error" ) ;
1000+ expect ( result . errorMessage ) . toBe ( "Google SSE stream returned malformed JSON" ) ;
1001+ expect ( result . content ) . toEqual ( [ { type : "text" , text : "partial" } ] ) ;
1002+ } ) ;
1003+
9161004 it ( "refreshes authorized_user ADC before Google Vertex requests" , async ( ) => {
9171005 const tempDir = await mkdtemp ( path . join ( os . tmpdir ( ) , "openclaw-google-vertex-adc-" ) ) ;
9181006 const credentialsPath = path . join ( tempDir , "application_default_credentials.json" ) ;
@@ -1083,6 +1171,85 @@ describe("google transport stream", () => {
10831171 expect ( tokenFetchMock ) . toHaveBeenCalledTimes ( 2 ) ;
10841172 } ) ;
10851173
1174+ it ( "clears cached Vertex ADC tokens and retries once after unauthenticated responses" , async ( ) => {
1175+ const tempDir = await mkdtemp ( path . join ( os . tmpdir ( ) , "openclaw-google-vertex-adc-401-" ) ) ;
1176+ const credentialsPath = path . join ( tempDir , "application_default_credentials.json" ) ;
1177+ await writeFile (
1178+ credentialsPath ,
1179+ JSON . stringify ( {
1180+ type : "authorized_user" ,
1181+ client_id : "client-id" ,
1182+ client_secret : "client-secret" ,
1183+ refresh_token : "refresh-token" ,
1184+ } ) ,
1185+ "utf8" ,
1186+ ) ;
1187+ vi . stubEnv ( "GOOGLE_APPLICATION_CREDENTIALS" , credentialsPath ) ;
1188+ vi . stubEnv ( "GOOGLE_CLOUD_PROJECT" , "vertex-project" ) ;
1189+ vi . stubEnv ( "GOOGLE_CLOUD_LOCATION" , "global" ) ;
1190+ const tokenFetchMock = vi
1191+ . fn ( )
1192+ . mockResolvedValueOnce (
1193+ new Response ( JSON . stringify ( { access_token : "ya29.stale-token" , expires_in : 3600 } ) , {
1194+ status : 200 ,
1195+ headers : { "content-type" : "application/json" } ,
1196+ } ) ,
1197+ )
1198+ . mockResolvedValueOnce (
1199+ new Response ( JSON . stringify ( { access_token : "ya29.fresh-token" , expires_in : 3600 } ) , {
1200+ status : 200 ,
1201+ headers : { "content-type" : "application/json" } ,
1202+ } ) ,
1203+ ) ;
1204+ guardedFetchMock
1205+ . mockResolvedValueOnce (
1206+ new Response (
1207+ JSON . stringify ( {
1208+ error : {
1209+ message : "Request had invalid authentication credentials." ,
1210+ status : "UNAUTHENTICATED" ,
1211+ } ,
1212+ } ) ,
1213+ { status : 401 , headers : { "content-type" : "application/json" } } ,
1214+ ) ,
1215+ )
1216+ . mockResolvedValueOnce (
1217+ buildSseResponse ( [
1218+ {
1219+ candidates : [ { content : { parts : [ { text : "ok" } ] } , finishReason : "STOP" } ] ,
1220+ } ,
1221+ ] ) ,
1222+ ) ;
1223+
1224+ const streamFn = createGoogleVertexTransportStreamFn ( ) ;
1225+ const stream = await Promise . resolve (
1226+ streamFn (
1227+ buildGoogleVertexModel ( ) ,
1228+ {
1229+ messages : [ { role : "user" , content : "hello" , timestamp : 0 } ] ,
1230+ } as Parameters < typeof streamFn > [ 1 ] ,
1231+ {
1232+ apiKey : "gcp-vertex-credentials" ,
1233+ fetch : tokenFetchMock ,
1234+ } as Parameters < typeof streamFn > [ 2 ] ,
1235+ ) ,
1236+ ) ;
1237+ const result = await stream . result ( ) ;
1238+
1239+ expect ( tokenFetchMock ) . toHaveBeenCalledTimes ( 2 ) ;
1240+ expect ( guardedFetchMock ) . toHaveBeenCalledTimes ( 2 ) ;
1241+ expectHeaders (
1242+ requireRequestInit ( requireMockCall ( guardedFetchMock , 0 , "guarded fetch" ) , "guarded fetch" ) ,
1243+ { Authorization : "Bearer ya29.stale-token" } ,
1244+ ) ;
1245+ expectHeaders (
1246+ requireRequestInit ( requireMockCall ( guardedFetchMock , 1 , "guarded fetch" ) , "guarded fetch" ) ,
1247+ { Authorization : "Bearer ya29.fresh-token" } ,
1248+ ) ;
1249+ expect ( result . stopReason ) . toBe ( "stop" ) ;
1250+ expect ( result . content ) . toEqual ( [ { type : "text" , text : "ok" } ] ) ;
1251+ } ) ;
1252+
10861253 it ( "refreshes authorized_user ADC from the Windows APPDATA fallback for Google Vertex requests" , async ( ) => {
10871254 const tempDir = await mkdtemp ( path . join ( os . tmpdir ( ) , "openclaw-google-vertex-appdata-adc-" ) ) ;
10881255 const homeDir = path . join ( tempDir , "home" ) ;
0 commit comments