@@ -4143,7 +4143,9 @@ export default function ChatView(props: ChatViewProps) {
41434143 if ( ! activeThreadRef || ! supportsThreadPullRequests ) return ;
41444144 useRightPanelStore . getState ( ) . open ( activeThreadRef , "pull-requests" ) ;
41454145 } , [ activeThreadRef , supportsThreadPullRequests ] ) ;
4146- const { state : deviceState } = useDeviceState ( activeThreadRef ?. environmentId ?? null ) ;
4146+ const { state : deviceState , loaded : deviceStateLoaded } = useDeviceState (
4147+ activeThreadRef ?. environmentId ?? null ,
4148+ ) ;
41474149 const [ deviceSetupThread , setDeviceSetupThread ] = useState < ScopedThreadRef | null > ( null ) ;
41484150 const addDeviceSurface = useCallback ( ( ) => {
41494151 if ( ! activeThreadRef ) return ;
@@ -4153,24 +4155,53 @@ export default function ChatView(props: ChatViewProps) {
41534155 }
41544156 useRightPanelStore . getState ( ) . open ( activeThreadRef , "device" ) ;
41554157 } , [ activeThreadRef , deviceState . onboardingCompleted , deviceState . hostStatus ] ) ;
4156- // An agent's `device_open` surfaces in every client the same way a
4157- // `preview_open` does: the thread starts a device or gains a session and the panel
4158- // opens on it. Closing the last session leaves the tab in place so the
4159- // user keeps their picker; only new sessions raise the panel.
4160- const threadDeviceSessionCount = activeThreadRef
4161- ? deviceState . sessions . filter ( ( session ) => session . threadId === activeThreadRef . threadId )
4162- . length +
4163- ( deviceState . bootingDevices ?. filter ( ( device ) => device . threadId === activeThreadRef . threadId )
4164- . length ?? 0 )
4165- : 0 ;
4166- const previousDeviceSessionCount = useRef ( threadDeviceSessionCount ) ;
4158+ // Reconcile new server sessions into separate tabs, including sessions opened
4159+ // by an agent or another client. The first snapshot is a baseline: persisted
4160+ // tabs restore themselves, and existing sessions must not resurrect closed tabs.
4161+ const previousDeviceSessions = useRef ( new Map < string , Set < string > > ( ) ) ;
41674162 useEffect ( ( ) => {
4168- const previous = previousDeviceSessionCount . current ;
4169- previousDeviceSessionCount . current = threadDeviceSessionCount ;
4170- if ( ! activeThreadRef || threadDeviceSessionCount <= previous ) return ;
4171- if ( shouldUseRightPanelSheet ) return ;
4172- useRightPanelStore . getState ( ) . open ( activeThreadRef , "device" ) ;
4173- } , [ activeThreadRef , shouldUseRightPanelSheet , threadDeviceSessionCount ] ) ;
4163+ if ( ! activeThreadRef || ! deviceStateLoaded ) return ;
4164+ const threadKey = `${ activeThreadRef . environmentId } :${ activeThreadRef . threadId } ` ;
4165+ const sessions = deviceState . sessions . filter (
4166+ ( session ) => session . threadId === activeThreadRef . threadId ,
4167+ ) ;
4168+ const key = ( session : ( typeof sessions ) [ number ] ) => `${ session . hostId } :${ session . deviceId } ` ;
4169+ const previous = previousDeviceSessions . current . get ( threadKey ) ;
4170+ previousDeviceSessions . current . set ( threadKey , new Set ( sessions . map ( key ) ) ) ;
4171+ if ( ! previous || shouldUseRightPanelSheet ) return ;
4172+ for ( const session of sessions ) {
4173+ if ( previous ?. has ( key ( session ) ) ) continue ;
4174+ const existing = useRightPanelStore
4175+ . getState ( )
4176+ . byThreadKey [ scopedThreadKey ( activeThreadRef ) ] ?. surfaces . some (
4177+ ( surface ) =>
4178+ surface . kind === "device" &&
4179+ surface . target ?. hostId === session . hostId &&
4180+ surface . target . deviceId === session . deviceId ,
4181+ ) ;
4182+ if ( existing ) continue ;
4183+ const device = deviceState . devices . find (
4184+ ( entry ) => entry . hostId === session . hostId && entry . id === session . deviceId ,
4185+ ) ;
4186+ if ( ! device ) continue ;
4187+ useRightPanelStore . getState ( ) . openDevice (
4188+ activeThreadRef ,
4189+ {
4190+ hostId : session . hostId ,
4191+ deviceId : session . deviceId ,
4192+ platform : device . platform ,
4193+ name : device . name ,
4194+ } ,
4195+ true ,
4196+ ) ;
4197+ }
4198+ } , [
4199+ activeThreadRef ,
4200+ deviceStateLoaded ,
4201+ shouldUseRightPanelSheet ,
4202+ deviceState . sessions ,
4203+ deviceState . devices ,
4204+ ] ) ;
41744205 const openFileSurface = useCallback (
41754206 ( relativePath : string ) => {
41764207 if ( ! activeThreadRef || ! activeProject ) return ;
@@ -8142,7 +8173,8 @@ export default function ChatView(props: ChatViewProps) {
81428173 < DevicePanel
81438174 mode = "embedded"
81448175 threadRef = { activeThreadRef }
8145- deviceId = { null }
8176+ key = { renderedRightPanelSurface . id }
8177+ surface = { renderedRightPanelSurface }
81468178 visible = { rightPanelOpen }
81478179 onDismissSetup = { ( ) => {
81488180 closeRightPanelSurface ( renderedRightPanelSurface ) ;
@@ -8714,6 +8746,10 @@ export default function ChatView(props: ChatViewProps) {
87148746 terminalLabelsById = { activeTerminalLabelsById }
87158747 onActivate = { activateRightPanelSurface }
87168748 onCloseSurface = { closeRightPanelSurface }
8749+ onRenameDevice = { ( surfaceId , title ) => {
8750+ if ( activeThreadRef )
8751+ useRightPanelStore . getState ( ) . renameDevice ( activeThreadRef , surfaceId , title ) ;
8752+ } }
87178753 onCloseOtherSurfaces = { closeOtherRightPanelSurfaces }
87188754 onCloseSurfacesToRight = { closeRightPanelSurfacesToRight }
87198755 onCloseAllSurfaces = { closeAllRightPanelSurfaces }
@@ -8768,6 +8804,10 @@ export default function ChatView(props: ChatViewProps) {
87688804 terminalLabelsById = { activeTerminalLabelsById }
87698805 onActivate = { activateRightPanelSurface }
87708806 onCloseSurface = { closeRightPanelSurface }
8807+ onRenameDevice = { ( surfaceId , title ) => {
8808+ if ( activeThreadRef )
8809+ useRightPanelStore . getState ( ) . renameDevice ( activeThreadRef , surfaceId , title ) ;
8810+ } }
87718811 onCloseOtherSurfaces = { closeOtherRightPanelSurfaces }
87728812 onCloseSurfacesToRight = { closeRightPanelSurfacesToRight }
87738813 onCloseAllSurfaces = { closeAllRightPanelSurfaces }
0 commit comments