@@ -8,6 +8,7 @@ type SessionTabParams = {
88 targetId ?: string ;
99 baseUrl ?: string ;
1010 profile ?: string ;
11+ profileAliases ?: Array < string | undefined > ;
1112 ownership ?: BrowserTabOwnership ;
1213 aliases ?: Array < string | undefined > ;
1314} ;
@@ -62,33 +63,39 @@ export function stripBrowserOpenInternalMetadata(value: unknown): unknown {
6263 return agentVisible ;
6364}
6465
65- async function trackOpenedHostBrowserTab ( params : {
66+ async function trackOpenedBrowserTab ( params : {
6667 result : unknown ;
6768 sessionKey ?: string ;
68- fallbackProfile : string ;
69+ fallbackProfile ?: string ;
70+ baseUrl ?: string ;
6971 track : SessionTabRegistry [ "trackSessionBrowserTab" ] ;
70- closeTab : ( targetId : string , profile : string ) => Promise < void > ;
72+ closeTab : ( targetId : string , profile ? : string ) => Promise < void > ;
7173} ) : Promise < void > {
7274 const opened = readOpenedTab ( params . result ) ;
7375 const profile = opened . profile ?? params . fallbackProfile ;
7476 try {
7577 params . track ( {
7678 sessionKey : params . sessionKey ,
7779 targetId : opened . targetId ,
78- baseUrl : undefined ,
80+ baseUrl : params . baseUrl ,
7981 profile,
80- ownership : opened . ownership ,
82+ ...( params . fallbackProfile && opened . profile && opened . profile !== params . fallbackProfile
83+ ? { profileAliases : [ params . fallbackProfile ] }
84+ : { } ) ,
85+ // Sandbox/browser-bridge tabs belong to a different browser process.
86+ // Keep them process-local even if that server returned durable metadata.
87+ ownership : params . baseUrl ? undefined : opened . ownership ,
8188 aliases : opened . aliases ,
8289 } ) ;
8390 } catch ( trackingError ) {
84- if ( opened . ownership ?. status !== "durable" || ! opened . targetId ) {
91+ if ( ! opened . targetId ) {
8592 throw trackingError ;
8693 }
8794 try {
8895 await params . closeTab ( opened . targetId , profile ) ;
8996 } catch ( closeError ) {
9097 throw Object . assign (
91- new Error ( "Failed to persist browser tab ownership and close the newly opened tab" , {
98+ new Error ( "Failed to register browser tab cleanup and close the newly opened tab" , {
9299 cause : closeError ,
93100 } ) ,
94101 {
@@ -110,36 +117,39 @@ export function createBrowserToolSessionTabs(params: {
110117 registry : SessionTabRegistry ;
111118} ) {
112119 const profile = params . requestedProfile ?? params . defaultProfile ;
113- const isTrackedHostRoute = ( ) =>
114- ! params . baseUrl && ( ! params . isHostFallbackActive || params . isHostFallbackActive ( ) ) ;
120+ const isTrackedRoute = ( ) => ! params . isHostFallbackActive || params . isHostFallbackActive ( ) ;
121+ const trackedBaseUrl = ( ) => ( params . isHostFallbackActive ? undefined : params . baseUrl ) ;
122+ const trackedProfile = ( ) => ( trackedBaseUrl ( ) && ! params . requestedProfile ? undefined : profile ) ;
115123 const identity = ( targetId : string ) => ( {
116124 sessionKey : params . sessionKey ,
117125 targetId,
118- baseUrl : undefined ,
119- profile,
126+ baseUrl : trackedBaseUrl ( ) ,
127+ profile : trackedProfile ( ) ,
120128 } ) ;
121129 return {
122130 touch : ( targetId : string | undefined ) : void => {
123- if ( targetId && isTrackedHostRoute ( ) ) {
131+ if ( targetId && isTrackedRoute ( ) ) {
124132 params . registry . touchSessionBrowserTab ( identity ( targetId ) ) ;
125133 }
126134 } ,
127135 untrack : ( targetId : string | undefined ) : void => {
128- if ( targetId && isTrackedHostRoute ( ) ) {
136+ if ( targetId && isTrackedRoute ( ) ) {
129137 params . registry . untrackSessionBrowserTab ( identity ( targetId ) ) ;
130138 }
131139 } ,
132140 trackOpened : async (
133141 result : unknown ,
134- closeTab : ( targetId : string , openedProfile : string ) => Promise < void > ,
142+ closeTab : ( targetId : string , openedProfile ? : string ) => Promise < void > ,
135143 ) : Promise < void > => {
136- if ( ! isTrackedHostRoute ( ) ) {
144+ if ( ! isTrackedRoute ( ) ) {
137145 return ;
138146 }
139- await trackOpenedHostBrowserTab ( {
147+ const baseUrl = trackedBaseUrl ( ) ;
148+ await trackOpenedBrowserTab ( {
140149 result,
141150 sessionKey : params . sessionKey ,
142- fallbackProfile : profile ,
151+ fallbackProfile : baseUrl && ! params . requestedProfile ? undefined : profile ,
152+ baseUrl,
143153 track : params . registry . trackSessionBrowserTab ,
144154 closeTab,
145155 } ) ;
0 commit comments