@@ -92,6 +92,19 @@ function workspaceDescriptorToInstance(descriptor: WorkspaceDescriptor): Instanc
9292 }
9393}
9494
95+ function ensureActiveInstanceSelected ( ) : void {
96+ const current = activeInstanceId ( )
97+ const instanceMap = instances ( )
98+ if ( current && instanceMap . has ( current ) ) return
99+
100+ for ( const [ id , instance ] of instanceMap . entries ( ) ) {
101+ if ( instance . status === "ready" ) {
102+ setActiveInstanceId ( id )
103+ return
104+ }
105+ }
106+ }
107+
95108function upsertWorkspace ( descriptor : WorkspaceDescriptor ) {
96109 const mapped = workspaceDescriptorToInstance ( descriptor )
97110 if ( instances ( ) . has ( descriptor . id ) ) {
@@ -102,6 +115,9 @@ function upsertWorkspace(descriptor: WorkspaceDescriptor) {
102115
103116 if ( descriptor . status === "ready" ) {
104117 attachClient ( descriptor )
118+ // If no tab is currently selected (common after UI refresh),
119+ // auto-select the first ready instance.
120+ ensureActiveInstanceSelected ( )
105121 }
106122}
107123
@@ -225,15 +241,18 @@ async function hydrateInstanceData(instanceId: string) {
225241 }
226242}
227243
228- void ( async function initializeWorkspaces ( ) {
244+ void ( async function initializeWorkspaces ( ) {
229245 try {
230246 const workspaces = await serverApi . fetchWorkspaces ( )
231247 workspaces . forEach ( ( workspace ) => upsertWorkspace ( workspace ) )
248+ // After a UI refresh, we may have instances but no active selection.
249+ ensureActiveInstanceSelected ( )
232250 } catch ( error ) {
233251 log . error ( "Failed to load workspaces" , error )
234252 }
235253} ) ( )
236254
255+
237256serverEvents . on ( "*" , ( event ) => handleWorkspaceEvent ( event ) )
238257
239258function handleWorkspaceEvent ( event : WorkspaceEventPayload ) {
0 commit comments