fix: propagate manual termination status

This commit is contained in:
JOJO 2025-11-15 17:20:26 +08:00
parent 45ba4f5a49
commit 1b69bc2719
2 changed files with 31 additions and 7 deletions

View File

@ -174,8 +174,10 @@ class SubAgentManager:
if not task: if not task:
return {"success": False, "error": "未找到对应的子智能体任务"} return {"success": False, "error": "未找到对应的子智能体任务"}
if task.get("status") in TERMINAL_STATUSES and task.get("final_result"): if task.get("status") in TERMINAL_STATUSES or task.get("status") == "terminated":
return task["final_result"] if task.get("final_result"):
return task["final_result"]
return {"success": False, "status": task.get("status"), "message": "子智能体已结束。"}
timeout_seconds = timeout_seconds or task.get("timeout_seconds") or SUB_AGENT_DEFAULT_TIMEOUT timeout_seconds = timeout_seconds or task.get("timeout_seconds") or SUB_AGENT_DEFAULT_TIMEOUT
deadline = time.time() + timeout_seconds deadline = time.time() + timeout_seconds
@ -188,7 +190,7 @@ class SubAgentManager:
time.sleep(SUB_AGENT_STATUS_POLL_INTERVAL) time.sleep(SUB_AGENT_STATUS_POLL_INTERVAL)
continue continue
if status in {"completed", "failed", "timeout"}: if status in {"completed", "failed", "timeout", "terminated"}:
break break
time.sleep(SUB_AGENT_STATUS_POLL_INTERVAL) time.sleep(SUB_AGENT_STATUS_POLL_INTERVAL)
@ -230,7 +232,9 @@ class SubAgentManager:
"message": response.get("message") or "子智能体已被强制关闭。", "message": response.get("message") or "子智能体已被强制关闭。",
} }
self._save_state() self._save_state()
if "system_message" not in response: if "system_message" not in response:
response["system_message"] = response.get("message") or "🛑 子智能体已被手动关闭。"
elif "system_message" not in response:
response["system_message"] = response.get("message") response["system_message"] = response.get("message")
return response return response
@ -363,7 +367,7 @@ class SubAgentManager:
updates: List[Dict] = [] updates: List[Dict] = []
pending_tasks = [ pending_tasks = [
task for task in self.tasks.values() task for task in self.tasks.values()
if task.get("status") not in TERMINAL_STATUSES if task.get("status") not in TERMINAL_STATUSES.union({"terminated"})
] ]
logger.debug(f"[SubAgentManager] 待检查任务: {len(pending_tasks)}") logger.debug(f"[SubAgentManager] 待检查任务: {len(pending_tasks)}")
if not pending_tasks: if not pending_tasks:
@ -408,7 +412,7 @@ class SubAgentManager:
def _finalize_task(self, task: Dict, service_payload: Dict, status: str) -> Dict: def _finalize_task(self, task: Dict, service_payload: Dict, status: str) -> Dict:
existing_result = task.get("final_result") existing_result = task.get("final_result")
if existing_result and task.get("status") in TERMINAL_STATUSES: if existing_result and task.get("status") in TERMINAL_STATUSES.union({"terminated"}):
return existing_result return existing_result
task["status"] = status task["status"] = status
@ -417,6 +421,21 @@ class SubAgentManager:
deliverables_dir = Path(service_payload.get("deliverables_dir") or task.get("deliverables_dir", "")) deliverables_dir = Path(service_payload.get("deliverables_dir") or task.get("deliverables_dir", ""))
logger.debug(f"[SubAgentManager] finalize task={task['task_id']} status={status}") logger.debug(f"[SubAgentManager] finalize task={task['task_id']} status={status}")
if status == "terminated":
system_message = service_payload.get("system_message") or "🛑 子智能体已被手动关闭。"
result = {
"success": False,
"task_id": task["task_id"],
"agent_id": task["agent_id"],
"status": "terminated",
"message": message or "子智能体已被手动关闭。",
"details": service_payload,
"sub_conversation_id": task.get("sub_conversation_id"),
"system_message": system_message,
}
task["final_result"] = result
return result
if status != "completed": if status != "completed":
result = { result = {
"success": False, "success": False,

View File

@ -2158,7 +2158,12 @@ def force_terminate_sub_agent(task_id: str) -> Dict[str, Any]:
"message": "子智能体已被手动关闭" "message": "子智能体已被手动关闭"
}) })
_purge_sub_agent_task(task_id) _purge_sub_agent_task(task_id)
return {"success": True, "message": "子智能体已被手动关闭"} return {
"success": True,
"status": "terminated",
"message": "子智能体已被手动关闭",
"system_message": "🛑 子智能体已被手动关闭。"
}
# ========================================== # ==========================================