@@ -792,6 +792,20 @@ describe("browser tool snapshot maxChars", () => {
792792 expect ( opts . timeoutMs ) . toBeUndefined ( ) ;
793793 } ) ;
794794
795+ it ( "preserves cancellation while listing host system profiles" , async ( ) => {
796+ const controller = new AbortController ( ) ;
797+ const abortError = new Error ( "agent turn cancelled" ) ;
798+ browserClientMocks . browserSystemProfiles . mockImplementationOnce ( async ( ) => {
799+ controller . abort ( abortError ) ;
800+ throw abortError ;
801+ } ) ;
802+
803+ await expect (
804+ createBrowserTool ( ) . execute ?.( "call-1" , { action : "profiles" } , controller . signal ) ,
805+ ) . rejects . toBe ( abortError ) ;
806+ expect ( browserClientMocks . browserProfiles ) . not . toHaveBeenCalled ( ) ;
807+ } ) ;
808+
795809 it ( "uses a longer default timeout for existing-session profile status through node proxy" , async ( ) => {
796810 mockSingleBrowserProxyNode ( ) ;
797811 setResolvedBrowserProfiles ( {
@@ -1251,6 +1265,7 @@ describe("browser tool snapshot maxChars", () => {
12511265
12521266 it ( "compensates durable tracking failure on the automatic host fallback" , async ( ) => {
12531267 const trackingError = new Error ( "sqlite unavailable" ) ;
1268+ const controller = new AbortController ( ) ;
12541269 mockSingleBrowserProxyNode ( ) ;
12551270 gatewayMocks . callGatewayTool . mockRejectedValueOnce (
12561271 new Error ( "Browser control host is not reachable on 127.0.0.1:18791." ) ,
@@ -1267,25 +1282,36 @@ describe("browser tool snapshot maxChars", () => {
12671282 } ,
12681283 } ) ;
12691284 sessionTabRegistryMocks . trackSessionBrowserTab . mockImplementationOnce ( ( ) => {
1285+ controller . abort ( new Error ( "agent turn cancelled" ) ) ;
12701286 throw trackingError ;
12711287 } ) ;
12721288 const tool = createBrowserTool ( { agentSessionKey : "agent:main:main" } ) ;
12731289
12741290 await expect (
1275- tool . execute ?.( "call-1" , {
1276- action : "open" ,
1277- profile : "work" ,
1278- url : "https://example.com" ,
1279- } ) ,
1291+ tool . execute ?.(
1292+ "call-1" ,
1293+ {
1294+ action : "open" ,
1295+ profile : "work" ,
1296+ url : "https://example.com" ,
1297+ } ,
1298+ controller . signal ,
1299+ ) ,
12801300 ) . rejects . toBe ( trackingError ) ;
1281- expect ( toolCommonMocks . fetchBrowserJson ) . toHaveBeenLastCalledWith (
1282- "/tabs/host-tab-compensate?profile=work-actual" ,
1301+ expect ( browserClientMocks . browserCloseTab ) . toHaveBeenCalledWith (
1302+ undefined ,
1303+ "host-tab-compensate" ,
12831304 {
1284- method : "DELETE" ,
1285- body : undefined ,
1305+ profile : "work-actual" ,
12861306 timeoutMs : undefined ,
12871307 } ,
12881308 ) ;
1309+ expect ( toolCommonMocks . fetchBrowserJson ) . toHaveBeenLastCalledWith ( "/tabs/open?profile=work" , {
1310+ method : "POST" ,
1311+ body : JSON . stringify ( { url : "https://example.com" } ) ,
1312+ timeoutMs : undefined ,
1313+ signal : controller . signal ,
1314+ } ) ;
12891315 } ) ;
12901316
12911317 it ( "touches tabs used after automatic host fallback" , async ( ) => {
@@ -1521,6 +1547,92 @@ describe("browser tool snapshot maxChars", () => {
15211547 expect ( opts . timeoutMs ) . toBe ( 12_345 ) ;
15221548 } ) ;
15231549
1550+ it . each ( [
1551+ [ "doctor" , { action : "doctor" , target : "host" } , browserClientMocks . browserDoctor , 1 ] ,
1552+ [ "status" , { action : "status" , target : "host" } , browserClientMocks . browserStatus , 1 ] ,
1553+ [ "start" , { action : "start" , target : "host" } , browserClientMocks . browserStart , 1 ] ,
1554+ [ "stop" , { action : "stop" , target : "host" } , browserClientMocks . browserStop , 1 ] ,
1555+ [ "profiles" , { action : "profiles" , target : "host" } , browserClientMocks . browserProfiles , 1 ] ,
1556+ [
1557+ "importprofile" ,
1558+ { action : "importprofile" , target : "host" } ,
1559+ browserClientMocks . browserImportProfile ,
1560+ 1 ,
1561+ ] ,
1562+ [ "tabs" , { action : "tabs" , target : "host" } , browserClientMocks . browserTabs , 1 ] ,
1563+ [
1564+ "open" ,
1565+ { action : "open" , target : "host" , url : "about:blank" } ,
1566+ browserClientMocks . browserOpenTab ,
1567+ 2 ,
1568+ ] ,
1569+ [
1570+ "focus" ,
1571+ { action : "focus" , target : "host" , targetId : "tab-1" } ,
1572+ browserClientMocks . browserFocusTab ,
1573+ 2 ,
1574+ ] ,
1575+ [
1576+ "close" ,
1577+ { action : "close" , target : "host" , targetId : "tab-1" } ,
1578+ browserClientMocks . browserCloseTab ,
1579+ 2 ,
1580+ ] ,
1581+ [ "snapshot" , { action : "snapshot" , target : "host" } , browserClientMocks . browserSnapshot , 1 ] ,
1582+ [
1583+ "screenshot" ,
1584+ { action : "screenshot" , target : "host" } ,
1585+ browserActionsMocks . browserScreenshotAction ,
1586+ 1 ,
1587+ ] ,
1588+ [
1589+ "navigate" ,
1590+ { action : "navigate" , target : "host" , url : "about:blank" } ,
1591+ browserActionsMocks . browserNavigate ,
1592+ 1 ,
1593+ ] ,
1594+ [ "pdf" , { action : "pdf" , target : "host" } , browserActionsMocks . browserPdfSave , 1 ] ,
1595+ [
1596+ "upload" ,
1597+ { action : "upload" , target : "host" , paths : [ "/tmp/report.pdf" ] } ,
1598+ browserActionsMocks . browserArmFileChooser ,
1599+ 1 ,
1600+ ] ,
1601+ [
1602+ "dialog" ,
1603+ { action : "dialog" , target : "host" , accept : true } ,
1604+ browserActionsMocks . browserArmDialog ,
1605+ 1 ,
1606+ ] ,
1607+ [
1608+ "console" ,
1609+ { action : "console" , target : "host" } ,
1610+ browserActionsMocks . browserConsoleMessages ,
1611+ 1 ,
1612+ ] ,
1613+ [
1614+ "act" ,
1615+ { action : "act" , target : "host" , request : { kind : "click" , ref : "e1" } } ,
1616+ browserActionsMocks . browserAct ,
1617+ 2 ,
1618+ ] ,
1619+ ] as const ) (
1620+ "forwards the agent signal to local %s actions" ,
1621+ async ( _name , args , mock , optionsIndex ) => {
1622+ const controller = new AbortController ( ) ;
1623+ pathValidationMocks . resolveExistingUploadPaths . mockResolvedValue ( {
1624+ ok : true ,
1625+ paths : [ "/tmp/report.pdf" ] ,
1626+ } ) ;
1627+
1628+ await createBrowserTool ( ) . execute ?.( "call-1" , args , controller . signal ) ;
1629+
1630+ expect ( lastMockCallArg < { signal ?: AbortSignal } > ( mock , optionsIndex ) . signal ) . toBe (
1631+ controller . signal ,
1632+ ) ;
1633+ } ,
1634+ ) ;
1635+
15241636 it ( "parses string screenshot timeoutMs values" , async ( ) => {
15251637 const tool = createBrowserTool ( ) ;
15261638 await tool . execute ?.( "call-1" , {
@@ -2679,14 +2791,21 @@ describe("browser tool url alias support", () => {
26792791 targetId : "nav-tab" ,
26802792 url : "https://example.com/next" ,
26812793 } ) ;
2682- const abortError = new Error ( "This operation was aborted" ) ;
2683- abortError . name = "AbortError" ;
2684- browserClientMocks . browserSnapshot . mockRejectedValueOnce ( abortError ) ;
2794+ const controller = new AbortController ( ) ;
2795+ const abortError = new Error ( "agent turn cancelled" ) ;
2796+ browserClientMocks . browserSnapshot . mockImplementationOnce ( async ( ) => {
2797+ controller . abort ( abortError ) ;
2798+ throw abortError ;
2799+ } ) ;
26852800 const tool = createBrowserTool ( ) ;
26862801
26872802 await expect (
2688- tool . execute ?.( "call-1" , { action : "navigate" , url : "https://example.com/next" } ) ,
2689- ) . rejects . toMatchObject ( { name : "AbortError" } ) ;
2803+ tool . execute ?.(
2804+ "call-1" ,
2805+ { action : "navigate" , url : "https://example.com/next" } ,
2806+ controller . signal ,
2807+ ) ,
2808+ ) . rejects . toBe ( abortError ) ;
26902809 } ) ;
26912810
26922811 it ( "degrades inline page state when the node proxy falls back to the host mid-call" , async ( ) => {
@@ -3736,6 +3855,29 @@ describe("browser tool act stale target recovery", () => {
37363855 expect ( browserActionsMocks . browserAct ) . toHaveBeenCalledTimes ( 2 ) ;
37373856 } ) ;
37383857
3858+ it ( "preserves cancellation while refreshing a stale target" , async ( ) => {
3859+ const controller = new AbortController ( ) ;
3860+ const abortError = new Error ( "agent turn cancelled" ) ;
3861+ browserActionsMocks . browserAct . mockRejectedValueOnce ( new Error ( "404: tab not found" ) ) ;
3862+ browserClientMocks . browserTabs . mockImplementationOnce ( async ( ) => {
3863+ controller . abort ( abortError ) ;
3864+ throw abortError ;
3865+ } ) ;
3866+ const tool = createBrowserTool ( ) ;
3867+
3868+ await expect (
3869+ tool . execute ?.(
3870+ "call-1" ,
3871+ {
3872+ action : "act" ,
3873+ profile : "user" ,
3874+ request : { kind : "wait" , targetId : "stale-tab" , timeMs : 1 } ,
3875+ } ,
3876+ controller . signal ,
3877+ ) ,
3878+ ) . rejects . toBe ( abortError ) ;
3879+ } ) ;
3880+
37393881 it ( "retries stale targetIds returned through the node browser proxy" , async ( ) => {
37403882 mockSingleBrowserProxyNode ( ) ;
37413883 setResolvedBrowserProfiles ( {
0 commit comments