@@ -392,6 +392,8 @@ async def _run_subagent():
392392 _run_subagent (), timeout = execution_timeout
393393 )
394394 except asyncio .TimeoutError :
395+ # 若超时,保存已产生的部分历史
396+ cls ._save_subagent_history (agent_name , runner_messages , umo )
395397 error_msg = f"SubAgent '{ agent_name } ' execution timeout after { execution_timeout :.1f} seconds."
396398 logger .warning (f"[SubAgentTimeout] { error_msg } " )
397399
@@ -528,7 +530,6 @@ async def _run():
528530 is_enhanced = cls ._is_enhanced_subagent (umo , agent_name )
529531 if is_enhanced :
530532 await cls ._handle_enhanced_subagent_background_result (
531- cls = cls ,
532533 umo = umo ,
533534 agent_name = agent_name ,
534535 task_id = tool_args .get ("subagent_task_id" ),
@@ -917,7 +918,9 @@ def _build_background_submission_message(
917918 original_task_id : str ,
918919 subagent_task_id : str | None ,
919920 ) -> mcp .types .TextContent :
920- if subagent_task_id :
921+ if subagent_task_id and not subagent_task_id .startswith (
922+ "__PENDING_TASK_CREATE_FAILED__"
923+ ):
921924 return mcp .types .TextContent (
922925 type = "text" ,
923926 text = (
@@ -969,7 +972,7 @@ def _is_enhanced_subagent(umo: str, agent_name: str | None) -> bool:
969972 return True
970973 return False
971974
972- @staticmethod
975+ @classmethod
973976 async def _handle_enhanced_subagent_background_result (
974977 cls ,
975978 * ,
@@ -985,8 +988,20 @@ async def _handle_enhanced_subagent_background_result(
985988 ) -> None :
986989 from astrbot .core .dynamic_subagent_manager import DynamicSubAgentManager
987990
991+ success = error_text is None
992+ status = "COMPLETED" if success else "FAILED"
988993 DynamicSubAgentManager .set_subagent_status (
989- session_id = umo , agent_name = agent_name , status = "FAILED"
994+ session_id = umo , agent_name = agent_name , status = status
995+ )
996+
997+ DynamicSubAgentManager .store_subagent_result (
998+ session_id = umo ,
999+ agent_name = agent_name ,
1000+ success = success ,
1001+ result = result_text ,
1002+ task_id = task_id ,
1003+ error = error_text ,
1004+ execution_time = execution_time ,
9901005 )
9911006
9921007 if not await cls ._maybe_wake_main_agent_after_background (
@@ -1020,9 +1035,9 @@ async def _maybe_wake_main_agent_after_background(
10201035 )
10211036 else :
10221037 main_agent_is_running = False
1023- except Exception as e : # noqa: BLE001
1038+ except Exception as e :
10241039 logger .error ("Failed to check main agent status: %s" , e )
1025- main_agent_is_running = True
1040+ main_agent_is_running = False # 异常时尝试通知,避免结果丢失
10261041
10271042 if main_agent_is_running :
10281043 return False
0 commit comments