@@ -186,6 +186,13 @@ const sessionTabRegistryMocks = vi.hoisted(() => ({
186186vi . mock ( "./browser/session-tab-registry.js" , ( ) => sessionTabRegistryMocks ) ;
187187
188188const toolCommonMocks = vi . hoisted ( ( ) => ( {
189+ fetchBrowserJson : vi . fn (
190+ async ( ..._args : unknown [ ] ) : Promise < Record < string , unknown > > => ( {
191+ ok : true ,
192+ running : true ,
193+ source : "gateway-host" ,
194+ } ) ,
195+ ) ,
189196 imageResultFromFile : vi . fn ( ) ,
190197 describeImageFile : vi . fn ( async ( ) => ( { text : undefined , decision : { outcome : "skipped" } } ) ) ,
191198 normalizeBrowserScreenshot : vi . fn ( async ( buffer : Buffer ) => ( { buffer } ) ) ,
@@ -233,6 +240,7 @@ vi.mock("./browser-tool.runtime.js", () => {
233240 ...configMocks ,
234241 ...gatewayMocks ,
235242 ...sessionTabRegistryMocks ,
243+ fetchBrowserJson : toolCommonMocks . fetchBrowserJson ,
236244 getRuntimeConfig : configMocks . loadConfig ,
237245 resolveRuntimeImageSanitization : ( ) => {
238246 const configured = configMocks . loadConfig ( ) . agents ?. defaults ?. imageMaxDimensionPx ;
@@ -330,6 +338,11 @@ function resetBrowserToolMocks() {
330338 toolCommonMocks . stageBrowserScreenshotForSharing . mockResolvedValue (
331339 "/tmp/openclaw-media/outbound/share.png" ,
332340 ) ;
341+ toolCommonMocks . fetchBrowserJson . mockResolvedValue ( {
342+ ok : true ,
343+ running : true ,
344+ source : "gateway-host" ,
345+ } ) ;
333346}
334347
335348function setResolvedBrowserProfiles (
@@ -999,6 +1012,136 @@ describe("browser tool snapshot maxChars", () => {
9991012 expect ( browserClientMocks . browserStatus ) . not . toHaveBeenCalled ( ) ;
10001013 } ) ;
10011014
1015+ it ( "falls back to the Gateway host when an auto-selected node has no browser host" , async ( ) => {
1016+ mockSingleBrowserProxyNode ( ) ;
1017+ gatewayMocks . callGatewayTool . mockRejectedValueOnce (
1018+ new Error (
1019+ "Browser control host is not reachable on 127.0.0.1:18791. Start the local OpenClaw browser control host." ,
1020+ ) ,
1021+ ) ;
1022+ const tool = createBrowserTool ( ) ;
1023+
1024+ const result = await tool . execute ?.( "call-1" , { action : "status" } ) ;
1025+
1026+ expect ( result ?. details ) . toMatchObject ( { source : "gateway-host" } ) ;
1027+ expect ( toolCommonMocks . fetchBrowserJson ) . toHaveBeenCalledWith ( "/" , {
1028+ method : "GET" ,
1029+ body : undefined ,
1030+ timeoutMs : undefined ,
1031+ } ) ;
1032+ } ) ;
1033+
1034+ it ( "tracks tabs opened after automatic host fallback" , async ( ) => {
1035+ mockSingleBrowserProxyNode ( ) ;
1036+ gatewayMocks . callGatewayTool . mockRejectedValueOnce (
1037+ new Error ( "Browser control host is not reachable on 127.0.0.1:18791." ) ,
1038+ ) ;
1039+ toolCommonMocks . fetchBrowserJson . mockResolvedValueOnce ( {
1040+ targetId : "host-tab-opened" ,
1041+ url : "https://example.com" ,
1042+ } ) ;
1043+ const tool = createBrowserTool ( { agentSessionKey : "agent:main:main" } ) ;
1044+
1045+ await tool . execute ?.( "call-1" , { action : "open" , url : "https://example.com" } ) ;
1046+
1047+ expect ( sessionTabRegistryMocks . trackSessionBrowserTab ) . toHaveBeenCalledWith ( {
1048+ sessionKey : "agent:main:main" ,
1049+ targetId : "host-tab-opened" ,
1050+ baseUrl : undefined ,
1051+ profile : undefined ,
1052+ } ) ;
1053+ } ) ;
1054+
1055+ it ( "touches tabs used after automatic host fallback" , async ( ) => {
1056+ mockSingleBrowserProxyNode ( ) ;
1057+ gatewayMocks . callGatewayTool . mockRejectedValueOnce (
1058+ new Error ( "Browser control host is not reachable on 127.0.0.1:18791." ) ,
1059+ ) ;
1060+ toolCommonMocks . fetchBrowserJson . mockResolvedValueOnce ( {
1061+ targetId : "host-tab-used" ,
1062+ url : "https://example.com/next" ,
1063+ } ) ;
1064+ const tool = createBrowserTool ( { agentSessionKey : "agent:main:main" } ) ;
1065+
1066+ await tool . execute ?.( "call-1" , {
1067+ action : "navigate" ,
1068+ url : "https://example.com/next" ,
1069+ targetId : "host-tab-used" ,
1070+ } ) ;
1071+
1072+ expect ( sessionTabRegistryMocks . touchSessionBrowserTab ) . toHaveBeenCalledWith ( {
1073+ sessionKey : "agent:main:main" ,
1074+ targetId : "host-tab-used" ,
1075+ baseUrl : undefined ,
1076+ profile : undefined ,
1077+ } ) ;
1078+ } ) ;
1079+
1080+ it ( "untracks tabs closed after automatic host fallback" , async ( ) => {
1081+ mockSingleBrowserProxyNode ( ) ;
1082+ gatewayMocks . callGatewayTool . mockRejectedValueOnce (
1083+ new Error ( "Browser control host is not reachable on 127.0.0.1:18791." ) ,
1084+ ) ;
1085+ toolCommonMocks . fetchBrowserJson . mockResolvedValueOnce ( { ok : true } ) ;
1086+ const tool = createBrowserTool ( { agentSessionKey : "agent:main:main" } ) ;
1087+
1088+ await tool . execute ?.( "call-1" , { action : "close" , targetId : "host-tab-closed" } ) ;
1089+
1090+ expect ( sessionTabRegistryMocks . untrackSessionBrowserTab ) . toHaveBeenCalledWith ( {
1091+ sessionKey : "agent:main:main" ,
1092+ targetId : "host-tab-closed" ,
1093+ baseUrl : undefined ,
1094+ profile : undefined ,
1095+ } ) ;
1096+ } ) ;
1097+
1098+ it . each ( [
1099+ [ "an explicit node target" , { target : "node" } ] ,
1100+ [ "an explicit node pin" , { node : "node-1" } ] ,
1101+ ] ) ( "does not host-fallback for %s" , async ( _label , route ) => {
1102+ mockSingleBrowserProxyNode ( ) ;
1103+ gatewayMocks . callGatewayTool . mockRejectedValueOnce (
1104+ new Error ( "Browser control host is not reachable on 127.0.0.1:18791." ) ,
1105+ ) ;
1106+ const tool = createBrowserTool ( ) ;
1107+
1108+ await expect ( tool . execute ?.( "call-1" , { action : "status" , ...route } ) ) . rejects . toThrow (
1109+ / B r o w s e r c o n t r o l h o s t i s n o t r e a c h a b l e / ,
1110+ ) ;
1111+ expect ( toolCommonMocks . fetchBrowserJson ) . not . toHaveBeenCalled ( ) ;
1112+ } ) ;
1113+
1114+ it ( "does not host-fallback after an ambiguous node failure" , async ( ) => {
1115+ mockSingleBrowserProxyNode ( ) ;
1116+ gatewayMocks . callGatewayTool . mockRejectedValueOnce ( new Error ( "node invoke timed out" ) ) ;
1117+ const tool = createBrowserTool ( ) ;
1118+
1119+ await expect ( tool . execute ?.( "call-1" , { action : "status" } ) ) . rejects . toThrow (
1120+ / n o d e i n v o k e t i m e d o u t / ,
1121+ ) ;
1122+ expect ( toolCommonMocks . fetchBrowserJson ) . not . toHaveBeenCalled ( ) ;
1123+ } ) ;
1124+
1125+ it ( "does not host-fallback for a browser-service error with similar wording" , async ( ) => {
1126+ mockSingleBrowserProxyNode ( ) ;
1127+ gatewayMocks . callGatewayTool . mockResolvedValueOnce ( {
1128+ ok : true ,
1129+ payload : {
1130+ error : {
1131+ status : 503 ,
1132+ body : { error : "Browser control host is not reachable during this action" } ,
1133+ } ,
1134+ } ,
1135+ } ) ;
1136+ const tool = createBrowserTool ( ) ;
1137+
1138+ await expect ( tool . execute ?.( "call-1" , { action : "status" } ) ) . rejects . toMatchObject ( {
1139+ name : "BrowserServiceError" ,
1140+ status : 503 ,
1141+ } ) ;
1142+ expect ( toolCommonMocks . fetchBrowserJson ) . not . toHaveBeenCalled ( ) ;
1143+ } ) ;
1144+
10021145 it . each ( [
10031146 [ "target=node" , { target : "node" } ] ,
10041147 [ "an explicit node pin" , { node : "node-1" } ] ,
0 commit comments