sync: 鍚屾灞€鍩熺綉鏈€鏂颁唬鐮?(source master @ b69568e4)

This commit is contained in:
gozeros committed 2026-10-21 14:51:31 +08:00
1 parent 4f7f07504a
commit 8bc8b0e082
1651 files changed
+278599 -11416

No files matched your search

@@ -0,0 +1,19 @@
"""Task-analysis runtime package (external-task scheduled chain analysis)."""
from deerflow.runtime.task_analysis.data_delivery import (
block_defs_payload,
period_detail_payload,
periods_payload,
submit_block_data,
)
from deerflow.runtime.task_analysis.executor import ChainBindingExecutor
from deerflow.runtime.task_analysis.service import TaskAnalysisService
__all__ = [
"ChainBindingExecutor",
"TaskAnalysisService",
"block_defs_payload",
"period_detail_payload",
"periods_payload",
"submit_block_data",
]
@@ -0,0 +1,176 @@
"""Scheduled data-delivery domain logic(定时数据交付:任务 6 块 JSON).
职责边界(已定稿口径):
- 只做数据管道 + 调度;智能体干完活调技能入库,归配智能体/业务链的人管。
- 不做席位↔块映射(多个智能体可各写一个块的不同部分,一个智能体也可写多个块),
不做收口兜底。
- 块 key 定死 ``block_1``~``block_6``;payload 不做 schema 校验,只要求是 JSON 对象。
- 合并规则:顶层 key 合并、同 key 后写覆盖;块 payload 由提交流水按序合并得出
(每次提交后从流水重算落库,天然规避并发读改写丢片段)。
- 不挂时间轴:任务的数据只有一份「当前状态」,所有提交都合并进它;首次提交时
若还没有承载记录,自动开一条(trigger=external),保证写入永远成功。
"""
from __future__ import annotations
import logging
from typing import Any
from deerflow.persistence.task_analysis.base import TaskAnalysisStore
from deerflow.persistence.task_analysis.data_blocks import (
PERIOD_TRIGGER_EXTERNAL,
merge_payload,
)
logger = logging.getLogger(__name__)
async def submit_block_data(
store: TaskAnalysisStore,
*,
task_id: str,
block_key: str,
data: Any,
submitted_by: str | None = None,
) -> dict[str, Any]:
"""提交一块数据片段 → 落当前期 + 按流水重算合并结果。
Raises:
ValueError: payload 不是 JSON 对象,或 block_key 不在块定义内。
"""
if not isinstance(data, dict):
raise ValueError("payload 必须是 JSON 对象(不接受数组/字符串/数字)")
block_key = str(block_key or "").strip()
defs = await store.ensure_block_defs(task_id)
known = {definition["block_key"] for definition in defs}
if block_key not in known:
raise ValueError(f"未知数据块 {block_key or '(空)'};可用块:{', '.join(sorted(known))}")
period = await store.current_period(task_id)
if period is None:
period = await store.open_period(task_id, source=PERIOD_TRIGGER_EXTERNAL)
period_index = int(period["period_index"])
await store.append_submission(
task_id, period_index, block_key, data, submitted_by=submitted_by
)
# 从流水按序重算(而非增量合并现值):并发提交下最终状态仍收敛到流水顺序。
merged: dict[str, Any] = {}
for submission in await store.list_submissions(task_id, period_index, block_key):
fragment = submission.get("payload")
if isinstance(fragment, dict):
merged = merge_payload(merged, fragment)
block = await store.upsert_period_block(task_id, period_index, block_key, merged)
return {
"block_key": block_key,
"period_index": period_index,
"updated_at": block["updated_at"],
"keys": sorted(merged.keys()),
}
def _task_brief(task: dict[str, Any]) -> dict[str, Any]:
return {
"task_id": task["id"],
"source_task_id": task["source_task_id"],
"name": task["name"],
}
async def block_defs_payload(store: TaskAnalysisStore, task: dict[str, Any]) -> dict[str, Any]:
"""GET …/blocks 响应:块定义列表(建任务自动种 6 条,旧任务懒补缺失 key)。"""
defs = await store.ensure_block_defs(task["id"])
return {
"task": _task_brief(task),
"blocks": [
{
"block_key": definition["block_key"],
"title": definition["title"],
"sort_order": definition["sort_order"],
}
for definition in defs
],
}
async def periods_payload(store: TaskAnalysisStore, task: dict[str, Any], *, limit: int = 500) -> dict[str, Any]:
"""GET …/periods 响应(兼容旧路径):只返回任务当前的数据承载记录(最新一期)。
时间轴维度已去掉:任务的数据只有一份当前状态;存量任务的历史期保留在表里
但不再对外暴露,读历史接口(periods/{n})仍可按期号访问。
"""
defs = await store.ensure_block_defs(task["id"])
def_keys = [definition["block_key"] for definition in defs]
current = await store.current_period(task["id"])
blocks = await store.list_task_blocks(task["id"])
updated_by_period: dict[int, dict[str, str | None]] = {}
for block in blocks:
updated_by_period.setdefault(int(block["period_index"]), {})[block["block_key"]] = block.get("updated_at")
if current is None:
return {"task": _task_brief(task), "current_period_index": None, "total": 0, "periods": []}
current_index = int(current["period_index"])
return {
"task": _task_brief(task),
"current_period_index": current_index,
"total": 1,
"periods": [
{
"period_index": current["period_index"],
"opened_at": current["opened_at"],
"trigger": current["trigger"],
"run_id": current.get("run_id"),
"is_current": True,
"blocks": {
key: {"updated_at": updated_by_period.get(current_index, {}).get(key)}
for key in def_keys
},
}
],
}
async def period_detail_payload(store: TaskAnalysisStore, task: dict[str, Any], period_ref: str) -> dict[str, Any] | None:
"""GET …/periods/{n|latest} 响应:该期 6 块完整 JSON + 各块 updated_at。
期不存在返回 None(路由转 404)。
"""
defs = await store.ensure_block_defs(task["id"])
if period_ref == "latest":
period = await store.current_period(task["id"])
else:
period = await store.get_period(task["id"], int(period_ref))
if period is None:
return None
period_index = int(period["period_index"])
blocks = {
block["block_key"]: block
for block in await store.list_task_blocks(task["id"])
if int(block["period_index"]) == period_index
}
current = await store.current_period(task["id"])
current_index = int(current["period_index"]) if current else None
return {
"task": _task_brief(task),
"period": {
"period_index": period_index,
"opened_at": period["opened_at"],
"trigger": period["trigger"],
"run_id": period.get("run_id"),
"is_current": current_index is not None and period_index == current_index,
},
"blocks": [
{
"block_key": definition["block_key"],
"title": definition["title"],
"sort_order": definition["sort_order"],
"payload": (blocks.get(definition["block_key"]) or {}).get("payload"),
"updated_at": (blocks.get(definition["block_key"]) or {}).get("updated_at"),
}
for definition in defs
],
}
@@ -0,0 +1,959 @@
"""Chain-binding orchestration executor for the task-analysis feature.
Runs one binding run stage-by-stage following the business-chain snapshot:
seats within a stage run in parallel (each on its own lead-agent thread with
``execution_context.agent_name`` pointing at the seat agent — the same in-process
``start_run`` trick ``ScheduledTaskService._start_agent_run`` uses), stages run
serially. Later seats receive prior seats' **full** deliveries (same contract as
roundtable Step 2 ``_seat_message``), plus the chain's per-stage receive/produce
goals, a structured delivery template, skill-directive assist, and a role boundary.
Process messages (input / llm.ai / artifact per seat) are flushed as they
happen. While a seat is still running, ``GET /runs/{id}/messages`` also reads
the live RunEventStore / checkpoint (same source as scheduled-task run detail)
so the frontend's 5-second poll shows in-flight LLM turns and tool calls.
Before the stages run, a dedicated
built-in coordinator (``task-analysis-coordinator``, unattended: no clarification)
reads the task context and emits a concrete per-seat task via the
``agent_orchestration`` tool; each seat then executes that assigned task.
Coordinator failure degrades to the same duty / stage / delivery contract
without a coordinator-assigned task.
One content "timeline" version row is written per successful run.
A seat may fail without sinking the whole run: failed seats are marked, later
stages are skipped, and the already-completed stages still land a (partial)
content version — the partial-success semantics agreed for this feature.
"""
from __future__ import annotations
import asyncio
import logging
import time
from datetime import UTC, datetime
from typing import Any
from deerflow.persistence.task_analysis.base import TaskAnalysisStore
logger = logging.getLogger(__name__)
COORDINATOR_SEAT = "__coordinator__"
MARKDOWN_BLOCK_MAX_CHARS = 24000
PRIOR_DELIVERY_MAX_CHARS = 8000
# 对齐会商第二步 ``useStep2Orchestration`` 的 ``SEAT_DELIVERY_GUIDE``:席位必须知道
# 「产出长什么样」,否则只会复述任务简介。
SEAT_DELIVERY_GUIDE = (
"【交付要求】请直接基于已知信息做出合理假设、给出完整交付,**不要向用户提问或等待确认**;"
"承接此前各席位的交付,不要重复或推翻已有结论。"
"交付请按以下结构组织:① 一句话核心结论(先行);② 关键要点(分条列出);"
"③ 风险 / 注意事项(如有);④ 与其它席位的依赖 / 承接关系(如有)。"
)
# 对齐会商第二步 ``engine._SEAT_NO_ASK``:无人值守、正文必须带交付、同时落盘 outputs。
SEAT_NO_ASK = (
"注意:这是自动化任务分析,没有用户在线应答——**请勿向用户提问或等待确认**,"
"直接基于已知信息做出合理假设,产出完整的结构化交付。\n"
"**交付方式**:请用 write_file 工具把你这一轮的完整交付整理成一份 "
"Markdown 文件,写入 /mnt/user-data/outputs/ 下(文件名体现你的职责,如 `情报收集.md`、"
"`风险评估.md`);同时在回复正文里给出这份交付的内容本身(供后续席位承接),"
"不要只回复『已写入文件』这类空话。"
)
class _RuntimeUser:
"""Minimal user stand-in for in-process runs (mirrors scheduler service)."""
def __init__(self, id: str) -> None: # noqa: A002 - mirror scheduler API
self.id = id
def _goal_text(value: Any) -> str:
text = str(value or "").strip()
return text
def _prior_deliveries_block(
stage_outputs: list[tuple[str, str]], max_chars: int = PRIOR_DELIVERY_MAX_CHARS
) -> str:
"""Full prior-seat deliveries, matching roundtable ``_deliveries_block`` (not a 600-char digest)."""
parts: list[str] = []
for seat_name, text in stage_outputs:
clipped = (text or "").strip()
if not clipped:
continue
if len(clipped) > max_chars:
clipped = clipped[:max_chars] + "…"
parts.append(f"### {seat_name}\n{clipped}")
return "\n\n".join(parts)
def _stage_receive_text(
*,
stage_index: int,
prev_goal: str | None,
task: dict[str, Any],
) -> str:
if stage_index <= 0:
name = str(task.get("name") or "").strip() or "(未命名)"
desc = str(task.get("description") or "").strip() or "(无简介)"
return f"外部任务「{name}」及其简介:{desc}"
if prev_goal:
return f"上一阶段目标「{prev_goal}」,以及下方「前序各席位已完成的交付」全文"
return "上一阶段各席位的交付全文(见下方)"
def _stage_produce_text(*, stage_goal: str | None, seat_description: str, seat_name: str) -> str:
if stage_goal:
return stage_goal
if seat_description:
return f"按席位职责完成交付:{seat_description}"
return f"按「{seat_name}」的专业职责给出完整结构化交付"
def _fallback_seat_task(
*,
seat_name: str,
seat_description: str,
stage_receive: str,
stage_produce: str,
) -> str:
duty = seat_description or f"作为「{seat_name}」完成你的专业职责"
return (
"总控本轮未能完成派活。请按席位职责与阶段契约自行完成交付,不要空等总控。\n"
f"职责:{duty}\n"
f"本阶段接收:{stage_receive}\n"
f"本阶段产出:{stage_produce}\n"
"请给出完整结构化交付,不要向用户提问。"
)
def _strip_display_suffix(agent_name: str) -> str:
# 兼容「<id>(中文名)」形式的派活(与会商 _strip_display_suffix 同语义)。
return agent_name.split("<")[0].split("(")[0].strip()
def _coerce_tool_call(call: Any) -> dict[str, Any] | None:
"""LangChain ToolCall 可能是 dict,也可能是带 name/args 属性的对象。"""
if isinstance(call, dict):
name = call.get("name") or (call.get("function") or {}).get("name")
args = call.get("args")
if args is None:
args = (call.get("function") or {}).get("arguments")
if not name:
return None
return {"name": name, "args": args or {}}
name = getattr(call, "name", None)
args = getattr(call, "args", None)
if not name:
return None
return {"name": name, "args": args or {}}
def _parse_orchestration_calls(
tool_calls: list[Any], valid_ids: set[str]
) -> dict[str, str]:
"""从总控 tool_calls 解析 agent_orchestration 派活 → {seat_id: task}。"""
dispatch: dict[str, str] = {}
for raw in tool_calls:
call = _coerce_tool_call(raw)
if call is None:
continue
name = str(call.get("name") or "").lower()
if name != "agent_orchestration":
continue
args = call.get("args")
if isinstance(args, str):
import json as _json
try:
args = _json.loads(args)
except (TypeError, ValueError):
continue
if not isinstance(args, dict):
continue
agent = _strip_display_suffix(str(args.get("agent_name") or "").strip())
task_text = str(args.get("task") or "").strip()
if agent in valid_ids and task_text and agent not in dispatch:
dispatch[agent] = task_text
return dispatch
class ChainBindingExecutor:
"""Executes one ``task_analysis_runs`` row against a binding snapshot."""
def __init__(self, app: Any, store: TaskAnalysisStore) -> None:
self.app = app
self.store = store
# ── public entry ────────────────────────────────────────────────────────
async def execute(self, run: dict[str, Any], binding: dict[str, Any], task: dict[str, Any]) -> None:
run_id = run["id"]
await self.store.update_run(run_id, {"status": "running"})
stages: list[list[str]] = run.get("stages_snapshot") or []
seat_names: dict[str, str] = run.get("seat_names_snapshot") or {}
stage_goals: list[Any] = run.get("stage_goals_snapshot") or []
user_id = binding.get("created_by") or ""
started = time.monotonic()
seat_results: dict[str, dict[str, Any]] = {}
snapshot_seats = (binding.get("chain_snapshot") or {}).get("seats") or []
descriptions = {
seat.get("agent_id"): str(seat.get("description") or "") for seat in snapshot_seats
}
# 过程消息即时落库(逐段 flush),前端 5 秒轮询就能看到执行中的动态;
# 落库失败仅记日志,绝不影响执行。
async def emit(messages: list[dict[str, Any]]) -> None:
if not messages:
return
try:
await self.store.append_run_messages(run_id, messages)
except Exception:
logger.exception("task-analysis run %s message persist failed", run_id)
stage_outputs: list[tuple[str, str]] = []
failed = False
run_error: str | None = None
try:
# 流程图先把总控标成执行中、席位等待,避免前端把「run=running 且席位未开始」
# 误显示成总控还在跑、同时席位已经开了。
seat_results[COORDINATOR_SEAT] = {
"state": "running",
"duration_ms": 0,
"thread_id": None,
"seat_name": "总控",
"error": None,
}
for stage in stages:
for seat_id in stage:
seat_results[seat_id] = {
"state": "waiting",
"duration_ms": 0,
"thread_id": None,
"error": None,
}
await self._flush_seat_results(run_id, seat_results)
async def note_live(
seat_id: str,
*,
thread_id: str | None = None,
agent_run_id: str | None = None,
) -> None:
await self._note_live(
run_id,
seat_results,
seat_id,
thread_id=thread_id,
agent_run_id=agent_run_id,
)
coord_started = time.monotonic()
seat_tasks = await self._coordinator_dispatch(
run=run,
binding=binding,
task=task,
user_id=user_id,
emit=emit,
note_live=note_live,
)
coord_ms = int((time.monotonic() - coord_started) * 1000)
prev_coord = dict(seat_results.get(COORDINATOR_SEAT) or {})
if seat_tasks:
seat_results[COORDINATOR_SEAT] = {
**prev_coord,
"state": "done",
"duration_ms": coord_ms,
"seat_name": "总控",
"error": None,
}
await emit([{
"seat_agent_id": COORDINATOR_SEAT,
"stage_index": 0,
"event_type": "artifact",
"content": "【总控派活】\n"
+ "\n".join(
f"→ {seat_names.get(sid, sid)}:{text[:400]}"
for sid, text in seat_tasks.items()
),
}])
else:
prev_coord = dict(seat_results.get(COORDINATOR_SEAT) or {})
seat_results[COORDINATOR_SEAT] = {
**prev_coord,
"state": "failed",
"duration_ms": coord_ms,
"seat_name": "总控",
"error": "总控未产生有效派活,席位将按阶段契约自行执行",
}
await emit([{
"seat_agent_id": COORDINATOR_SEAT,
"stage_index": 0,
"event_type": "artifact",
"content": "【总控未派活】未拿到各席位具体任务。后续席位按业务链职责与阶段契约自行交付,不会在总控还在跑时抢跑。",
}])
await self._flush_seat_results(run_id, seat_results)
for stage_index, stage in enumerate(stages):
if failed:
for seat_id in stage:
seat_results[seat_id] = {
"state": "waiting",
"duration_ms": 0,
"thread_id": None,
"error": None,
}
continue
goal = _goal_text(stage_goals[stage_index]) if stage_index < len(stage_goals) else ""
prev_goal = (
_goal_text(stage_goals[stage_index - 1])
if stage_index > 0 and stage_index - 1 < len(stage_goals)
else ""
)
for seat_id in stage:
current = dict(seat_results.get(seat_id) or {})
current["state"] = "running"
seat_results[seat_id] = current
await self._flush_seat_results(run_id, seat_results)
results = await asyncio.gather(
*[
self._run_seat(
seat_id=seat_id,
seat_name=seat_names.get(seat_id, seat_id),
seat_description=descriptions.get(seat_id) or "",
stage_index=stage_index,
stage_goal=goal or None,
prev_goal=prev_goal or None,
task=task,
binding=binding,
prior_deliveries=list(stage_outputs),
seat_task=seat_tasks.get(seat_id),
user_id=user_id,
emit=emit,
note_live=note_live,
)
for seat_id in stage
],
return_exceptions=True,
)
stage_failed = False
for seat_id, result in zip(stage, results, strict=False):
if isinstance(result, BaseException):
logger.exception(
"task-analysis seat %s crashed (run %s)", seat_id, run_id
)
seat_results[seat_id] = {
"state": "failed",
"duration_ms": 0,
"thread_id": None,
"error": str(result),
}
stage_failed = True
else:
seat_results[seat_id] = result
if result["state"] == "failed":
stage_failed = True
elif result.get("delivery"):
stage_outputs.append(
(result.get("seat_name") or seat_id, result["delivery"])
)
if stage_failed:
failed = True
run_error = f"阶段 {stage_index + 1} 存在失败席位,后续阶段已跳过"
status = "failed" if failed and not stage_outputs else ("partial" if failed else "succeeded")
await self._finish_run(
run=run,
task=task,
status=status,
seat_results=seat_results,
seat_names=seat_names,
error=run_error,
duration_ms=int((time.monotonic() - started) * 1000),
)
except Exception as exc: # noqa: BLE001 - top-level guard for the run
logger.exception("task-analysis run %s crashed", run_id)
await self._finish_run(
run=run,
task=task,
status="failed",
seat_results=seat_results,
seat_names=seat_names,
error=str(exc),
duration_ms=int((time.monotonic() - started) * 1000),
)
# ── single seat ─────────────────────────────────────────────────────────
async def _run_seat(
self,
*,
seat_id: str,
seat_name: str,
seat_description: str,
stage_index: int,
stage_goal: str | None,
prev_goal: str | None,
task: dict[str, Any],
binding: dict[str, Any],
prior_deliveries: list[tuple[str, str]],
seat_task: str | None,
user_id: str,
emit,
note_live=None,
) -> dict[str, Any]:
thread_id = f"ta-seat-{seat_id}-{int(time.time() * 1000)}"
prompt = self._build_seat_prompt(
task=task,
binding=binding,
seat_name=seat_name,
seat_description=seat_description,
stage_index=stage_index,
stage_goal=stage_goal,
prev_goal=prev_goal,
prior_deliveries=prior_deliveries,
seat_task=seat_task,
)
prompt = self._apply_seat_assist(seat_id, prompt)
if note_live is not None:
await note_live(seat_id, thread_id=thread_id)
await emit(
[{"seat_agent_id": seat_id, "stage_index": stage_index, "event_type": "input", "content": prompt}]
)
started = time.monotonic()
try:
record = await self._start_seat_run(
seat_id=seat_id, prompt=prompt, thread_id=thread_id, user_id=user_id,
run_meta={"binding_id": binding["id"]},
model_name=binding.get("model") or "",
)
if note_live is not None and record is not None:
await note_live(
seat_id,
thread_id=thread_id,
agent_run_id=getattr(record, "run_id", None),
)
# start_run 异步启动即返回;record.task 是执行任务,等它完成再取交付。
if record is not None and getattr(record, "task", None) is not None:
await record.task
run_error = getattr(record, "error", None) if record is not None else None
run_status = str(getattr(record, "status", "") or "") if record is not None else ""
agent_run_id = getattr(record, "run_id", None) if record is not None else None
delivery = await self._await_delivery(thread_id)
duration_ms = int((time.monotonic() - started) * 1000)
if delivery is None:
detail = run_error or f"席位执行结束但未产生文字交付(run 状态 {run_status or 'unknown'})"
return {
"state": "failed",
"duration_ms": duration_ms,
"thread_id": thread_id,
"agent_run_id": agent_run_id,
"seat_name": seat_name,
"delivery": None,
"error": detail,
}
await emit(
[{"seat_agent_id": seat_id, "stage_index": stage_index, "event_type": "llm.ai", "content": delivery}]
)
return {
"state": "done",
"duration_ms": duration_ms,
"thread_id": thread_id,
"agent_run_id": agent_run_id,
"seat_name": seat_name,
"delivery": delivery,
"error": None,
}
except Exception as exc: # noqa: BLE001 - per-seat isolation
logger.exception("task-analysis seat %s failed", seat_id)
return {
"state": "failed",
"duration_ms": int((time.monotonic() - started) * 1000),
"thread_id": thread_id,
"seat_name": seat_name,
"delivery": None,
"error": str(exc),
}
@staticmethod
def _apply_seat_assist(seat_id: str, prompt: str) -> str:
"""对齐会商第二步:技能识别强化(无人值守必须用技能)+ 席位角色边界。"""
try:
from app.gateway.roundtable_seat_skills import (
append_seat_role_boundary,
append_seat_skill_directive,
)
prompt = append_seat_skill_directive(seat_id, prompt, chain_enabled=True)
return append_seat_role_boundary(prompt)
except Exception:
logger.debug("task-analysis seat assist inject failed for %s", seat_id, exc_info=True)
return prompt
def _build_seat_prompt(
self,
*,
task: dict[str, Any],
binding: dict[str, Any],
seat_name: str,
seat_description: str,
stage_index: int,
stage_goal: str | None,
prev_goal: str | None,
prior_deliveries: list[tuple[str, str]],
seat_task: str | None = None,
) -> str:
"""对齐会商第二步 ``_seat_message``:背景 + 职责 + 阶段接收/产出 + 前序全文 + 本轮任务 + 交付模板。"""
stage_receive = _stage_receive_text(stage_index=stage_index, prev_goal=prev_goal, task=task)
stage_produce = _stage_produce_text(
stage_goal=stage_goal, seat_description=seat_description, seat_name=seat_name
)
assigned = (seat_task or "").strip()
concrete_task = assigned or _fallback_seat_task(
seat_name=seat_name,
seat_description=seat_description,
stage_receive=stage_receive,
stage_produce=stage_produce,
)
background = [
f"外部任务:{task.get('name', '')}(任务号 {task.get('source_task_id', '')})",
f"任务简介:{task.get('description') or '(无)'}",
# 显式键值形式的外部任务号:task-data-submit 技能的 --task-id 参数直接取这一行。
f"数据交付 task_id(调用 task-data-submit 技能入库时的 --task-id 参数):{task.get('source_task_id', '')}",
]
notes = binding.get("direction_notes") or []
if notes:
background.append("执行方向备注(历次问答沉淀):\n" + "\n".join(f"- {n}" for n in notes))
coordinator_prompt = binding.get("coordinator_prompt")
if coordinator_prompt:
background.append(f"总控提示:{coordinator_prompt}")
duty = (seat_description or "").strip() or f"作为「{seat_name}」完成你的专业职责"
# 总控派活必须放在最前面:抽屉默认看到的第一段就是「我要干什么」,
# 避免席位把背景/职责复述一遍却找不到本轮任务。
if assigned:
lead = "【总控派给你的本轮任务 — 必须按此执行,不要自行改题】\n" + concrete_task
else:
lead = "【总控本轮未能完成派活 — 请按席位职责与阶段契约自行交付】\n" + concrete_task
parts = [
lead,
"【本次任务背景】\n" + "\n".join(background),
f"【你的席位职责】\n你是「{seat_name}」。{duty}\n请严格按该席位的专业角色完成本轮交付,不要越权做总控或其它席位的工作。",
f"【本阶段契约】\n本阶段接收:{stage_receive}\n本阶段产出:{stage_produce}",
]
prior_block = _prior_deliveries_block(prior_deliveries)
if prior_block:
parts.append(
"【前序各席位已完成的交付,请在此基础上承接、补充、不要重复或推翻】\n" + prior_block
)
parts.append(SEAT_DELIVERY_GUIDE)
parts.append(SEAT_NO_ASK)
return "\n\n---\n\n".join(parts)
async def _start_seat_run(
self, *, seat_id: str, prompt: str, thread_id: str, user_id: str, run_meta: dict[str, Any],
model_name: str = "",
):
"""Run one seat round in-process (same trick as scheduler ``_start_agent_run``)."""
from types import SimpleNamespace
from app.gateway.routers.thread_runs import RunCreateRequest
from app.gateway.services import start_run
from deerflow.runtime.user_context import reset_current_user, set_current_user
token = set_current_user(_RuntimeUser(id=user_id)) if user_id else None
try:
request = SimpleNamespace(app=self.app, state=SimpleNamespace(user=None, auth=None))
body = RunCreateRequest(
assistant_id="lead_agent",
input={"messages": [{"type": "human", "content": prompt}]},
metadata={
"thread_type": "task-analysis",
"system": True,
"task_analysis_binding_id": run_meta.get("binding_id"),
"task_analysis_seat": seat_id,
},
context={
"agent_name": seat_id,
"is_scheduled_run": True,
"thread_id": thread_id,
"thinking_enabled": True,
"is_plan_mode": False,
"excluded_tools": ["ask_clarification"],
**({"model_name": model_name} if model_name else {}),
},
multitask_strategy="reject",
)
return await start_run(body, thread_id, request) # type: ignore[arg-type]
finally:
if token is not None:
reset_current_user(token)
# ── coordinator-first round (方案 A) ─────────────────────────────────────
async def _coordinator_dispatch(
self,
*,
run: dict[str, Any],
binding: dict[str, Any],
task: dict[str, Any],
user_id: str,
emit,
note_live=None,
) -> dict[str, str]:
"""无人值守总控先行:理解任务并给每个席位产出具体 task。
对齐会商第二步后台路径:``roundtable_run_policy("leader")`` + 进程内
``_run_agent_turn``(关 thinking、禁写文件/bash、调用 ``agent_orchestration``
后停图)。不走 HTTP loopback——后台 ``asyncio.create_task`` 没有浏览器 JWT,
loopback ``/runs/stream`` 会把 thread 打成 404(总控抽屉里的 502,席位随即
用兜底任务抢跑)。本函数会一直等到总控这一轮结束才返回;任何失败返回空
dict,由调用方先把总控标成失败再启动席位。
"""
ctx_token = None
try:
from types import SimpleNamespace
from app.gateway.roundtable_inprocess_gateway import (
_latest_ai_text,
_run_agent_turn,
_tool_calls_since_last_human,
)
from app.gateway.roundtable_run_policy import roundtable_run_policy
from app.gateway.routers._task_analysis_seed import (
TASK_ANALYSIS_COORDINATOR_ID,
ensure_task_analysis_functional_agents,
)
from app.gateway.routers.multi_agent import _create_thread_direct, _extract_dispatch
from deerflow.runtime.user_context import set_current_user
ensure_task_analysis_functional_agents()
if user_id:
ctx_token = set_current_user(_RuntimeUser(id=user_id))
request = SimpleNamespace(
app=self.app,
state=SimpleNamespace(
user=_RuntimeUser(id=user_id) if user_id else None,
auth=None,
),
)
seat_names: dict[str, str] = run.get("seat_names_snapshot") or {}
stages: list[list[str]] = run.get("stages_snapshot") or []
snapshot_seats = (binding.get("chain_snapshot") or {}).get("seats") or []
descriptions = {seat.get("agent_id"): seat.get("description") or "" for seat in snapshot_seats}
thread_id = await _create_thread_direct(
request, metadata={"thread_type": "task-analysis", "system": True}
)
if note_live is not None:
await note_live(COORDINATOR_SEAT, thread_id=thread_id)
seat_lines = [
f"- agent_name: {agent_id}({seat_names.get(agent_id, agent_id)}):"
f"{descriptions.get(agent_id, "")}"
for stage in stages
for agent_id in stage
]
roster = (
"【圆桌系统初始化】本次定时执行可调度的席位清单如下,请严格按 agent_name 通过 "
"agent_orchestration 工具派活,不要派给不在清单内的 agent:" + "\n\n"
+ "\n".join(seat_lines)
)
prompt = self._build_coordinator_prompt(run=run, binding=binding, task=task)
await emit([
{"seat_agent_id": COORDINATOR_SEAT, "stage_index": 0,
"event_type": "input", "content": roster + "\n\n" + prompt},
{"seat_agent_id": COORDINATOR_SEAT, "stage_index": 0,
"event_type": "artifact",
"content": "【总控执行中】正在理解任务并给各席位派活,席位会等本轮结束再开跑。"},
])
policy = roundtable_run_policy("leader")
excluded = list(policy.excluded_tools)
if "ask_clarification" not in excluded:
excluded.append("ask_clarification")
valid_ids = set(seat_names)
full_prompt = roster + "\n\n" + prompt
for attempt in range(2):
message = full_prompt if attempt == 0 else (
full_prompt
+ "\n\n【系统重申】上一轮未产生有效派活。请立即通过 agent_orchestration 工具"
"给清单中**每个席位**派发具体任务;无人值守,禁止澄清,信息不足自行假设。"
"每个 task 必须写清:该席位接收什么、产出什么、输出结构。"
)
async def _on_coordinator_started(record) -> None:
if note_live is None:
return
await note_live(
COORDINATOR_SEAT,
thread_id=thread_id,
agent_run_id=getattr(record, "run_id", None),
)
messages = await _run_agent_turn(
self.app,
user_id=user_id or "default",
thread_id=thread_id,
agent_name=TASK_ANALYSIS_COORDINATOR_ID,
message=message,
model=binding.get("model") or None,
excluded_tools=excluded,
skill_stop_names=list(policy.skill_stop_names),
thinking_enabled=False,
reasoning_effort="low",
stage="task_analysis_leader",
on_started=_on_coordinator_started,
)
content = _latest_ai_text(messages)
if content:
await emit([
{"seat_agent_id": COORDINATOR_SEAT, "stage_index": 0,
"event_type": "llm.ai", "content": content},
])
turn_calls = _tool_calls_since_last_human(messages)
if not turn_calls:
turn_calls = await self._extract_last_tool_calls(thread_id)
dispatch = _parse_orchestration_calls(turn_calls, valid_ids)
if not dispatch:
for tc in turn_calls:
try:
agent, task_text = _extract_dispatch(tc)
except Exception:
continue
agent = _strip_display_suffix(agent)
if agent in valid_ids and task_text and agent not in dispatch:
dispatch[agent] = task_text
if dispatch:
return dispatch
logger.warning(
"task-analysis coordinator attempt %d no dispatch (run %s); calls=%s",
attempt, run.get("id"), str(turn_calls)[:300],
)
return {}
except Exception as exc:
logger.exception(
"task-analysis coordinator round crashed (run %s); falling back to static prompts",
run.get("id"),
)
try:
await emit([
{"seat_agent_id": COORDINATOR_SEAT, "stage_index": 0,
"event_type": "artifact",
"content": "【总控轮异常】" + type(exc).__name__ + ": " + str(exc)[:300]}
])
except Exception:
pass
return {}
finally:
if ctx_token is not None:
from deerflow.runtime.user_context import reset_current_user
reset_current_user(ctx_token)
def _build_coordinator_prompt(
self, *, run: dict[str, Any], binding: dict[str, Any], task: dict[str, Any]
) -> str:
stages: list[list[str]] = run.get("stages_snapshot") or []
seat_names: dict[str, str] = run.get("seat_names_snapshot") or {}
stage_goals: list[Any] = run.get("stage_goals_snapshot") or []
snapshot_seats = (binding.get("chain_snapshot") or {}).get("seats") or []
descriptions = {seat.get("agent_id"): seat.get("description") or "" for seat in snapshot_seats}
lines: list[str] = [
"【任务分析定时执行 · 总控派活】",
f"外部任务:{task.get('name', '')}(任务号 {task.get('source_task_id', '')})",
f"任务简介:{task.get('description') or '(无)'}",
]
notes = binding.get("direction_notes") or []
if notes:
lines.append("执行方向备注(历次问答沉淀,派活必须体现):\n" + "\n".join(f"- {n}" for n in notes))
coordinator_prompt = binding.get("coordinator_prompt")
if coordinator_prompt:
lines.append(f"总控提示:{coordinator_prompt}")
lines.append("本次编排席位清单(按业务链阶段结构;agent_name 必须逐字符复制):")
for stage_index, stage in enumerate(stages):
mode = f"并行 ×{len(stage)}" if len(stage) > 1 else "串行"
goal = _goal_text(stage_goals[stage_index]) if stage_index < len(stage_goals) else ""
prev_goal = (
_goal_text(stage_goals[stage_index - 1])
if stage_index > 0 and stage_index - 1 < len(stage_goals)
else ""
)
receive = _stage_receive_text(stage_index=stage_index, prev_goal=prev_goal or None, task=task)
produce = goal or "按本阶段各席位职责给出可被后续阶段承接的完整交付"
lines.append(f"- 阶段 {stage_index + 1}({mode}):")
lines.append(f" 本阶段接收:{receive}")
lines.append(f" 本阶段产出:{produce}")
for agent_id in stage:
name = seat_names.get(agent_id, agent_id)
desc = descriptions.get(agent_id)
lines.append(f" - agent_name: {agent_id}({name}){(':' + desc) if desc else ''}")
lines.append(
"\n请立刻**直接调用 agent_orchestration 工具**给上面清单中的**每个席位**派发具体任务"
"(它是已注册的工具,不是技能:不要先读技能文档、不要用 load_tool,直接在回复中发起工具调用)。"
"每个 task 必须写清:(1)该席位本轮接收什么(2)该席位本轮产出什么(3)输出结构"
"(①核心结论 ②要点 ③风险 ④承接关系),并贴合本次任务领域定制(目标 / 关注维度),"
"不要原样转发任务简介。"
"本任务无人值守:禁止调用 ask_clarification 等澄清工具,信息不足时自行做出合理假设并在任务中注明「假设:…」。"
)
return "\n".join(lines)
async def _extract_last_tool_calls(self, thread_id: str) -> list[dict[str, Any]]:
"""停图时 tool_calls 可能还在 pending_writes,序列化后的 messages 里看不到。"""
checkpointer = getattr(self.app.state, "checkpointer", None)
if checkpointer is None:
return []
config = {"configurable": {"thread_id": thread_id, "checkpoint_ns": ""}}
checkpoint_tuple = await checkpointer.aget_tuple(config)
if checkpoint_tuple is None:
return []
candidates: list[Any] = []
pending = getattr(checkpoint_tuple, "pending_writes", None) or []
for write in pending:
value = write[2] if isinstance(write, (tuple, list)) and len(write) > 2 else None
if value is not None:
candidates.append(value)
checkpoint = getattr(checkpoint_tuple, "checkpoint", {}) or {}
candidates.extend((checkpoint.get("channel_values", {}) or {}).get("messages") or [])
for message in reversed(candidates):
if isinstance(message, dict):
message_type = message.get("type") or message.get("role")
raw_calls = message.get("tool_calls")
additional = message.get("additional_kwargs") or {}
else:
message_type = getattr(message, "type", None)
raw_calls = getattr(message, "tool_calls", None)
additional = getattr(message, "additional_kwargs", None) or {}
if message_type not in {"ai", "assistant"}:
continue
coerced = [item for item in (_coerce_tool_call(call) for call in (raw_calls or [])) if item]
if coerced:
return coerced
legacy = additional.get("tool_calls") if isinstance(additional, dict) else None
if isinstance(legacy, list) and legacy:
coerced = [item for item in (_coerce_tool_call(call) for call in legacy) if item]
if coerced:
return coerced
return []
return []
async def _await_delivery(self, thread_id: str, *, timeout_s: float = 180.0) -> str | None:
# record.task 完成与 checkpoint 可见之间存在竞态:轮询等 AI 交付落盘。
deadline = time.monotonic() + timeout_s
while time.monotonic() < deadline:
delivery = await self._extract_latest_ai_text(thread_id)
if delivery is not None:
return delivery
await asyncio.sleep(1.0)
return None
async def _extract_latest_ai_text(self, thread_id: str) -> str | None:
"""Pull the seat's last AI delivery from the thread checkpoint."""
checkpointer = getattr(self.app.state, "checkpointer", None)
if checkpointer is None:
return None
config = {"configurable": {"thread_id": thread_id, "checkpoint_ns": ""}}
checkpoint_tuple = await checkpointer.aget_tuple(config)
if checkpoint_tuple is None:
return None
checkpoint = getattr(checkpoint_tuple, "checkpoint", {}) or {}
channel_values = checkpoint.get("channel_values", {})
messages = channel_values.get("messages") or []
for message in reversed(messages):
message_type = getattr(message, "type", None)
if message_type != "ai":
continue
content = getattr(message, "content", None)
raw = ""
if isinstance(content, str) and content.strip():
raw = content
elif isinstance(content, list):
raw = "".join(
part.get("text", "") for part in content if isinstance(part, dict)
).strip()
if not raw:
continue
try:
from deerflow.agents.roundtable_orchestrator.delivery import visible_seat_text
text = (visible_seat_text(raw) or raw).strip()
except Exception:
text = raw.strip()
if text:
return text[:MARKDOWN_BLOCK_MAX_CHARS]
return None
async def _flush_seat_results(self, run_id: str, seat_results: dict[str, dict[str, Any]]) -> None:
try:
await self.store.update_run(run_id, {"seat_results": seat_results})
except Exception:
logger.exception("task-analysis run %s seat_results flush failed", run_id)
async def _note_live(
self,
run_id: str,
seat_results: dict[str, dict[str, Any]],
seat_id: str,
*,
thread_id: str | None = None,
agent_run_id: str | None = None,
) -> None:
current = dict(seat_results.get(seat_id) or {})
if thread_id:
current["thread_id"] = thread_id
if agent_run_id:
current["agent_run_id"] = agent_run_id
seat_results[seat_id] = current
await self._flush_seat_results(run_id, seat_results)
# ── finalisation ────────────────────────────────────────────────────────
async def _finish_run(
self,
*,
run: dict[str, Any],
task: dict[str, Any],
status: str,
seat_results: dict[str, dict[str, Any]],
seat_names: dict[str, str],
error: str | None,
duration_ms: int,
) -> None:
run_id = run["id"]
blocks: dict[str, dict[str, Any]] = {}
if status in {"succeeded", "partial"}:
for seat_id, result in seat_results.items():
if seat_id == COORDINATOR_SEAT:
continue
if result.get("state") == "done" and result.get("delivery"):
blocks[seat_id] = {
"name": seat_names.get(seat_id, seat_id),
"seat_agent_id": seat_id,
"content": result["delivery"],
"format": "markdown",
}
try:
await self.store.create_version(
{"external_task_id": run["external_task_id"], "run_id": run_id, "blocks": blocks}
)
except Exception:
logger.exception("task-analysis run %s version persist failed", run_id)
await self.store.update_run(
run_id,
{
"status": status,
"finished_at": datetime.now(UTC).isoformat(),
"seat_results": seat_results,
"error": error,
},
)
_ = duration_ms # per-seat durations live in seat_results
@@ -0,0 +1,146 @@
"""Project live agent events into task-analysis process-message rows.
Scheduled tasks poll ``GET /runs/{id}/messages`` every few seconds and read the
agent's RunEventStore / checkpoint. Task analysis uses the same sources so the
drawer can show in-flight LLM turns and tool calls instead of waiting until the
seat finishes.
"""
from __future__ import annotations
import json
from typing import Any
_KIND_BY_EVENT = {
"llm.human.input": "input",
"llm.human": "input",
"llm.ai.response": "llm.ai",
"llm.ai": "llm.ai",
"llm.tool.result": "tool.result",
"llm.tool": "tool.result",
}
def flatten_message_content(content: Any) -> str:
"""Turn a LangChain dump / OpenAI-style payload into drawer text."""
if content is None:
return ""
if isinstance(content, str):
return content.strip()
if isinstance(content, list):
parts: list[str] = []
for item in content:
if isinstance(item, dict):
parts.append(str(item.get("text") or item.get("content") or ""))
else:
parts.append(str(item))
return "".join(parts).strip()
if not isinstance(content, dict):
return str(content).strip()
inner = content.get("content")
if inner is not None and inner is not content:
text = flatten_message_content(inner)
if text:
return text
tool_calls = content.get("tool_calls") or []
if isinstance(tool_calls, list) and tool_calls:
lines: list[str] = []
for call in tool_calls:
if not isinstance(call, dict):
name = getattr(call, "name", "") or ""
args = getattr(call, "args", {}) or {}
else:
name = str(call.get("name") or (call.get("function") or {}).get("name") or "")
args = call.get("args")
if args is None:
args = (call.get("function") or {}).get("arguments")
if isinstance(args, (dict, list)):
args_text = json.dumps(args, ensure_ascii=False)
else:
args_text = str(args or "")
lines.append(f"调用工具:{name}\n{args_text}".strip())
if lines:
return "\n\n".join(lines)
name = str(content.get("name") or "").strip()
if name:
body = flatten_message_content(content.get("output") or content.get("result") or "")
return f"{name}:{body}".strip(":")
try:
return json.dumps(content, ensure_ascii=False)
except TypeError:
return str(content)
def map_live_event_kind(event_type: str) -> str | None:
return _KIND_BY_EVENT.get(str(event_type or "").strip())
def live_rows_to_process_messages(
live_rows: list[dict[str, Any]],
*,
seat: str,
stage_index: int = 0,
) -> list[dict[str, Any]]:
"""Convert event-store / checkpoint rows into task-analysis message dicts."""
out: list[dict[str, Any]] = []
for index, row in enumerate(live_rows):
kind = map_live_event_kind(str(row.get("event_type") or ""))
if kind is None:
# Checkpoint snapshots use LangChain message types.
raw_type = ""
content = row.get("content")
if isinstance(content, dict):
raw_type = str(content.get("type") or content.get("role") or "")
if raw_type in {"human", "user"}:
kind = "input"
elif raw_type in {"ai", "assistant"}:
kind = "llm.ai"
elif raw_type == "tool":
kind = "tool.result"
else:
continue
text = flatten_message_content(row.get("content"))
if not text:
continue
seq = row.get("seq")
out.append(
{
"id": str(row.get("id") or f"live-{seat}-{seq if seq is not None else index}"),
"run_id": row.get("run_id"),
"seq": seq if seq is not None else index,
"seat_agent_id": seat,
"stage_index": int(row.get("stage_index") or stage_index),
"event_type": kind,
"content": text,
"created_at": row.get("created_at"),
"metadata": {"source": (row.get("metadata") or {}).get("source") or "live"},
}
)
return out
def merge_process_messages(
*,
stored: list[dict[str, Any]],
live: list[dict[str, Any]],
limit: int = 500,
) -> list[dict[str, Any]]:
"""Keep orchestration notes (input/artifact) and overlay live LLM/tool turns.
Live human input is dropped — the stored ``input`` row is the prompt we
already showed in the drawer. Live AI/tool rows replace stored ``llm.ai``
once they appear, so the user sees in-flight output instead of waiting for
the final flush.
"""
live_body = [row for row in live if row.get("event_type") != "input"]
live_has_ai = any(row.get("event_type") in {"llm.ai", "tool.result"} for row in live_body)
if not live_body:
return stored[:limit]
kept: list[dict[str, Any]] = []
for row in stored:
kind = row.get("event_type")
if live_has_ai and kind in {"llm.ai", "tool.result"}:
continue
kept.append(row)
return (kept + live_body)[:limit]
@@ -0,0 +1,294 @@
"""Scheduler service for external-task chain bindings (任务分析定时).
Mirrors ``ScheduledTaskService``'s proven skeleton: a 30-second poll loop
scans due bindings and spawns one asyncio task per due binding, refreshing
each binding's chain snapshot before executing, and skipping bindings whose
chain has been deleted / disabled (marked ``chain_missing``). A watchdog pass
fails runs stuck in pending/running past a grace window so one wedged
execution cannot block the schedule.
Overlap policy (数据交付第三期定稿口径):调度不管上一轮跑没跑完——到期照常触发、
照常开新期,同任务允许多轮并行,不跳过不顺延。因此**没有**在跑守卫;去重仅靠
``create_run_once`` 的 (binding_id, scheduled_for) 幂等键。每次执行建 run 的同时开
一期数据交付刻度(``open_period``,run_id 幂等锚点),智能体提交的数据落在提交
时刻的当前最新一期。
"""
from __future__ import annotations
import asyncio
import logging
from datetime import UTC, datetime, timedelta
from typing import Any
from zoneinfo import ZoneInfo
from croniter import croniter
from deerflow.persistence.task_analysis.base import TaskAnalysisStore
from deerflow.runtime.task_analysis.executor import ChainBindingExecutor
logger = logging.getLogger(__name__)
STUCK_GRACE_SECONDS = 60 * 60 # 1h without finishing → watchdog marks failed
def next_run_at(cron_expr: str, timezone: str, base: datetime | None = None) -> datetime:
tz = ZoneInfo(timezone or "UTC")
base_local = (base or datetime.now(UTC)).astimezone(tz)
next_local = croniter(cron_expr, base_local).get_next(datetime)
if next_local.tzinfo is None:
next_local = next_local.replace(tzinfo=tz)
return next_local.astimezone(UTC)
def expand_chain_stages(chain: dict[str, Any]) -> list[list[str]]:
seats = chain.get("seats") or []
stages = chain.get("stages")
if stages:
return [list(stage) for stage in stages]
return [[seat["agent_id"]] for seat in seats]
def build_chain_snapshot(chain: dict[str, Any]) -> dict[str, Any]:
return {
"title": chain.get("title") or chain.get("id") or "",
"seats": chain.get("seats") or [],
"stages": expand_chain_stages(chain),
"stage_goals": chain.get("stage_goals") or [],
"coordinator_prompt": chain.get("coordinator_prompt"),
}
class TaskAnalysisService:
def __init__(
self,
app: Any,
store: TaskAnalysisStore,
*,
poll_interval_seconds: float = 30.0,
) -> None:
self.app = app
self.store = store
self.poll_interval_seconds = poll_interval_seconds
self._stopping = asyncio.Event()
self._loop_task: asyncio.Task | None = None
# ── lifecycle ───────────────────────────────────────────────────────────
async def start(self) -> None:
self._stopping = asyncio.Event()
self._loop_task = asyncio.create_task(self._poll_loop(), name="task-analysis-scheduler")
async def stop(self) -> None:
self._stopping.set()
if self._loop_task is not None:
try:
await asyncio.wait_for(self._loop_task, timeout=5)
except (TimeoutError, asyncio.CancelledError):
self._loop_task.cancel()
self._loop_task = None
async def _poll_loop(self) -> None:
while not self._stopping.is_set():
try:
await self._reconcile_stuck_runs()
except asyncio.CancelledError:
raise
except Exception:
logger.exception("task-analysis watchdog scan failed")
try:
due = await self.store.due_bindings(datetime.now(UTC), limit=20)
for binding in due:
# 到期即触发,不管上一轮是否结束:同任务允许多轮并行。
asyncio.create_task(self._execute_and_untrack(binding))
except asyncio.CancelledError:
raise
except Exception:
logger.exception("task-analysis scan failed")
try:
await asyncio.wait_for(self._stopping.wait(), timeout=self.poll_interval_seconds)
except TimeoutError:
pass
async def _execute_and_untrack(self, binding: dict[str, Any]) -> None:
try:
await self.execute_binding(binding["id"], manual=False)
except Exception:
logger.exception("task-analysis binding %s execution crashed", binding.get("id"))
async def _reconcile_stuck_runs(self) -> None:
cutoff = datetime.now(UTC) - timedelta(seconds=STUCK_GRACE_SECONDS)
for run in await self.store.stuck_runs(cutoff):
logger.warning("task-analysis run %s stuck since %s — failing", run["id"], run["started_at"])
await self.store.update_run(
run["id"],
{
"status": "failed",
"finished_at": datetime.now(UTC).isoformat(),
"error": "执行超时,由调度器看门狗标记失败",
},
)
# ── execution entry (scheduler + manual trigger share this) ─────────────
async def execute_binding(self, binding_id: str, *, manual: bool) -> dict[str, Any] | None:
"""Synchronous full path (scheduler loop / tests): prepare + carry out."""
prepared = await self.prepare_binding_run(binding_id, manual=manual)
if prepared is None:
return None
run, binding, task, snapshot = prepared
await self._carry_out(run, binding, task, snapshot)
return await self.store.get_run(run["id"])
async def launch_binding(self, binding_id: str, *, manual: bool) -> dict[str, Any] | None:
"""Async entry (manual trigger API): create the run, execute in background.
Returns the freshly created run row (status=pending) immediately so the
HTTP caller can start polling; None when skipped (chain/task missing).
"""
prepared = await self.prepare_binding_run(binding_id, manual=manual)
if prepared is None:
return None
run, binding, task, snapshot = prepared
asyncio.create_task(self._carry_out_and_swallow(run, binding, task, snapshot))
return run
async def prepare_binding_run(
self, binding_id: str, *, manual: bool
) -> tuple[dict[str, Any], dict[str, Any], dict[str, Any], dict[str, Any]] | None:
"""Validate + snapshot + create the run row. Shared by sync/async paths."""
binding = await self.store.get_binding_by_id(binding_id)
if binding is None:
return None
task = await self.store.get_task(binding["external_task_id"])
if task is None:
await self.store.update_binding(binding_id, {"last_status": "task_missing"})
return None
chain = await self._load_chain(binding.get("chain_id") or "", binding.get("created_by") or "")
if chain is None or chain.get("enabled") is False:
# 口径:链被删/停用 → 跳过执行并标记,绑定保留。
await self.store.update_binding(
binding_id,
{
"last_status": "chain_missing",
"next_run_at": None
if (manual or self._is_once(binding))
else self._advance(binding).isoformat(),
},
)
return None
snapshot = build_chain_snapshot(chain)
stages = snapshot["stages"]
seat_names = {seat["agent_id"]: seat.get("name") or seat["agent_id"] for seat in snapshot["seats"]}
scheduled_for = datetime.now(UTC).replace(microsecond=0) if manual else _scheduled_for(binding)
run = await self.store.create_run_once(
{
"binding_id": binding_id,
"external_task_id": binding["external_task_id"],
"trigger": "manual" if manual else "schedule",
"scheduled_for": scheduled_for,
"status": "pending",
"stages_snapshot": stages,
"stage_goals_snapshot": snapshot["stage_goals"],
"seat_names_snapshot": seat_names,
"seat_results": {},
}
)
await self.store.update_binding(
binding_id,
{
"chain_snapshot": snapshot,
# once 绑定永不排期(next_run_at 恒空,调度器自然不再触发)。
**(
{}
if (manual or self._is_once(binding))
else {"next_run_at": self._advance(binding).isoformat()}
),
},
)
# 数据交付已去掉时间轴(期)维度:调度轮次不再开新期,席位提交统一
# 合并进任务唯一的当前数据(首次提交时由 submit 兜底开期)。
return run, binding, task, snapshot
async def _carry_out_and_swallow(
self,
run: dict[str, Any],
binding: dict[str, Any],
task: dict[str, Any],
snapshot: dict[str, Any],
) -> None:
try:
await self._carry_out(run, binding, task, snapshot)
except Exception:
logger.exception("task-analysis background run %s crashed", run.get("id"))
async def _carry_out(
self,
run: dict[str, Any],
binding: dict[str, Any],
task: dict[str, Any],
snapshot: dict[str, Any],
) -> None:
executor = ChainBindingExecutor(self.app, self.store)
user_id = binding.get("created_by") or ""
ctx_token = None
if user_id:
from deerflow.runtime.task_analysis.executor import _RuntimeUser
from deerflow.runtime.user_context import reset_current_user, set_current_user
ctx_token = set_current_user(_RuntimeUser(id=user_id))
try:
await executor.execute(run, {**binding, "chain_snapshot": snapshot}, task)
finally:
if ctx_token is not None:
reset_current_user(ctx_token)
final = await self.store.get_run(run["id"])
status = (final or {}).get("status") or "unknown"
await self.store.update_binding(
binding["id"],
{
"last_run_at": datetime.now(UTC).isoformat(),
"last_status": status,
},
)
# ── helpers ─────────────────────────────────────────────────────────────
def _advance(self, binding: dict[str, Any]) -> datetime:
return next_run_at(
binding.get("cron_expr") or "0 9 * * *",
binding.get("timezone") or "Asia/Shanghai",
)
@staticmethod
def _is_once(binding: dict[str, Any]) -> bool:
return (binding.get("schedule_type") or "cron") == "once"
async def _load_chain(self, chain_id: str, user_id: str) -> dict[str, Any] | None:
chain_store = getattr(self.app.state, "roundtable_chain_store", None)
if chain_store is None or not chain_id:
return None
# created_by 为空(旧数据/未登录创建)时回退 default 用户,再试公共链可见性。
effective_user = user_id or "default"
try:
chain = await chain_store.get_chain(chain_id, effective_user)
if chain is None and effective_user != "default":
chain = await chain_store.get_chain(chain_id, "default")
return chain
except Exception:
logger.exception("task-analysis load chain %s failed", chain_id)
return None
def _scheduled_for(binding: dict[str, Any]) -> datetime:
value = binding.get("next_run_at")
if isinstance(value, datetime):
return value
if isinstance(value, str) and value:
try:
return datetime.fromisoformat(value)
except ValueError:
pass
return datetime.now(UTC).replace(microsecond=0)