sync update from LAN repo (source master @ ca48c4ec)

This commit is contained in:
gozeros committed 2026-11-05 10:53:54 +08:00
1 parent 8bc8b0e082
commit ea9dcff9c0
873 files changed
+107866 -14011

No files matched your search

@@ -61,7 +61,6 @@ from typing import Any
import httpx
from fastapi import APIRouter, Body, HTTPException, Request
from fastapi.responses import StreamingResponse
from langgraph.checkpoint.base import empty_checkpoint
from app.gateway.roundtable_diag import record_foreground_diag
from app.gateway.roundtable_model_fallback import fallback_model_chain, is_llm_error_message, reason_text
@@ -82,11 +81,18 @@ from deerflow.agents.roundtable_orchestrator.extraction_spec import build_summar
from deerflow.config import get_app_config
from deerflow.runtime.user_context import get_effective_user_id
from deerflow.tools.builtins.clarification_utils import resolve_allow_multiple
from deerflow.utils.time import now_iso
logger = logging.getLogger(__name__)
router = APIRouter(prefix="/api/multi-agent", tags=["multi-agent"])
_STEP3_AGENT_IDS = frozenset({
ACTION_PLAN_AGENT_ID,
DASHBOARD_AGENT_ID,
REPORT_AGENT_ID,
STRUCTURE_AGENT_ID,
SUMMARY_AGENT_ID,
})
def _stage_for_agent(agent_type: str, agent_name: str) -> str:
"""把一次 /run/stream 的 (agent_type, agent_name) 映射成诊断日志的 stage。
@@ -107,12 +113,62 @@ def _stage_for_agent(agent_type: str, agent_name: str) -> str:
return "step2_leader" if agent_type == "leader" else "step2_seat"
def _is_step2_room_limited(
agent_type: str,
agent_name: str,
*,
finalize_only: bool = False,
) -> bool:
"""Only the actual Step-2 coordinator and seats consume a room.
The Step-3 singleton agents reuse the same ``/run/stream`` endpoint, so
this boundary must live here rather than at the route level.
"""
# 最后总控只把已经完成的席位交付整理成正文,不再占用席位执行资源。允许它在
# 长耗时席位跑过房间 hard deadline 后仍完成收口,避免最后一步被 409 踢回第一步。
if agent_type == "leader" and finalize_only:
return False
return agent_type == "leader" or agent_name not in _STEP3_AGENT_IDS
# 圆桌(intent / recommend / 席位 / 协调)创建的 thread 都是功能型内部线程,
# 不是用户的普通对话。打上这个 metadata,前端「最近的对话」列表(recent-chat-list.tsx)
# 据 `metadata.thread_type` 过滤掉,与 scheduler / bootstrap 系统会话同一套机制。
_ROUNDTABLE_THREAD_METADATA: dict[str, Any] = {"thread_type": "roundtable", "system": True}
def _room_thread_metadata(
*,
draft_id: str | None,
user_id: str | None,
task_id: str | None,
room_session_id: str | None,
) -> dict[str, Any]:
"""Step-2 threads carry a stable room key so later occupancy can see live runs."""
from deerflow.persistence.roundtable_jobs.sql import compute_active_dedupe_key
from deerflow.persistence.roundtable_rooms.sql import compute_room_session_key
meta = dict(_ROUNDTABLE_THREAD_METADATA)
clean_session_id = (room_session_id or "").strip() or None
key = compute_room_session_key(clean_session_id) or compute_active_dedupe_key(
draft_id, user_id, task_id
)
if key:
meta["roundtable_room_key"] = key
if clean_session_id:
meta["roundtable_room_session_id"] = clean_session_id
if draft_id:
meta["draft_id"] = draft_id
if task_id:
meta["task_id"] = task_id
return meta
def _step3_thread_metadata() -> dict[str, Any]:
"""Step-3 threads are internal threads, but never Step-2 room occupants."""
return dict(_ROUNDTABLE_THREAD_METADATA)
# ---------------------------------------------------------------------------
# 共享:异常诊断器(intent/recommend/multi-agent 共用)
# ---------------------------------------------------------------------------
@@ -415,59 +471,17 @@ def _resolve_user_id(request: Request) -> str:
async def _create_thread_direct(request: Request, *, metadata: dict[str, Any] | None = None) -> str:
"""同进程版的 ``POST /api/threads``。
完全镜像 ``routers/threads.create_thread`` 的写盘动作(thread_meta +
空 checkpoint),但跳过 TCP + Pydantic 反序列化。仅供 init 阶段批量建席位
线程用,失败时抛 HTTPException 由 SSE 帧上报。
``metadata`` 写入 thread 元数据;init 传 ``_ROUNDTABLE_THREAD_METADATA`` 把
席位 / 协调线程标记成系统会话,从前端「最近的对话」里隐藏。
只创建业务元数据,首轮图运行负责初始化 checkpoint。
metadata 的任务共享/系统会话规则与原入口保持一致。
"""
from app.gateway.deps import get_checkpointer, get_thread_store
from app.gateway.deps import get_thread_store
checkpointer = get_checkpointer(request)
thread_store = get_thread_store(request)
thread_id = str(uuid.uuid4())
now = now_iso()
try:
await thread_store.create(thread_id, assistant_id=None, metadata=metadata or {})
await get_thread_store(request).create(thread_id, assistant_id=None, metadata=metadata or {})
except Exception:
logger.exception("[init-direct] thread_meta create failed for %s", thread_id)
raise HTTPException(status_code=500, detail="Failed to create thread")
config = {"configurable": {"thread_id": thread_id, "checkpoint_ns": ""}}
ckpt_metadata = {
"step": -1,
"source": "input",
"writes": None,
"parents": {},
"created_at": now,
}
async def _cleanup_partial_thread() -> None:
delete_checkpoint = getattr(checkpointer, "adelete_thread", None)
if callable(delete_checkpoint):
try:
await delete_checkpoint(thread_id)
except Exception:
logger.exception("[init-direct] partial checkpoint cleanup failed for %s", thread_id)
try:
await thread_store.delete(thread_id)
except Exception:
logger.exception("[init-direct] partial thread_meta cleanup failed for %s", thread_id)
try:
await checkpointer.aput(config, empty_checkpoint(), ckpt_metadata, {})
except asyncio.CancelledError:
# A sibling seat may fail while this task is between metadata commit and
# checkpoint creation. Compensate before propagating cancellation, under
# shield so a second cancellation cannot abort the cleanup half-way.
await asyncio.shield(_cleanup_partial_thread())
raise
except Exception:
logger.exception("[init-direct] checkpoint create failed for %s", thread_id)
await _cleanup_partial_thread()
raise HTTPException(status_code=500, detail="Failed to create thread")
return thread_id
@@ -718,18 +732,23 @@ def _run_payload(
new_message: str,
excluded_tools: list[str],
skill_stop_names: list[str],
allowed_tools: list[str] | None = None,
recursion_limit: int = 1000,
thinking_enabled: bool = True,
reasoning_effort: str = "medium",
subagent_enabled: bool = False,
stream_custom: bool = False,
multitask_strategy: str = "reject",
cancel_on_disconnect: bool = False,
extra_context: dict[str, Any] | None = None,
files: list[dict[str, Any]] | None = None,
roundtable_file_stream: bool = False,
) -> dict[str, Any]:
"""构造 ``POST /api/threads/{thread_id}/runs/stream`` 的请求体。
- ``context.agent_name``:本次 run 使用的 agent(内置 id 或 init 创建的 coordinator id)。
- ``excluded_tools``:按路由裁剪工具面(intent 仅澄清、special 禁派活等)。
- ``allowed_tools``:安全敏感角色的真正白名单;``None`` 保持兼容,空列表禁用全部。
- ``skill_stop_names``:leader 侧配合 SkillStopMiddleware 在 ``agent_orchestration``
调用后截断图,以便网关从 AIMessage.tool_calls 解析派活列表。
- ``thinking_enabled`` / ``reasoning_effort``:默认开 CoT + medium,适合 Step 1 澄清
@@ -754,7 +773,7 @@ def _run_payload(
]
},
"metadata": {"run_scope": RUN_SCOPE_ORCHESTRATION_CHILD},
"config": {"recursion_limit": 1000},
"config": {"recursion_limit": recursion_limit},
"context": {
"agent_name": agent_name,
"model_name": model_name,
@@ -778,6 +797,7 @@ def _run_payload(
# 席位 → run/stream 报 400 "agent_name ... missing from d_agent_thread_id"。
# 圆桌是一次性研讨,本就不该吃历史会话记忆,这里统一关注入根治该污染。
"memory_injection_enabled": False,
"allowed_tools": allowed_tools,
},
# ultra 席位会派子智能体(task 工具),其 task_started/running/completed 进度走
# **custom** 流(StreamWriter)。仅这种场景加 "custom",让前端任务卡片能显示子智能体
@@ -788,13 +808,10 @@ def _run_payload(
"stream_subgraphs": True,
"stream_resumable": True,
"assistant_id": "lead_agent",
"on_disconnect": "continue",
# ⚠️ 圆桌 run 必须能被「下一次同 thread 的 run」抢占。圆桌每个 thread 同一时刻
# 只应有一个活跃 run,但 on_disconnect="continue" 让前端 abort SSE 后**后端 run
# 仍在跑**(为可恢复);此时用户「人工干预」立刻发起新 leader run 打到同一总控
# thread,默认 reject 策略就会 409「already has an active run」→ 被 _stream_upstream
# 包成 502。调用方(leader / special)传 multitask_strategy="rollback",让新 run 先
# 取消该 thread 上没结束的旧 run 再继续 —— 这正是「最新一轮/干预指令优先」的语义。
# 第二步实时会商不支持脱离浏览器继续执行。断网、暂停或前端主动 abort 后必须
# 取消后端 run;第三步等既有调用保持 continue,不改变其断线恢复语义。
"on_disconnect": "cancel" if cancel_on_disconnect else "continue",
# rollback 仍保留,负责处理人工干预与新一轮抢占同一 thread 的极短竞态。
"multitask_strategy": multitask_strategy,
"excluded_tools": excluded_tools,
"skill_stop_names": skill_stop_names,
@@ -803,9 +820,20 @@ def _run_payload(
# 供 read_peer_delivery 工具按需读取)。只进 context dict,不进 prompt → 不调工具就不耗 token。
if extra_context:
payload["context"].update(extra_context)
if roundtable_file_stream:
payload["context"].update(_roundtable_file_stream_context())
return payload
def _roundtable_file_stream_context() -> dict[str, bool]:
"""会商专用文件流式兼容。关闭时不写 context,本轮仍只看全局开关。"""
from deerflow.config.system_settings import get_roundtable_room_limit_settings
if get_roundtable_room_limit_settings().write_file_compatibility_enabled:
return {"roundtable_write_file_compatibility": True}
return {}
# ---------------------------------------------------------------------------
# POST /api/multi-agent/init — Step 2 席位 + 总控初始化(SSE)
# ---------------------------------------------------------------------------
@@ -826,7 +854,7 @@ async def init_multi_agent(request: Request, payload: dict = Body(...)) -> Strea
SSE 事件(每条 ``data: {...}\\n\\n``)::
seat_ready — 某一席位 thread 已创建(``asyncio.as_completed`` 顺序,先完先发)
coordinator_ready — 总控 thread 就绪 + 已注入本次席位列表
coordinator_ready — 总控 thread 元数据就绪,席位列表随首次问答注入
init_done — ``thread_ids`` 全量映射,前端存 ``threadIds`` 并开始编排
error — 失败详情(同步校验失败则直接 HTTP 4xx,不进 SSE)
@@ -837,9 +865,9 @@ async def init_multi_agent(request: Request, payload: dict = Body(...)) -> Strea
在首次调用时自动落地。每次 init **不再创建新的 coordinator agent**(避免
``.deer-flow/agents/`` 累积 ``roundtable-coordinator-<ts36>`` 残留)。
本次圆桌的「可调度席位列表」由 init 在创建好 coordinator thread 之后,以一条
human 消息追加到 thread 历史里,leader run 每轮都能从对话历史里读到。这样
SOUL 长期稳定,席位列表随会话动态注入。前端为兼容旧 client 仍可传
初始化只创建业务线程元数据,不访问 checkpoint。每轮 leader run 根据 thread
映射注入真实席位清单,随实际输入一起持久化。SOUL 长期稳定,席位列表随会话
动态注入。前端为兼容旧 client 仍可传
``main_agent_name`` 字段,但后端会忽略并使用 ``COORDINATOR_AGENT_ID``。
"""
# 调用前自检:与 intent/recommend 一样,缺失时就地补建,避免 /run/stream 报 500。
@@ -847,7 +875,6 @@ async def init_multi_agent(request: Request, payload: dict = Body(...)) -> Strea
agent_names_in = payload.get("agent_names") or []
agent_names: list[str] = [str(name).lower() for name in agent_names_in]
model: str | None = payload.get("model")
# main_agent_name 是历史字段,2026-05 改造后忽略,统一用单例 id。保留接收但不再用前端值,
# 避免下游再次创建带时间戳的临时 coordinator。
main_agent_name = COORDINATOR_AGENT_ID
@@ -858,6 +885,26 @@ async def init_multi_agent(request: Request, payload: dict = Body(...)) -> Strea
for name in agent_names:
_validate_agent_id(name, "agent_names entry")
# The browser already acquired the room before entering Step 2. Init only
# renews that lease; it cannot create a second room or bypass admission.
from app.gateway.roundtable_room_gate import enforce_existing_roundtable_room
room_draft_id = str(payload.get("draft_id") or "").strip() or None
room_task_id = str(payload.get("task_id") or "").strip() or None
room_session_id = str(payload.get("room_session_id") or "").strip() or None
await enforce_existing_roundtable_room(
request,
draft_id=room_draft_id,
task_id=room_task_id,
room_session_id=room_session_id,
)
room_thread_meta = _room_thread_metadata(
draft_id=room_draft_id,
user_id=_resolve_user_id(request),
task_id=room_task_id,
room_session_id=room_session_id,
)
def _frame(payload: dict[str, Any]) -> str:
return f"data: {json.dumps(payload, ensure_ascii=False)}\n\n"
@@ -873,10 +920,10 @@ async def init_multi_agent(request: Request, payload: dict = Body(...)) -> Strea
agent_meta = await _get_agents_batch_direct(request, agent_names)
t_meta_done = time.perf_counter()
# 2) 每个席位 thread 并行创建(直调 thread_store + checkpointer),
# 2) 每个席位 thread 元数据并行创建,不预写空 checkpoint,
# 用 asyncio.as_completed 保持"先完先发 seat_ready"的体验。
async def _prepare_sub_agent(sub_id: str) -> tuple[str, str, str, str]:
thread_id = await _create_thread_direct(request, metadata=_ROUNDTABLE_THREAD_METADATA)
thread_id = await _create_thread_direct(request, metadata=room_thread_meta)
meta = agent_meta.get(sub_id.lower(), {})
return (
sub_id.lower(),
@@ -908,50 +955,11 @@ async def init_multi_agent(request: Request, payload: dict = Body(...)) -> Strea
t_seats_done = time.perf_counter()
agent_threads: dict[str, str] = {sid: thread_id for sid, thread_id, _, _ in prepared}
seat_lines: list[str] = [
f"- agent_name: {sid}({display_name}):{description}"
for sid, _, display_name, description in prepared
]
# 协调智能体改造为**单例**(id = roundtable-coordinator):
# - SOUL 已经由 ensure_roundtable_functional_agents() 在路由入口落地,
# 通用协调规则常驻磁盘,无需每次重写。
# - 本次"可调度席位清单"以 human 消息形式 append 到 coordinator
# thread 历史里,leader run 每轮都能在对话历史最前面看到。
# 这样席位列表随会话动态变化,但磁盘上不再累积 roundtable-coordinator-<ts36>。
#
# 注意:model 字段在 lead_agent run 时通过 context.model_name 注入,
# 不需要写回到 agent config.yaml(否则会污染单例的配置)。
seat_introduction = (
"【圆桌系统初始化】本次研讨可调度的席位清单如下,请严格按 agent_name "
"通过 agent_orchestration 技能派活,不要派给不在清单内的 agent:\n"
+ "\n".join(seat_lines)
)
coord_thread = await _create_thread_direct(request, metadata=_ROUNDTABLE_THREAD_METADATA)
# SOUL 使用固定总控单例。席位清单由 _leader_run 随实际输入注入,
# 初始化不通过 loopback 追加消息,也无需创建任何空 checkpoint。
coord_thread = await _create_thread_direct(request, metadata=room_thread_meta)
agent_threads[main_agent_name] = coord_thread
# 把席位列表注入 coordinator thread 的对话历史。HTTP loopback 调用
# 走 /api/threads/{tid}/state(读+写),开销 ~10-30ms,在 init 整体
# 几百 ms 的尺度上微不足道,但比起手撕 langgraph checkpoint 内部结构
# 风险低、与 _broadcast / _append_thread_message 一套语义,长期可维护。
try:
base = _loopback_base(request)
headers = _auth_headers(request)
async with _make_loopback_client(request) as seed_client:
await _append_thread_message(
seed_client,
base,
headers,
coord_thread,
seat_introduction,
)
except Exception:
# 注入失败不阻断 init:leader run 仍能跑,只是看不到席位清单,
# 可能派活给错的 id。会被 _strip_display_suffix / _validate_agent_id
# 兜底,前端能看到错误帧;运维通过 backend 日志找到根因再修复。
logger.exception("[init-direct] failed to seed coordinator thread with seat list")
t_coord_done = time.perf_counter()
logger.info(
"[init-direct] meta=%.3fs seats=%.3fs coord=%.3fs total=%.3fs seats_count=%d",
@@ -1001,7 +1009,10 @@ async def init_multi_agent(request: Request, payload: dict = Body(...)) -> Strea
@router.post("/report/init")
async def init_report_thread(request: Request) -> dict[str, str]:
async def init_report_thread(
request: Request,
payload: dict | None = Body(default=None),
) -> dict[str, str]:
"""为 Step 3「结果绘制」创建一个供方案可视化总结智能体(``roundtable-report``)
运行的线程,返回 ``{"agent_name", "thread_id"}``。
@@ -1016,7 +1027,10 @@ async def init_report_thread(request: Request) -> dict[str, str]:
"""
try:
ensure_roundtable_functional_agents()
thread_id = await _create_thread_direct(request, metadata=_ROUNDTABLE_THREAD_METADATA)
thread_id = await _create_thread_direct(
request,
metadata=_step3_thread_metadata(),
)
except Exception as exc:
await record_foreground_diag(
request, stage="step3_report", level="error", event="thread_init_failed",
@@ -1028,7 +1042,10 @@ async def init_report_thread(request: Request) -> dict[str, str]:
@router.post("/summary/init")
async def init_summary_thread(request: Request) -> dict[str, str]:
async def init_summary_thread(
request: Request,
payload: dict | None = Body(default=None),
) -> dict[str, str]:
"""为 Step 3「总结报告」创建一个供方案总结报告智能体(``roundtable-summary``)
运行的线程,返回 ``{"agent_name", "thread_id"}``。
@@ -1044,7 +1061,10 @@ async def init_summary_thread(request: Request) -> dict[str, str]:
"""
try:
ensure_roundtable_functional_agents()
thread_id = await _create_thread_direct(request, metadata=_ROUNDTABLE_THREAD_METADATA)
thread_id = await _create_thread_direct(
request,
metadata=_step3_thread_metadata(),
)
except Exception as exc:
await record_foreground_diag(
request, stage="step3_summary", level="error", event="thread_init_failed",
@@ -1082,7 +1102,10 @@ async def init_position_action_plan_thread(request: Request) -> dict[str, str]:
@router.post("/dashboard/init")
async def init_dashboard_thread(request: Request) -> dict[str, str]:
async def init_dashboard_thread(
request: Request,
payload: dict | None = Body(default=None),
) -> dict[str, str]:
"""为 Step 3「大屏多页」创建一个供大屏多页数据智能体(``roundtable-dashboard``)
运行的线程,返回 ``{"agent_name", "thread_id"}``。
@@ -1100,7 +1123,10 @@ async def init_dashboard_thread(request: Request) -> dict[str, str]:
"""
try:
ensure_roundtable_functional_agents()
thread_id = await _create_thread_direct(request, metadata=_ROUNDTABLE_THREAD_METADATA)
thread_id = await _create_thread_direct(
request,
metadata=_step3_thread_metadata(),
)
except Exception as exc:
await record_foreground_diag(
request, stage="step3_dashboard", level="error", event="thread_init_failed",
@@ -1112,7 +1138,10 @@ async def init_dashboard_thread(request: Request) -> dict[str, str]:
@router.post("/structure/init")
async def init_structure_thread(request: Request) -> dict[str, str]:
async def init_structure_thread(
request: Request,
payload: dict | None = Body(default=None),
) -> dict[str, str]:
"""为 Step 3「入库」创建一个供结构化抽取智能体(``roundtable-structure``)运行的线程,
返回 ``{"agent_name", "thread_id"}``。
@@ -1130,7 +1159,10 @@ async def init_structure_thread(request: Request) -> dict[str, str]:
"""
try:
ensure_roundtable_functional_agents()
thread_id = await _create_thread_direct(request, metadata=_ROUNDTABLE_THREAD_METADATA)
thread_id = await _create_thread_direct(
request,
metadata=_step3_thread_metadata(),
)
except Exception as exc:
await record_foreground_diag(
request, stage="step3_structure", level="error", event="thread_init_failed",
@@ -1181,7 +1213,6 @@ async def _stream_upstream(
本机往返通常 <10ms),怀疑 HTTP_PROXY 拦了回环——检查 client 是否走 _make_loopback_client
(trust_env=False)。
"""
url = f"{base}/api/threads/{thread_id}/runs/stream"
captured: list[str] = []
agent_for_log = (body.get("context") or {}).get("agent_name") or thread_id
@@ -1189,8 +1220,8 @@ async def _stream_upstream(
# 头阶段非 200 的瞬时重试。
# 背景:网关是**单 worker** + checkpointer 是**单连接 aiosqlite**(全进程一把
# asyncio.Lock 串行 checkpoint 读写)。圆桌第三步进入时,前端把「方案总结/结果绘制/
# 大屏」三个重 run **同一刻并行**打出来,叠加第二步 on_disconnect=continue 仍在后台
# 跑的席位 run,瞬时把单锁/单 worker 打满 —— 此时回环子请求 /runs/stream 的 start_run
# 大屏」三个重 run **同一刻并行**打出来,若再叠加尚未完成取消收尾的第二步席位 run,
# 瞬时把单锁/单 worker 打满 —— 此时回环子请求 /runs/stream 的 start_run
# 里那串 DB / checkpoint 写有概率抛错(或同 thread 抢占竞态),返回非 200,被本函数原样
# 包成 502 漏给前端(用户侧表现为"进第三步概率性 502")。
# 关键安全前提:这个非 200 发生在**响应头阶段**,本函数**尚未 yield 任何 SSE 行**给下游,
@@ -1553,6 +1584,7 @@ async def _leader_run(
model_name: str,
seat_meta: dict[str, dict[str, Any]] | None = None,
synthesis_mode: bool = False,
finalize_only: bool = False,
suppress_pre_broadcast: bool = True,
files: list[dict[str, Any]] | None = None,
) -> AsyncIterator[str | dict[str, Any]]:
@@ -1584,6 +1616,9 @@ async def _leader_run(
# new_message),绝不阻断 leader run。这些只进总控**本轮 run 输入**,不广播给席位。
seat_status_block = ""
full_deliverables_block = ""
# Older clients may omit names/descriptions; the thread mapping still
# supplies the authoritative IDs, including on a checkpoint-free first run.
seat_meta = seat_meta or {sid: {} for sid in agent_threads if sid != agent_name}
if seat_meta:
seat_ids = [name for name in agent_threads if name != agent_name]
if seat_ids:
@@ -1600,10 +1635,9 @@ async def _leader_run(
undelivered = [sid for sid in seat_ids if not delivery.get(sid, (False, ""))[0]]
seats_meta_list = [(sid, seat_meta.get(sid, {})) for sid in seat_ids]
seat_status_block = _build_seat_status_block(seats_meta_list, delivery, undelivered_count=len(undelivered))
# 第 2 阶段(仅「综合轮 且 所有席位都已交付」):再**全文**取各已交付席位的交付,拼综合块。
# 还有 ⬜ 未交付席位时不注入 —— 否则"综合输出最终方案"的指令会怂恿弱模型在席位没集齐时
# 就提前收口(实测:3/5 交付就臆称"三方已发言"草草收口)。全文读只在这一轮才付出。
if synthesis_mode and not undelivered:
# 普通综合轮只有全部席位交付后才注入全文,避免弱模型过早收口;显式强制收口轮
# 即使仍有失败席位,也要注入其余有效交付并在最终结论中如实标明缺口。
if finalize_only or (synthesis_mode and not undelivered):
try:
full_delivery = await _collect_delivery_status(
client, base, headers,
@@ -1621,6 +1655,14 @@ async def _leader_run(
len(undelivered),
bool(full_deliverables_block),
)
if finalize_only:
new_message = (
f"{new_message}\n\n"
"【最终收口·纯正文】本轮所有工具均已关闭。请直接根据上方席位交付输出一份合并后的最终结论正文;"
"不要按席位逐份复述原文,也不要反复改写同一份总结。"
"不要调用或尝试 write_file、present_files、agent_orchestration、skill_view 或任何其它工具,"
"不要再派活,也不要只描述你准备做什么。"
)
leader_input = "\n\n".join(
part for part in (seat_status_block, full_deliverables_block, new_message) if part
)
@@ -1637,12 +1679,14 @@ async def _leader_run(
# 带了上传文件时,给总控解锁「读」类工具(leader policy 默认禁 read_file/ls/view_image),
# 否则文件清单注入了却无工具读取。写/派活类仍按 policy 封锁,不放开。
leader_excluded = policy.excluded_tools
if files:
leader_allowed = [] if finalize_only else list(getattr(policy, "allowed_tools", None) or [])
if files and not finalize_only:
_read_tools = {"read_file", "ls", "grep", "view_image"}
leader_excluded = [t for t in leader_excluded if t not in _read_tools]
leader_allowed.extend(sorted(_read_tools))
# 模型自动容错:按 config.yaml models[] 顺序的候选链。当前模型出问题(兜底错误文案 /
# 既无正文又无派活)时切到下一个模型重跑(见循环内检测)。``_mk_body`` 用候选模型重建 body。
# 模型自动容错:按 config.yaml models[] 顺序的候选链。当前模型返回明确的错误标记/
# 兜底错误文案时才切到下一个模型重跑;没有正文或派活不触发换模。
model_chain = fallback_model_chain(model_name) or [model_name]
model_idx = 0
current_model = model_chain[0]
@@ -1655,13 +1699,17 @@ async def _leader_run(
# 注入了席位清单的输入(leader_input);seat_roster 为空时等同 new_message。
new_message=leader_input,
excluded_tools=leader_excluded,
allowed_tools=list(dict.fromkeys(leader_allowed)),
skill_stop_names=effective_stop_names,
recursion_limit=int(getattr(policy, "recursion_limit", 1000)),
thinking_enabled=policy.thinking_enabled,
reasoning_effort=policy.reasoning_effort,
# 人工干预/恢复会重发 leader run 到同一总控 thread;抢占掉没结束的旧 run,避免 409。
multitask_strategy="rollback",
cancel_on_disconnect=True,
# 用户随干预上传的参考文件 → 总控可用 read_file 等工具读取后再派活。
files=files,
roundtable_file_stream=True,
)
body = _mk_body(current_model)
@@ -1729,11 +1777,11 @@ async def _leader_run(
leader_text = _flatten_content(dispatch_msg.get("content", ""))
tool_calls = dispatch_msg.get("tool_calls") or []
# 模型自动容错:本轮总控产出是「中间件兜底错误文案」或「既无正文又无派活」(模型没干活)
# → 若还有备选模型,切到下一个重跑(不消耗派活纠正预算)。先发 model_switch 帧让前端把
# 这条失败气泡重置,再用新模型重新流式。最后一个模型仍失败则照常往下走(错误文案当共识展示)。
# 模型自动容错通常只识别真正的模型错误。总控暂时没有正文/派活不代表模型故障,不能据此
# 擅自换掉用户选择的模型(部分模型会长时间 reasoning 或连续调用工具)。唯一例外是:
# 无工具的最终收口请求已经完整结束却仍无正文,此时该次结果客观不可交付,才换下一个模型。
fail_reason = is_llm_error_message(dispatch_msg)
if fail_reason is None and not (leader_text or "").strip() and not tool_calls:
if fail_reason is None and finalize_only and not (leader_text or "").strip():
fail_reason = "empty"
if fail_reason and model_idx + 1 < len(model_chain):
model_idx += 1
@@ -1750,6 +1798,16 @@ async def _leader_run(
current_model = next_model
body = _mk_body(current_model)
continue
if fail_reason:
yield {
"status": "error",
"role": "leader",
"agent_name": agent_name,
"model_used": current_model,
"reason": reason_text(fail_reason),
"content": leader_text,
}
return
# 澄清分支:与 intent.py 相同字段;status 为字符串 "clarification"(非数组),
# 前端据此暂停 runOrchestration 循环,展示 Step 2 澄清条。
@@ -1903,11 +1961,9 @@ async def _special_run(
directive = build_summary_directive(business_code)
if directive:
seat_message = f"{new_message}\n\n---\n\n{directive}"
# 模型自动容错:按 config.yaml models[] 顺序的候选链。某模型出问题(兜底错误文案;
# 真正的研讨席位还含「交付为空」)→ 发 model_switch 帧让前端重置该席位气泡,再用下一个
# 模型重跑。Step3 单例(report/summary/dashboard/ingest)的产物是落盘文件,正文可能很短,
# 故**不**把「正文为空」当失败(与后台 run_report/summary 的 validate=None 一致),只在真正
# 的模型错误时换模型。
# 模型自动容错:只在真正的模型错误(中间件错误标记/兜底错误文案)时换下一个模型。
# 「仅有思考 / 暂无可见正文 / 交付为空」不是模型故障,不能触发换模;这些状态仍在本轮
# 结束后按 invalid_delivery 交给既有交付校验处理。
model_chain = fallback_model_chain(model_name) or [model_name]
empty_is_failure = role in ("seat", "seat_parallel")
@@ -1918,7 +1974,9 @@ async def _special_run(
thread_id=thread_id,
new_message=seat_message,
excluded_tools=seat_excluded,
allowed_tools=getattr(policy, "allowed_tools", None),
skill_stop_names=merge_skill_stop_names(policy.skill_stop_names, skill_stop_names),
recursion_limit=int(getattr(policy, "recursion_limit", 1000)),
# 席位执行模式覆盖 policy 的 thinking/reasoning(None → 回退 policy 默认);
# subagent 仅 ultra 档置真。工具裁剪(excluded_tools)仍完全由 policy 决定。
thinking_enabled=(policy.thinking_enabled if thinking_enabled is None else thinking_enabled),
@@ -1928,12 +1986,14 @@ async def _special_run(
stream_custom=subagent_enabled,
# 干预/重新派活会把同一席位 thread 再跑一次;抢占掉没结束的旧 run,避免 409。
multitask_strategy="rollback",
cancel_on_disconnect=role in ("seat", "seat_parallel"),
# 前序席位完整交付 → run context,供 read_peer_delivery 工具按需取全文(§12.9)。
extra_context=(
{"roundtable_peer_deliveries": peer_deliveries} if peer_deliveries else None
),
# 用户随干预上传的参考文件 → 席位可用 read_file 等工具读取。
files=files,
roundtable_file_stream=True,
)
t_payload_built = time.perf_counter()
@@ -2004,10 +2064,8 @@ async def _special_run(
visible = _visible_delivery_from_messages(messages)
delivery_kind = classify_seat_delivery(content) if content.strip() else ("valid" if visible else "empty")
# 模型失败检测:兜底错误文案;研讨席位再加「无可见正文」(思考不算)。还有备选模型则换。
# 模型失败检测:只认中间件标记或兜底错误文案。思考较长/暂无正文绝不触发换模。
fail_reason = is_llm_error_message(last_msg)
if fail_reason is None and empty_is_failure and delivery_kind != "valid":
fail_reason = "thinking_only" if delivery_kind == "thinking_only" else "empty"
if fail_reason and idx + 1 < len(model_chain):
next_model = model_chain[idx + 1]
logger.warning("[special-fallback] %s 模型 %s 失败(%s),切换到 %s", agent_name, candidate, fail_reason, next_model)
@@ -2021,8 +2079,19 @@ async def _special_run(
}
continue
if fail_reason:
yield {
"status": "error",
"role": role,
"agent_name": agent_name,
"model_used": candidate,
"reason": reason_text(fail_reason),
"content": visible or content,
}
return
if empty_is_failure and delivery_kind != "valid":
logger.warning("[special-fallback] %s 所有模型均未产出可见正文(%s)", agent_name, delivery_kind)
logger.warning("[special-delivery] %s 本轮未产出可见正文(%s),不触发换模", agent_name, delivery_kind)
yield {
"status": "invalid_delivery",
"reason": delivery_kind,
@@ -2087,6 +2156,7 @@ async def run_multi_agent_stream(request: Request, payload: dict = Body(...)) ->
# 额外把各已交付席位的完整交付正文注入总控输入,供其写最终方案。缺省 False(派活决策轮
# 只给摘要,保持上下文精简)。旧前端不传 → False,行为安全退化。
synthesis_mode: bool = bool(payload.get("synthesis_mode") or False)
finalize_only: bool = bool(payload.get("finalize_only") or False)
# 并行禁写文件标记(仅 special 用):前端 runDagOrchestration 在真并行 stage(≥2 席位)
# 置真,网关据此用 seat_parallel policy 额外禁 write_file/bash/str_replace(§4)。
# 兼容 no_file 别名;缺省 False → 普通 seat,旧前端不传也不破坏。
@@ -2133,6 +2203,27 @@ async def run_multi_agent_stream(request: Request, payload: dict = Body(...)) ->
for key in agent_threads:
_validate_agent_id(key, "d_agent_thread_id key")
# Browser-side acquire admits the room exactly once when Step 2 begins.
# Every model-producing Step-2 entry only renews that existing room, so a
# pause, clarification answer or user intervention never competes for a
# second slot. Step 3 is excluded because consensus releases the room.
step2_room_limited = _is_step2_room_limited(
agent_type,
agent_name,
# 只有显式最终收口轮绕过房间时限;普通综合轮仍可继续派活。
finalize_only=finalize_only,
)
if step2_room_limited:
from app.gateway.roundtable_room_gate import enforce_roundtable_run
await enforce_roundtable_run(
request,
draft_id=str(payload.get("draft_id") or "").strip() or None,
task_id=str(payload.get("task_id") or "").strip() or None,
room_session_id=str(payload.get("room_session_id") or "").strip() or None,
thread_ids=list(agent_threads.values()),
)
base = _loopback_base(request)
headers = _auth_headers(request)
@@ -2160,6 +2251,7 @@ async def run_multi_agent_stream(request: Request, payload: dict = Body(...)) ->
client, base, headers, agent_threads, agent_name, new_message, skill_stop_names, model_name,
seat_meta=seat_meta,
synthesis_mode=synthesis_mode,
finalize_only=finalize_only,
suppress_pre_broadcast=suppress_pre_broadcast,
files=files,
)
@@ -2211,6 +2303,13 @@ async def run_multi_agent_stream(request: Request, payload: dict = Body(...)) ->
"agent_name": agent_name,
}
yield f"data: {json.dumps(err_frame, ensure_ascii=False)}\n\n"
except asyncio.CancelledError:
# Intentional pause and a real network disconnect are both an
# SSE cancellation at this layer. Releasing here made a normal
# pause lose its admitted room. Browser heartbeats now own
# liveness: pauses keep heartbeating; a disconnected browser
# stops heartbeating and the 60-second lease expires.
raise
return StreamingResponse(
stream_generator(),