From 1b69bc2719a0534869810712579240d0bb5b1612 Mon Sep 17 00:00:00 2001 From: JOJO <1498581755@qq.com> Date: Sat, 15 Nov 2025 17:20:26 +0800 Subject: [PATCH] fix: propagate manual termination status --- modules/sub_agent_manager.py | 31 +++++++++++++++++++++++++------ sub_agent/web_server.py | 7 ++++++- 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/modules/sub_agent_manager.py b/modules/sub_agent_manager.py index 6b01cf7..3a785c3 100644 --- a/modules/sub_agent_manager.py +++ b/modules/sub_agent_manager.py @@ -174,8 +174,10 @@ class SubAgentManager: if not task: return {"success": False, "error": "未找到对应的子智能体任务"} - if task.get("status") in TERMINAL_STATUSES and task.get("final_result"): - return task["final_result"] + if task.get("status") in TERMINAL_STATUSES or task.get("status") == "terminated": + 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 deadline = time.time() + timeout_seconds @@ -188,7 +190,7 @@ class SubAgentManager: time.sleep(SUB_AGENT_STATUS_POLL_INTERVAL) continue - if status in {"completed", "failed", "timeout"}: + if status in {"completed", "failed", "timeout", "terminated"}: break time.sleep(SUB_AGENT_STATUS_POLL_INTERVAL) @@ -230,7 +232,9 @@ class SubAgentManager: "message": response.get("message") or "子智能体已被强制关闭。", } 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") return response @@ -363,7 +367,7 @@ class SubAgentManager: updates: List[Dict] = [] pending_tasks = [ 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)}") if not pending_tasks: @@ -408,7 +412,7 @@ class SubAgentManager: def _finalize_task(self, task: Dict, service_payload: Dict, status: str) -> Dict: 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 task["status"] = status @@ -417,6 +421,21 @@ class SubAgentManager: deliverables_dir = Path(service_payload.get("deliverables_dir") or task.get("deliverables_dir", "")) 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": result = { "success": False, diff --git a/sub_agent/web_server.py b/sub_agent/web_server.py index 39375a0..45f89cb 100644 --- a/sub_agent/web_server.py +++ b/sub_agent/web_server.py @@ -2158,7 +2158,12 @@ def force_terminate_sub_agent(task_id: str) -> Dict[str, Any]: "message": "子智能体已被手动关闭" }) _purge_sub_agent_task(task_id) - return {"success": True, "message": "子智能体已被手动关闭"} + return { + "success": True, + "status": "terminated", + "message": "子智能体已被手动关闭", + "system_message": "🛑 子智能体已被手动关闭。" + } # ==========================================