diff --git a/server/chat_flow_task_main.py b/server/chat_flow_task_main.py index 95550db..c5720a6 100644 --- a/server/chat_flow_task_main.py +++ b/server/chat_flow_task_main.py @@ -577,6 +577,16 @@ async def handle_task_with_sender(terminal: WebTerminal, workspace: UserWorkspac iteration = 0 while max_iterations is None or iteration < max_iterations: + # 检查停止标志 + stop_entry = get_stop_flag(client_sid, username) + if stop_entry and stop_entry.get('stop'): + debug_log(f"[Task] 检测到停止标志,退出循环") + sender('task_stopped', { + 'message': '任务已停止', + 'reason': 'user_requested' + }) + break + current_iteration = iteration + 1 iteration += 1 total_iterations += 1 diff --git a/server/tasks.py b/server/tasks.py index 8160c84..4d08ec5 100644 --- a/server/tasks.py +++ b/server/tasks.py @@ -177,6 +177,9 @@ class TaskManager: with self._lock: rec.status = "cancel_requested" rec.updated_at = time.time() + # 清空事件队列,防止新任务读取到旧事件 + rec.events.clear() + debug_log(f"[Task] 任务 {task_id} 已取消,事件队列已清空") return True # ---- internal helpers ----