diff --git a/server/chat_flow_task_main.py b/server/chat_flow_task_main.py index 8859e70..cf27bc1 100644 --- a/server/chat_flow_task_main.py +++ b/server/chat_flow_task_main.py @@ -1695,6 +1695,25 @@ async def handle_task_with_sender( socketio.start_background_task(run_bg_poll) has_running_completion_jobs = has_running_sub_agents or has_running_background_commands + + # 任务结束时清理残留的 pending runtime mode(权限/执行环境/网络权限)。 + # 场景:用户在模型最后一次输出(无工具调用)后、task 仍为 running 状态时切换了 + # 权限/执行环境,此时会进入 queue_*_change 路径只设置 pending,但 task 随即自然 + # 结束,不会进入 execute_tool_calls → apply_pending_runtime_mode_changes。 + # 若不在此清理,pending 会残留到下一次工作首次工具调用时才被应用, + # 并把"变更通知"作为 user 消息注入到新一轮对话中(bug)。 + # 正确行为:task 结束时立即应用 pending(等同空闲期切换),但不生成通知。 + try: + if hasattr(web_terminal, "apply_pending_runtime_mode_changes"): + residual_notices = web_terminal.apply_pending_runtime_mode_changes() or [] + if residual_notices: + debug_log( + "[RuntimeMode] task 结束时应用残留 pending runtime mode,丢弃通知: " + f"{[n.get('source') if isinstance(n, dict) else n for n in residual_notices]}" + ) + except Exception as exc: + debug_log(f"[RuntimeMode] task 结束时清理 pending runtime mode 失败: {exc}") + pending_runtime_guidance_messages: List[str] = [] try: from .tasks import task_manager