@@ -246,15 +246,31 @@ async def _handle_start_session(
246246 }
247247 sent_to_extension = False
248248
249+ # *** FIX: Aggressively clear ALL stale session_ids before looking ***
250+ # This handles crashed CLI runs that leave stale state
251+ for client_id , client_conn in self ._connections .items ():
252+ # Clear session_id on all connections that aren't the current CLI caller
253+ # This ensures fresh state for each new CLI run
254+ if client_conn != conn and client_conn .session_id :
255+ logger .info (f"Clearing stale session_id on client { client_id [:8 ]} " )
256+ client_conn .session_id = None
257+
249258 # First, log all connections for debugging
250259 logger .info (f"Looking for available extension. Connections: { len (self ._connections )} " )
251260 for client_id , client_conn in self ._connections .items ():
252261 has_session = "has session" if client_conn .session_id else "no session"
253262 is_caller = "caller" if client_conn == conn else "not caller"
254263 logger .debug (f" Client { client_id [:8 ]} : { has_session } , { is_caller } " )
255-
264+
265+
256266 # Try to find an available extension
267+ logger .info (f"[DEBUG] Scanning { len (self ._connections )} connections for available extension" )
257268 for client_id , client_conn in self ._connections .items ():
269+ is_self = client_conn == conn
270+ has_websocket = client_conn .websocket is not None
271+ has_session = client_conn .session_id is not None
272+ logger .info (f"[DEBUG] Client { client_id [:8 ]} : is_self={ is_self } , websocket={ has_websocket } , session={ has_session } " )
273+
258274 # Only send to extensions (not CLI) that don't have an active session
259275 if client_conn != conn and client_conn .websocket and not client_conn .session_id :
260276 try :
0 commit comments