@@ -193,7 +193,7 @@ export type ServerBrowserViewerOutput =
193193 readonly accept : string ;
194194 }
195195 | { readonly _tag : "fileChooserClosed" ; readonly id : string }
196- | { readonly _tag : "popup" ; readonly tabId : string }
196+ | { readonly _tag : "popup" ; readonly tabId : string ; readonly handoffId ?: string }
197197 | {
198198 readonly _tag : "download" ;
199199 readonly id : string ;
@@ -219,6 +219,7 @@ export class ServerBrowser extends Context.Service<
219219 readonly quality : number ;
220220 readonly canOperate : boolean ;
221221 readonly sessionId ?: string ;
222+ readonly handoffId ?: string | undefined ;
222223 } ) => Effect . Effect <
223224 ServerBrowserViewer ,
224225 ServerBrowserTabNotFoundError | ServerBrowserLaunchError ,
@@ -247,10 +248,16 @@ export class ServerBrowser extends Context.Service<
247248 }
248249> ( ) ( "t3/preview/ServerBrowser" ) { }
249250
251+ interface PopupHandoff {
252+ readonly id : string ;
253+ readonly sessionId : string | undefined ;
254+ active : boolean ;
255+ }
256+
250257interface ViewerState {
251258 readonly id : string ;
252259 readonly canOperate : boolean ;
253- readonly sessionId : string | undefined ;
260+ handoff : PopupHandoff ;
254261 readonly pressedKeys : Map < string , { key : string ; code : string } > ;
255262 readonly pressedButtons : Map < "left" | "middle" | "right" , { x : number ; y : number } > ;
256263 readonly push : ( output : ServerBrowserViewerOutput ) => void ;
@@ -293,8 +300,7 @@ interface ServerTab {
293300 agentOperating : boolean ;
294301 fileChooserIntercepted : boolean ;
295302 readonly onFileChooser : ( chooser : FileChooser ) => void ;
296- // Only the session that opened this popup can take its initial human handoff.
297- handoffSessionId : string | null ;
303+ handoff : PopupHandoff | null ;
298304
299305 readonly isolatedContext : boolean ;
300306
@@ -477,7 +483,7 @@ const make = Effect.gen(function* () {
477483 {
478484 readonly page : Page ;
479485 readonly openerTabId : string ;
480- handoffSessionId : string | null ;
486+ readonly handoff : PopupHandoff | null ;
481487 }
482488 > ( ) ;
483489 const closedSessions = new Set < string > ( ) ;
@@ -735,6 +741,7 @@ const make = Effect.gen(function* () {
735741
736742 const createTab = async ( snapshot : PreviewSessionSnapshot ) : Promise < ServerTab > => {
737743 const adopted = adoptedPages . get ( tabKey ( snapshot . threadId , snapshot . tabId ) ) ;
744+ adoptedPages . delete ( tabKey ( snapshot . threadId , snapshot . tabId ) ) ;
738745 const desktop =
739746 adopted === undefined && ( await desktopRenders ( snapshot ) )
740747 ? await connectDesktop ( snapshot )
@@ -789,7 +796,7 @@ const make = Effect.gen(function* () {
789796 agentOperating : false ,
790797 fileChooserIntercepted : false ,
791798 onFileChooser : ( chooser ) => void offerFileChooser ( tab , chooser ) ,
792- handoffSessionId : adopted ?. handoffSessionId ?? null ,
799+ handoff : adopted ?. handoff ?? null ,
793800 isolatedContext,
794801 desktop : desktop === null ? null : { close : desktop . close } ,
795802 profileId : snapshot . profileId ,
@@ -800,7 +807,7 @@ const make = Effect.gen(function* () {
800807 setting : snapshot . viewport ?? FILL_PREVIEW_VIEWPORT ,
801808 colorScheme : "system" ,
802809 zoomFactor : 1 ,
803- loading : false ,
810+ loading : adopted !== undefined && snapshot . navStatus . _tag === "Loading" ,
804811 closing : false ,
805812 recording : null ,
806813 recordingStart : null ,
@@ -914,8 +921,6 @@ const make = Effect.gen(function* () {
914921 }
915922 page . on ( "close" , ( ) => dropTab ( tab , true ) ) ;
916923 page . on ( "crash" , ( ) => dropTab ( tab , true ) ) ;
917- // The opener's controller may have released during setup.
918- tab . handoffSessionId = adopted ?. handoffSessionId ?? null ;
919924 tabs . set ( key , tab ) ;
920925 reportLiveTabs ( ) ;
921926 // Existing desktop and popup PDFs can start loading before interception attaches.
@@ -935,16 +940,19 @@ const make = Effect.gen(function* () {
935940 if ( tab . control . agentId !== null ) tab . control . track ( tab . initialNavigation ) ;
936941 else void tab . initialNavigation . catch ( constVoid ) ;
937942 } else if ( adopted ) {
938- // Popup navigation can finish during setup, before this tab's listeners exist.
939- // A load after setup settles this wait too, and the `load` listener reports that one.
943+ // An adopted page can finish loading before its listeners attach.
940944 let loadReported = false ;
941945 page . once ( "load" , ( ) => {
942946 loadReported = true ;
943947 } ) ;
948+ const generation = tab . navigationGeneration ;
944949 void page
945950 . waitForLoadState ( "load" )
946951 . then ( ( ) => {
947- if ( ! loadReported ) return reportLoaded ( tab ) ;
952+ if ( ! loadReported && tab . navigationGeneration === generation ) {
953+ tab . loading = false ;
954+ return reportLoaded ( tab ) ;
955+ }
948956 } )
949957 . catch ( constVoid ) ;
950958 }
@@ -1234,6 +1242,7 @@ const make = Effect.gen(function* () {
12341242 return ;
12351243 }
12361244 const url = popup . url ( ) ;
1245+ const handoff = controller ?. handoff ?? null ;
12371246
12381247 await runPromise (
12391248 manager . open ( {
@@ -1252,12 +1261,13 @@ const make = Effect.gen(function* () {
12521261 adoptedPages . set ( tabKey ( snapshot . threadId , snapshot . tabId ) , {
12531262 page : popup ,
12541263 openerTabId : opener . tabId ,
1255- handoffSessionId : controller ?. sessionId ?? null ,
1264+ handoff ,
12561265 } ) ,
12571266 } ) ,
12581267 ) . then (
12591268 ( snapshot ) => {
1260- if ( controller !== undefined ) controller . push ( { _tag : "popup" , tabId : snapshot . tabId } ) ;
1269+ if ( handoff ?. active )
1270+ controller ?. push ( { _tag : "popup" , tabId : snapshot . tabId , handoffId : handoff . id } ) ;
12611271 } ,
12621272 async ( ) => {
12631273 await popup . close ( ) . catch ( constVoid ) ;
@@ -1284,7 +1294,6 @@ const make = Effect.gen(function* () {
12841294 . finally ( ( ) => {
12851295 pendingTabs . delete ( key ) ;
12861296 closedPendingTabs . delete ( key ) ;
1287- adoptedPages . delete ( key ) ;
12881297 } ) ;
12891298 pendingTabs . set ( key , opening ) ;
12901299 return opening ;
@@ -2341,7 +2350,7 @@ const make = Effect.gen(function* () {
23412350 const viewer : ViewerState = {
23422351 id : NodeCrypto . randomUUID ( ) ,
23432352 canOperate : input . canOperate ,
2344- sessionId : input . sessionId ,
2353+ handoff : { id : NodeCrypto . randomUUID ( ) , sessionId : input . sessionId , active : true } ,
23452354 pressedKeys : new Map ( ) ,
23462355 pressedButtons : new Map ( ) ,
23472356 push : ( next ) => {
@@ -2386,9 +2395,13 @@ const make = Effect.gen(function* () {
23862395 if (
23872396 input . canOperate &&
23882397 tab . control . controller === null &&
2389- ( tab . control . agentId === null || input . sessionId === tab . handoffSessionId )
2398+ ( tab . control . agentId === null ||
2399+ ( tab . handoff ?. active &&
2400+ input . handoffId === tab . handoff . id &&
2401+ input . sessionId !== undefined &&
2402+ input . sessionId === tab . handoff . sessionId ) )
23902403 ) {
2391- tab . handoffSessionId = null ;
2404+ tab . handoff = null ;
23922405 yield * Effect . promise ( ( ) => tab . control . take ( viewer . id ) ) ;
23932406 }
23942407 broadcastControl ( tab ) ;
@@ -2465,29 +2478,19 @@ const make = Effect.gen(function* () {
24652478 try {
24662479 if ( message . type === "takeControl" ) {
24672480 const taking = tab . control . take ( viewer . id ) ;
2481+ if ( ! viewer . handoff . active ) {
2482+ viewer . handoff = {
2483+ id : NodeCrypto . randomUUID ( ) ,
2484+ sessionId : input . sessionId ,
2485+ active : true ,
2486+ } ;
2487+ }
24682488 broadcastControl ( tab ) ;
24692489 await taking ;
24702490 pushFileChooser ( tab ) ;
24712491 } else if ( message . type === "releaseControl" ) {
2472- // Do not disarm on disconnect: the opener stream may drop while switching tabs.
2473- if ( viewer . id === tab . control . controller ) {
2474- for ( const candidate of tabs . values ( ) ) {
2475- if (
2476- candidate . openerTabId === tab . tabId &&
2477- candidate . handoffSessionId === viewer . sessionId
2478- ) {
2479- candidate . handoffSessionId = null ;
2480- }
2481- }
2482- for ( const pending of adoptedPages . values ( ) ) {
2483- if (
2484- pending . openerTabId === tab . tabId &&
2485- pending . handoffSessionId === viewer . sessionId
2486- ) {
2487- pending . handoffSessionId = null ;
2488- }
2489- }
2490- }
2492+ // A stream disconnect during the popup switch must preserve the grant.
2493+ if ( viewer . id === tab . control . controller ) viewer . handoff . active = false ;
24912494 const releasing = tab . control . release ( viewer . id , ( ) =>
24922495 releaseViewerInput ( viewer , session ) ,
24932496 ) ;
0 commit comments