@@ -195,10 +195,6 @@ async def get_initial_state(self, session: MCPSession) -> Any:
195195 Returns:
196196 Initial observation/state
197197 """
198- method_start = time .time ()
199- session_id_short = session .session_id [:8 ] if len (session .session_id ) > 8 else session .session_id
200- logger .info (f"### 🌟 GET_INITIAL_STATE_START: timestamp: { method_start } , session_id: { session_id_short } ..." )
201-
202198 if not session ._mcp_session :
203199 raise RuntimeError ("Session not initialized" )
204200
@@ -207,105 +203,53 @@ async def get_initial_state(self, session: MCPSession) -> Any:
207203
208204 try :
209205 # Extract base URL and session ID from the MCP session
210- url_extract_start = time .time ()
211- logger .info (
212- f"### 🔍 URL_EXTRACT_START: timestamp: { url_extract_start } , elapsed: { url_extract_start - method_start :.6f} s, session_id: { session_id_short } ..."
213- )
214-
215206 base_url = session .base_url .rstrip ("/" ).removesuffix ("/mcp" )
216207 session_id = session .session_id
217208
218- url_extract_end = time .time ()
219- logger .info (
220- f"### 🔍 URL_EXTRACT_END: timestamp: { url_extract_end } , elapsed: { url_extract_end - method_start :.6f} s, duration: { url_extract_end - url_extract_start :.6f} s, base_url: { base_url } , session_id: { session_id_short } ..."
221- )
222-
223209 if session_id :
224- headers_start = time .time ()
225- logger .info (
226- f"### 🔍 HEADERS_CREATE_START: timestamp: { headers_start } , elapsed: { headers_start - method_start :.6f} s, session_id: { session_id_short } ..."
227- )
228-
229210 headers = {"mcp-session-id" : session_id }
230211
231- headers_end = time .time ()
232- logger .info (
233- f"### 🔍 HEADERS_CREATE_END: timestamp: { headers_end } , elapsed: { headers_end - method_start :.6f} s, duration: { headers_end - headers_start :.6f} s, session_id: { session_id_short } ..."
234- )
235-
236212 # Query initial state endpoint
237213 try :
238- timeout_start = time .time ()
239- logger .info (
240- f"### 🔍 TIMEOUT_CONFIG_START: timestamp: { timeout_start } , elapsed: { timeout_start - method_start :.6f} s, session_id: { session_id_short } ..."
241- )
242-
243214 # Use shorter timeout for playback mode, longer timeout for high-concurrency initialization
244215 # (50+ concurrent sessions need more time for initial state setup)
245216 timeout = 3.0 if hasattr (session , "_is_playback_mode" ) and session ._is_playback_mode else 15.0
246217
247- timeout_end = time .time ()
248- logger .info (
249- f"### 🔍 TIMEOUT_CONFIG_END: timestamp: { timeout_end } , elapsed: { timeout_end - method_start :.6f} s, duration: { timeout_end - timeout_start :.6f} s, timeout: { timeout } s, session_id: { session_id_short } ..."
250- )
251-
252- # TIMING: Get shared client
253- # client = await self._get_shared_client(timeout)
254-
255- # TIMING: HTTP request with shared client
256- request_start = time .time ()
257- logger .info (
258- f"### 🌐 HTTP_REQUEST_START: timestamp: { request_start } , elapsed: { request_start - method_start :.6f} s, url: { base_url } /control/initial_state, session_id: { session_id_short } ..."
259- )
260-
261- timeout = 3.0 if hasattr (session , "_is_playback_mode" ) and session ._is_playback_mode else 15.0
262-
263218 async with httpx .AsyncClient (timeout = timeout ) as client :
264219 initial_state_response = await client .get (
265220 f"{ base_url } /control/initial_state" ,
266221 headers = headers ,
267222 timeout = timeout ,
268223 )
269- request_time = time .time () - request_start
270-
271- request_end = time .time ()
272- logger .info (
273- f"### 🌐 HTTP_REQUEST_END: timestamp: { request_end } , elapsed: { request_end - method_start :.6f} s, duration: { request_time :.6f} s, status_code: { initial_state_response .status_code } , session_id: { session_id_short } ..."
274- )
275224
276225 if initial_state_response .status_code == 200 :
277226 initial_observation = initial_state_response .json ()
278- success_end = time .time ()
279227 logger .info (
280- f"### ✅ RETURN: timestamp: { success_end } , total_duration: { success_end - method_start :.6f } s, session_id: { session_id_short } ... "
228+ f"Session { session . session_id } : ✅ Successfully fetched session-aware initial state from control plane endpoint "
281229 )
282- # return initial_observation
283230 else :
284- error_time = time .time ()
285231 logger .warning (
286- f"### ⚠️ HTTP_ERROR_RESPONSE: timestamp: { error_time } , elapsed: { error_time - method_start :.6f } s, status_code: { initial_state_response .status_code } , session_id: { session_id_short } "
232+ f"Control plane initial state endpoint returned { initial_state_response .status_code } "
287233 )
288234 except httpx .TimeoutException :
289- timeout_error_time = time .time ()
290- logger .warning (
291- f"### ⏰ HTTP_TIMEOUT: timestamp: { timeout_error_time } , elapsed: { timeout_error_time - method_start :.6f} s, timeout: { timeout } s, session_id: { session_id_short } "
292- )
235+ logger .warning (f"Control plane initial state endpoint timed out after { timeout } s" )
293236 except Exception as e :
294- http_error_time = time .time ()
295- logger .warning (
296- f"### ❌ HTTP_ERROR: timestamp: { http_error_time } , elapsed: { http_error_time - method_start :.6f} s, error: { str (e )} , session_id: { session_id_short } "
297- )
298-
237+ logger .warning (f"Failed to query control plane initial state endpoint: { e } " )
299238 except Exception as e :
300- general_error_time = time .time ()
301- logger .warning (
302- f"### ❌ GENERAL_ERROR: timestamp: { general_error_time } , elapsed: { general_error_time - method_start :.6f} s, error: { str (e )} , session_id: { session_id_short } "
303- )
304-
305- method_end = time .time ()
306- logger .info (
307- f"### 🔴 GET_INITIAL_STATE_END: timestamp: { method_end } , total_duration: { method_end - method_start :.6f} s, session_id: { session_id_short } ..."
308- )
239+ logger .warning (f"Failed to query control plane initial state endpoint: { e } " )
240+
241+ # Fallback to MCP resource if control plane endpoint fails (backward compatibility)
242+ if initial_observation is None :
243+ logger .debug (f"Session { session .session_id } : Falling back to MCP resource for initial state" )
244+ initial_observation = await self ._get_initial_state_from_mcp_resource (session )
245+
246+ # Ensure we have some observation
247+ if initial_observation is None :
248+ logger .debug (f"Session { session .session_id } : Using default initial state" )
249+ initial_observation = {
250+ "observation" : "default_initial_state" ,
251+ "session_id" : session .session_id ,
252+ }
309253
310254 return initial_observation
311255
0 commit comments