@@ -279,24 +279,9 @@ function McpAppTileContent({
279279 const onAutoLaunchDisabledRef = useRef ( onAutoLaunchDisabled ) ;
280280 onAutoLaunchDisabledRef . current = onAutoLaunchDisabled ;
281281 const launchRef = useRef < TileAttempt | null > ( null ) ;
282- const resolveLiveActions = useCallback ( async ( ) : Promise < LiveTarget > => {
283- const document = stateRef . current . phase === "ready" ? stateRef . current . lifetime : null ;
284- for ( ; ; ) {
285- const attempt = launchRef . current ;
286- // The launch's own handler settles the tile first, so the state read below is its outcome.
287- if ( attempt ?. promise ) await attempt . promise . catch ( ( ) => undefined ) ;
288- const current = stateRef . current ;
289- // A launch for a different document retires this one; only its replacement may use the new lease.
290- if ( ! document ?. active || current . phase !== "ready" || current . lifetime !== document ) {
291- throw new Error ( "This artifact view has closed or changed." ) ;
292- }
293- const live = liveTargetOf ( current ) ;
294- if ( live ?. app . launchId ) return live ;
295- if ( launchRef . current === attempt ) {
296- throw new Error ( "This artifact's workspace is still unavailable. Refresh the tile to reconnect." ) ;
297- }
298- }
299- } , [ ] ) ;
282+ // Wakes callers waiting for a launch they requested, or for their document to close.
283+ const launchWaiters = useRef < Array < ( ) => void > > ( [ ] ) ;
284+ const wakeLaunchWaiters = useCallback ( ( ) => { for ( const wake of launchWaiters . current . splice ( 0 ) ) wake ( ) ; } , [ ] ) ;
300285 const lifetime = useRef ( { active : true } ) ;
301286 const endpointsRef = useRef ( launchEndpoints ) ;
302287 endpointsRef . current = launchEndpoints ;
@@ -320,7 +305,8 @@ function McpAppTileContent({
320305 }
321306 removeDashboardTileCache ( cacheScopeKey , entry . id ) ;
322307 updateState ( next ) ;
323- } , [ cacheScopeKey , entry . id , releaseLaunch , updateState ] ) ;
308+ wakeLaunchWaiters ( ) ;
309+ } , [ cacheScopeKey , entry . id , releaseLaunch , updateState , wakeLaunchWaiters ] ) ;
324310 const requestRefresh = useCallback ( ( userInitiated : boolean ) => {
325311 if ( ! lifetime . current . active || refreshStateRef . current === "refreshing" ) return ;
326312 updateRefresh ( "refreshing" ) ;
@@ -330,6 +316,35 @@ function McpAppTileContent({
330316 setStarted ( true ) ;
331317 setNonce ( next ) ;
332318 } , [ updateRefresh ] ) ;
319+ const resolveLiveActions = useCallback ( async ( staleLaunchId ?: string ) : Promise < LiveTarget > => {
320+ const document = stateRef . current . phase === "ready" ? stateRef . current . lifetime : null ;
321+ let renewing = false ;
322+ for ( ; ; ) {
323+ const attempt = launchRef . current ;
324+ // The launch's own handler settles the tile first, so the state read below is its outcome.
325+ if ( attempt ?. promise ) await attempt . promise . catch ( ( ) => undefined ) ;
326+ const current = stateRef . current ;
327+ // A launch for a different document retires this one; only its replacement may use the new lease.
328+ if ( ! document ?. active || current . phase !== "ready" || current . lifetime !== document ) {
329+ throw new Error ( "This artifact view has closed or changed." ) ;
330+ }
331+ const live = liveTargetOf ( current ) ;
332+ const leaseId = live ?. app . launchId ;
333+ const expired = live ?. app . refresh ? live . app . refresh . expiresAt <= Date . now ( ) : false ;
334+ if ( live && leaseId && leaseId !== staleLaunchId && ! expired ) return live ;
335+ if ( launchRef . current !== attempt ) continue ;
336+ // A lease that expired or that the host reported stale is renewed by one
337+ // refresh of this tile, never handed out again.
338+ if ( leaseId && ! renewing ) {
339+ renewing = true ;
340+ const launched = new Promise < void > ( ( resolve ) => { launchWaiters . current . push ( resolve ) ; } ) ;
341+ requestRefresh ( false ) ;
342+ await launched ;
343+ continue ;
344+ }
345+ throw new Error ( "This artifact's workspace is still unavailable. Refresh the tile to reconnect." ) ;
346+ }
347+ } , [ requestRefresh ] ) ;
333348 useLayoutEffect ( ( ) => {
334349 lifetime . current . active = true ;
335350 if ( launchRef . current ?. controller . signal . aborted && ! launchRef . current . admitted ) launchRef . current = null ;
@@ -340,8 +355,9 @@ function McpAppTileContent({
340355 launchRef . current ?. controller . abort ( ) ;
341356 if ( stateRef . current . phase === "ready" ) stateRef . current . lifetime . active = false ;
342357 releaseLaunches ( ) ;
358+ wakeLaunchWaiters ( ) ;
343359 } ;
344- } , [ releaseLaunches ] ) ;
360+ } , [ releaseLaunches , wakeLaunchWaiters ] ) ;
345361 useLayoutEffect ( ( ) => {
346362 if ( savedCache && ( ! cached || ! cachedEndpoint ) ) removeDashboardTileCache ( cacheScopeKey , entry . id ) ;
347363 const current = stateRef . current ;
@@ -445,10 +461,14 @@ function McpAppTileContent({
445461 reused = current ;
446462 } catch ( cause ) {
447463 assertActive ( ) ;
448- if ( ! ( cause instanceof OpenworkServerError ) || cause . status !== 422 || ! [ "mcp_app_resource_changed" , "mcp_app_refresh_denied" ] . includes ( cause . code ) ) throw cause ;
449- fallbackEndpoint = current . endpoint ;
450- clearDocument ( { phase : "loading" } ) ;
451- attempt . document = undefined ;
464+ if ( ! ( cause instanceof OpenworkServerError ) || cause . status !== 422 ) throw cause ;
465+ if ( [ "mcp_app_resource_changed" , "mcp_app_refresh_denied" ] . includes ( cause . code ) ) {
466+ fallbackEndpoint = current . endpoint ;
467+ clearDocument ( { phase : "loading" } ) ;
468+ attempt . document = undefined ;
469+ // Only the lease went stale (expired, or its connection settings changed):
470+ // launch again for the same document, as an expired lease does.
471+ } else if ( ! [ "stale_launch_context" , "missing_launch_context" ] . includes ( cause . code ) ) throw cause ;
452472 }
453473 }
454474 if ( ! target ) {
@@ -531,6 +551,7 @@ function McpAppTileContent({
531551 } ,
532552 } ) ;
533553 attempt . promise = promise ;
554+ wakeLaunchWaiters ( ) ;
534555 const markSettled = ( ) => { if ( launchRef . current === attempt ) attempt . promise = null ; } ;
535556 void promise . then ( markSettled , markSettled ) ;
536557 void promise . then ( next => {
@@ -609,7 +630,7 @@ function McpAppTileContent({
609630 attempt . controller . signal . addEventListener ( "abort" , stopCredentialRetry , { once : true } ) ;
610631 }
611632 } ) ;
612- } , [ cacheScopeKey , entry , launchArguments , argumentsSignature , manualLaunch , nonce , started , geometry . ref , clearDocument , releaseLaunch , releaseLaunches , updateState , updateRefresh , requestRefresh ] ) ;
633+ } , [ cacheScopeKey , entry , launchArguments , argumentsSignature , manualLaunch , nonce , started , geometry . ref , clearDocument , releaseLaunch , releaseLaunches , updateState , updateRefresh , requestRefresh , wakeLaunchWaiters ] ) ;
613634
614635 useEffect ( ( ) => {
615636 if ( manualLaunch ) return ;
0 commit comments