feat: 岗位会商命令幂等与圆桌后台作业租约调度
岗位会商写操作引入 command_id 幂等与版本锁,前端支持同键重试;圆桌后台作业改为 DB 租约 dispatcher、去重与草稿 outbox 补偿,并持久化 intent turns。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
4f536d9b39
commit
75963c9ad9
51 files changed
+4449
-670
No files matched your search
@@ -255,23 +255,23 @@ async def langgraph_runtime(app: FastAPI) -> AsyncGenerator[None, None]:
|
||||
|
||||
app.state.roundtable_draft_share_store = make_roundtable_draft_share_store(sf)
|
||||
|
||||
from deerflow.persistence.roundtable_chains import make_roundtable_chain_store
|
||||
|
||||
app.state.roundtable_chain_store = make_roundtable_chain_store(sf)
|
||||
|
||||
# 独立的岗位协同 Session / Node 存储。它只索引业务流程状态,真实消息和
|
||||
# 文件仍复用 LangGraph thread/checkpoint 与现有 artifact API。
|
||||
from deerflow.persistence.position_roundtable import make_position_roundtable_store
|
||||
|
||||
app.state.position_roundtable_store = make_position_roundtable_store(sf)
|
||||
|
||||
# 岗位会商工作视角(名称 / 顺序 / 特殊意图岗)是全局配置,和组织/RBAC
|
||||
# 岗位分开保存。启动只补齐缺失内置项,不覆盖管理员的既有修改。
|
||||
from app.gateway.position_role_defaults import POSITION_ROLE_SEED
|
||||
from deerflow.persistence.position_roles import make_position_role_store
|
||||
|
||||
app.state.position_role_store = make_position_role_store(sf)
|
||||
await app.state.position_role_store.ensure_seed(POSITION_ROLE_SEED)
|
||||
from deerflow.persistence.roundtable_chains import make_roundtable_chain_store
|
||||
|
||||
app.state.roundtable_chain_store = make_roundtable_chain_store(sf)
|
||||
|
||||
# 独立的岗位协同 Session / Node 存储。它只索引业务流程状态,真实消息和
|
||||
# 文件仍复用 LangGraph thread/checkpoint 与现有 artifact API。
|
||||
from deerflow.persistence.position_roundtable import make_position_roundtable_store
|
||||
|
||||
app.state.position_roundtable_store = make_position_roundtable_store(sf)
|
||||
|
||||
# 岗位会商工作视角(名称 / 顺序 / 特殊意图岗)是全局配置,和组织/RBAC
|
||||
# 岗位分开保存。启动只补齐缺失内置项,不覆盖管理员的既有修改。
|
||||
from app.gateway.position_role_defaults import POSITION_ROLE_SEED
|
||||
from deerflow.persistence.position_roles import make_position_role_store
|
||||
|
||||
app.state.position_role_store = make_position_role_store(sf)
|
||||
await app.state.position_role_store.ensure_seed(POSITION_ROLE_SEED)
|
||||
|
||||
from deerflow.persistence.business_mapping import make_business_mapping_store
|
||||
|
||||
@@ -293,30 +293,30 @@ async def langgraph_runtime(app: FastAPI) -> AsyncGenerator[None, None]:
|
||||
|
||||
app.state.dashboard_session_store = make_dashboard_session_store(sf)
|
||||
|
||||
from deerflow.persistence.task_buttons import make_task_button_store
|
||||
|
||||
app.state.task_button_store = make_task_button_store(sf)
|
||||
|
||||
from deerflow.integrations.weknora.runtime import register_llmwiki_store
|
||||
from deerflow.persistence.llmwiki import make_llmwiki_store
|
||||
|
||||
app.state.llmwiki_store = make_llmwiki_store(sf)
|
||||
register_llmwiki_store(app.state.llmwiki_store)
|
||||
|
||||
from deerflow.persistence.taskcop_tasks import make_taskcop_task_store
|
||||
|
||||
app.state.taskcop_task_store = make_taskcop_task_store(sf)
|
||||
|
||||
from deerflow.persistence.task_reports import make_task_report_store
|
||||
|
||||
app.state.task_report_store = make_task_report_store(sf)
|
||||
|
||||
from deerflow.persistence.roundtable_artifact_submissions import make_roundtable_artifact_submission_store
|
||||
|
||||
# Task-scoped artifact submission status: shared by task; actor is audit-only.
|
||||
app.state.roundtable_artifact_submission_store = make_roundtable_artifact_submission_store(sf)
|
||||
|
||||
from deerflow.persistence.parallel_agent_tasks import make_parallel_agent_task_store
|
||||
from deerflow.persistence.task_buttons import make_task_button_store
|
||||
|
||||
app.state.task_button_store = make_task_button_store(sf)
|
||||
|
||||
from deerflow.integrations.weknora.runtime import register_llmwiki_store
|
||||
from deerflow.persistence.llmwiki import make_llmwiki_store
|
||||
|
||||
app.state.llmwiki_store = make_llmwiki_store(sf)
|
||||
register_llmwiki_store(app.state.llmwiki_store)
|
||||
|
||||
from deerflow.persistence.taskcop_tasks import make_taskcop_task_store
|
||||
|
||||
app.state.taskcop_task_store = make_taskcop_task_store(sf)
|
||||
|
||||
from deerflow.persistence.task_reports import make_task_report_store
|
||||
|
||||
app.state.task_report_store = make_task_report_store(sf)
|
||||
|
||||
from deerflow.persistence.roundtable_artifact_submissions import make_roundtable_artifact_submission_store
|
||||
|
||||
# Task-scoped artifact submission status: shared by task; actor is audit-only.
|
||||
app.state.roundtable_artifact_submission_store = make_roundtable_artifact_submission_store(sf)
|
||||
|
||||
from deerflow.persistence.parallel_agent_tasks import make_parallel_agent_task_store
|
||||
|
||||
app.state.parallel_agent_task_store = make_parallel_agent_task_store(sf)
|
||||
|
||||
@@ -332,6 +332,13 @@ async def langgraph_runtime(app: FastAPI) -> AsyncGenerator[None, None]:
|
||||
# (Phase 2 后台编排图写进度用)。
|
||||
_register_roundtable_job_default_store(app.state.roundtable_job_store)
|
||||
|
||||
# 意图回合单飞 store(Phase 4):``/api/intent/stream`` 据此对 (thread_id,
|
||||
# client_turn_id) 跨 worker 单飞 + 幂等回放,杜绝并发双发意图。sf 为 None(无 DB)
|
||||
# 时为 None,路由侧判空跳过单飞、走原流程。
|
||||
from deerflow.persistence.intent_turns import make_intent_turn_store
|
||||
|
||||
app.state.intent_turn_store = make_intent_turn_store(sf)
|
||||
|
||||
# 圆桌会商「诊断日志」store:会商全流程各处报错 / 关键事件落库,供管理员诊断页筛选。
|
||||
# 同样登记进程级默认仓储——harness 侧编排引擎 / 进程内网关跨边界写诊断用。
|
||||
from deerflow.persistence.roundtable_diagnostics import (
|
||||
@@ -393,18 +400,32 @@ async def langgraph_runtime(app: FastAPI) -> AsyncGenerator[None, None]:
|
||||
# task 作业把最终报告写回**独立的** task-draft 存储(不分权)。
|
||||
task_draft_store=getattr(app.state, "roundtable_task_draft_store", None),
|
||||
)
|
||||
|
||||
# Phase 3 dispatcher:跨 worker 领取 queued/租约过期作业 + outbox 补偿器。
|
||||
# 每个 worker 进程都跑一个;谁先 claim_job 成功谁执行(DB 条件 UPDATE 互斥),
|
||||
# 崩溃的 worker 租约过期后由其它 worker 接管。路由 start/resume 后 nudge 立即唤醒。
|
||||
from app.gateway.roundtable_job_dispatcher import RoundtableJobDispatcher
|
||||
|
||||
app.state.roundtable_job_dispatcher = RoundtableJobDispatcher(
|
||||
app.state.roundtable_job_store,
|
||||
app.state.roundtable_job_executor,
|
||||
draft_store=getattr(app.state, "roundtable_draft_store", None),
|
||||
task_draft_store=getattr(app.state, "roundtable_task_draft_store", None),
|
||||
)
|
||||
app.state.roundtable_job_dispatcher.start()
|
||||
else:
|
||||
app.state.roundtable_job_executor = None
|
||||
app.state.roundtable_job_dispatcher = None
|
||||
|
||||
# Run event store (has its own factory with config-driven backend selection)
|
||||
run_events_config = getattr(config, "run_events", None)
|
||||
app.state.run_event_store = make_run_event_store(run_events_config)
|
||||
|
||||
# RunManager with store backing for persistence
|
||||
app.state.run_manager = RunManager(store=app.state.run_store)
|
||||
from deerflow.runtime import make_redis_concurrency_gate
|
||||
|
||||
app.state.concurrency_gate = make_redis_concurrency_gate(config)
|
||||
# RunManager with store backing for persistence
|
||||
app.state.run_manager = RunManager(store=app.state.run_store)
|
||||
from deerflow.runtime import make_redis_concurrency_gate
|
||||
|
||||
app.state.concurrency_gate = make_redis_concurrency_gate(config)
|
||||
|
||||
try:
|
||||
yield
|
||||
@@ -412,16 +433,19 @@ async def langgraph_runtime(app: FastAPI) -> AsyncGenerator[None, None]:
|
||||
from deerflow.persistence.notifications import set_notification_store
|
||||
|
||||
set_notification_store(None)
|
||||
dispatcher = getattr(app.state, "roundtable_job_dispatcher", None)
|
||||
if dispatcher is not None:
|
||||
await dispatcher.stop()
|
||||
queue = getattr(app.state, "knowledge_auto_ingest_queue", None)
|
||||
if queue is not None:
|
||||
from deerflow.knowledge.auto_ingest import set_auto_ingest_queue
|
||||
|
||||
await queue.stop()
|
||||
set_auto_ingest_queue(None)
|
||||
await close_engine()
|
||||
gate = getattr(app.state, "concurrency_gate", None)
|
||||
if gate is not None:
|
||||
await gate.aclose()
|
||||
await close_engine()
|
||||
gate = getattr(app.state, "concurrency_gate", None)
|
||||
if gate is not None:
|
||||
await gate.aclose()
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
@@ -447,12 +471,12 @@ get_run_manager: Callable[[Request], RunManager] = _require("run_manager", "Run
|
||||
get_checkpointer: Callable[[Request], Checkpointer] = _require("checkpointer", "Checkpointer")
|
||||
get_run_event_store: Callable[[Request], RunEventStore] = _require("run_event_store", "Run event store")
|
||||
get_feedback_repo: Callable[[Request], FeedbackRepository] = _require("feedback_repo", "Feedback")
|
||||
get_run_store: Callable[[Request], RunStore] = _require("run_store", "Run store")
|
||||
|
||||
|
||||
def get_concurrency_gate(request: Request):
|
||||
"""Return the optional cross-worker concurrency gate."""
|
||||
return getattr(request.app.state, "concurrency_gate", None)
|
||||
get_run_store: Callable[[Request], RunStore] = _require("run_store", "Run store")
|
||||
|
||||
|
||||
def get_concurrency_gate(request: Request):
|
||||
"""Return the optional cross-worker concurrency gate."""
|
||||
return getattr(request.app.state, "concurrency_gate", None)
|
||||
|
||||
|
||||
def get_store(request: Request):
|
||||
@@ -624,34 +648,34 @@ _cached_local_provider: LocalAuthProvider | None = None
|
||||
_cached_repo: SQLiteUserRepository | None = None
|
||||
|
||||
|
||||
def get_request_access_token(request: Request) -> str | None:
|
||||
"""Extract access token from cookie or Authorization header.
|
||||
|
||||
Priority:
|
||||
1. ``access_token`` cookie (browser session mode)
|
||||
2. ``Authorization: Bearer <token>`` header (API client mode)
|
||||
3. ``access_token`` query parameter for the WeKnora iframe bootstrap only
|
||||
"""
|
||||
cookie_token = request.cookies.get("access_token")
|
||||
if cookie_token:
|
||||
return cookie_token
|
||||
|
||||
authorization = request.headers.get("authorization", "").strip()
|
||||
if authorization:
|
||||
scheme, _, token = authorization.partition(" ")
|
||||
if scheme.lower() == "bearer":
|
||||
token = token.strip()
|
||||
if token:
|
||||
return token
|
||||
|
||||
from app.gateway.proxy_path import app_relative_path
|
||||
|
||||
path = app_relative_path(request).rstrip("/")
|
||||
if path.startswith("/api/llmwiki/knowledge-bases/") and path.endswith("/weknora/frame"):
|
||||
query_token = (request.query_params.get("access_token") or "").strip()
|
||||
if query_token:
|
||||
return query_token
|
||||
return None
|
||||
def get_request_access_token(request: Request) -> str | None:
|
||||
"""Extract access token from cookie or Authorization header.
|
||||
|
||||
Priority:
|
||||
1. ``access_token`` cookie (browser session mode)
|
||||
2. ``Authorization: Bearer <token>`` header (API client mode)
|
||||
3. ``access_token`` query parameter for the WeKnora iframe bootstrap only
|
||||
"""
|
||||
cookie_token = request.cookies.get("access_token")
|
||||
if cookie_token:
|
||||
return cookie_token
|
||||
|
||||
authorization = request.headers.get("authorization", "").strip()
|
||||
if authorization:
|
||||
scheme, _, token = authorization.partition(" ")
|
||||
if scheme.lower() == "bearer":
|
||||
token = token.strip()
|
||||
if token:
|
||||
return token
|
||||
|
||||
from app.gateway.proxy_path import app_relative_path
|
||||
|
||||
path = app_relative_path(request).rstrip("/")
|
||||
if path.startswith("/api/llmwiki/knowledge-bases/") and path.endswith("/weknora/frame"):
|
||||
query_token = (request.query_params.get("access_token") or "").strip()
|
||||
if query_token:
|
||||
return query_token
|
||||
return None
|
||||
|
||||
|
||||
def get_local_provider() -> LocalAuthProvider:
|
||||
|
||||
@@ -0,0 +1,355 @@
|
||||
"""圆桌后台作业 dispatcher(Phase 3):跨 worker 领取 + 崩溃接管 + outbox 补偿。
|
||||
|
||||
把「谁来跑这个作业」从「发起请求的那个 worker 的进程内存」收敛到 **DB 租约**:
|
||||
|
||||
- **领取(claim)**:后台循环定期 ``list_claimable``(``queued`` 新作业/续跑,或 ``running``
|
||||
但租约过期的僵尸),对每个候选用 ``claim_job`` 的**原子条件 UPDATE** 抢占 —— 多 worker
|
||||
并发扫到同一候选时只有一个 ``rowcount==1``,其余拿到 None 自动跳过。一致性来自 DB,
|
||||
不是进程内锁。
|
||||
- **心跳**:领取成功后交给 ``RoundtableJobExecutor`` 跑,执行器内部定期 ``renew_lease``;
|
||||
worker 崩溃则心跳停止,租约最长一个 TTL 后过期,下一轮循环在其它 worker 重新领取
|
||||
(``attempt+1``)—— 崩溃可接管。
|
||||
- **启动扫描**:本 worker 启动时立即跑一轮 ``dispatch_once``,尽快接走历史遗留的
|
||||
queued / 租约过期作业(不用等第一个轮询周期)。
|
||||
- **outbox 补偿器**:``draft_persist_pending=1`` 的作业(后台研讨结果因乐观锁冲突没能写回
|
||||
草稿)在这里被重新投影到草稿 step2.runs,成功后清标记 —— 「研讨结果不丢」由 DB 标记驱动。
|
||||
|
||||
所有 worker 都跑同一个 dispatcher;谁先 ``claim_job`` 成功谁执行,天然幂等、互斥。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
from contextlib import suppress
|
||||
from datetime import UTC, datetime, timedelta
|
||||
from typing import Any
|
||||
|
||||
from app.gateway.roundtable_job_executor import (
|
||||
_LEASE_TTL_SECONDS,
|
||||
WORKER_ID,
|
||||
params_from_snapshot,
|
||||
)
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# dispatcher 轮询间隔(秒)。路由 start/resume 成功后会 ``nudge()`` 立即唤醒,故正常路径
|
||||
# 几乎无感延迟;该间隔只是「没有 nudge 时的兜底」,兼作崩溃接管的发现周期。
|
||||
_DISPATCH_POLL_INTERVAL = 3.0
|
||||
# outbox 补偿器轮询间隔(秒)。pending 标记是低频异常路径,慢一点无妨。
|
||||
_COMPENSATE_INTERVAL = 10.0
|
||||
# 单轮最多领取的作业数(防止一次扫太多把事件循环占死)。
|
||||
_CLAIM_BATCH = 20
|
||||
|
||||
|
||||
class RoundtableJobDispatcher:
|
||||
"""跨 worker 的圆桌后台作业调度器(每个 worker 进程一个实例,lifespan 起停)。"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
store,
|
||||
executor,
|
||||
*,
|
||||
draft_store=None,
|
||||
task_draft_store=None,
|
||||
worker_id: str | None = None,
|
||||
) -> None:
|
||||
self._store = store
|
||||
self._executor = executor
|
||||
# outbox 补偿器投影草稿用(个人草稿 / task 草稿两个存储)。
|
||||
self._draft_store = draft_store
|
||||
self._task_draft_store = task_draft_store
|
||||
self._worker_id = worker_id or WORKER_ID
|
||||
self._wake = asyncio.Event()
|
||||
self._task: asyncio.Task | None = None
|
||||
self._stopping = False
|
||||
self._last_compensate = datetime.now(UTC)
|
||||
|
||||
# ── 生命周期 ─────────────────────────────────────────────────────────────
|
||||
|
||||
def start(self) -> None:
|
||||
"""启动后台循环(lifespan startup 调用)。幂等。"""
|
||||
if self._task is not None and not self._task.done():
|
||||
return
|
||||
self._stopping = False
|
||||
self._task = asyncio.create_task(self._loop(), name="roundtable-job-dispatcher")
|
||||
logger.info("[roundtable-jobs] dispatcher started (worker=%s)", self._worker_id)
|
||||
|
||||
async def stop(self) -> None:
|
||||
"""停止后台循环(lifespan shutdown 调用)。"""
|
||||
self._stopping = True
|
||||
self._wake.set()
|
||||
if self._task is not None:
|
||||
self._task.cancel()
|
||||
with suppress(asyncio.CancelledError, Exception):
|
||||
await self._task
|
||||
self._task = None
|
||||
|
||||
def nudge(self) -> None:
|
||||
"""立即唤醒一轮领取(路由 start/resume 成功后调用,降低启动延迟)。"""
|
||||
self._wake.set()
|
||||
|
||||
# ── 主循环 ───────────────────────────────────────────────────────────────
|
||||
|
||||
async def _loop(self) -> None:
|
||||
# 启动即扫一轮:尽快接管历史遗留 queued / 租约过期(僵尸)作业。
|
||||
await self._safe_dispatch()
|
||||
while not self._stopping:
|
||||
try:
|
||||
await asyncio.wait_for(self._wake.wait(), timeout=_DISPATCH_POLL_INTERVAL)
|
||||
except TimeoutError:
|
||||
pass
|
||||
self._wake.clear()
|
||||
if self._stopping:
|
||||
break
|
||||
await self._safe_dispatch()
|
||||
# outbox 补偿器按自己的节奏跑(低频)。
|
||||
if (datetime.now(UTC) - self._last_compensate).total_seconds() >= _COMPENSATE_INTERVAL:
|
||||
self._last_compensate = datetime.now(UTC)
|
||||
await self._safe_compensate()
|
||||
|
||||
async def _safe_dispatch(self) -> None:
|
||||
try:
|
||||
await self.dispatch_once()
|
||||
except Exception: # noqa: BLE001 — 单轮失败不能杀死循环,下轮重试
|
||||
logger.exception("[roundtable-jobs] dispatch round failed")
|
||||
|
||||
async def _safe_compensate(self) -> None:
|
||||
try:
|
||||
await self.compensate_once()
|
||||
except Exception: # noqa: BLE001
|
||||
logger.exception("[roundtable-jobs] compensate round failed")
|
||||
|
||||
# ── 领取 ─────────────────────────────────────────────────────────────────
|
||||
|
||||
async def dispatch_once(self) -> int:
|
||||
"""扫一轮可领取作业并逐个条件抢占。返回本轮成功启动的作业数。"""
|
||||
now = datetime.now(UTC)
|
||||
await self._reap_cancel_requested(now)
|
||||
try:
|
||||
candidate_ids = await self._store.list_claimable(now=now, limit=_CLAIM_BATCH)
|
||||
except Exception: # noqa: BLE001
|
||||
logger.warning("[roundtable-jobs] list_claimable failed", exc_info=True)
|
||||
return 0
|
||||
if not candidate_ids:
|
||||
return 0
|
||||
|
||||
started = 0
|
||||
lease_until = now + timedelta(seconds=_LEASE_TTL_SECONDS)
|
||||
for job_id in candidate_ids:
|
||||
try:
|
||||
row = await self._store.claim_job(
|
||||
job_id, lease_owner=self._worker_id, lease_until=lease_until
|
||||
)
|
||||
except Exception: # noqa: BLE001
|
||||
logger.warning("[roundtable-jobs] claim_job failed for %s", job_id, exc_info=True)
|
||||
continue
|
||||
if row is None:
|
||||
# 别的 worker 先领了 / 状态已变 —— 条件 UPDATE rowcount=0,跳过即可。
|
||||
continue
|
||||
params = params_from_snapshot(
|
||||
row.get("input_snapshot"), job_id=job_id, lease_owner=self._worker_id
|
||||
)
|
||||
if params is None:
|
||||
# 快照缺失/损坏:无法重建执行参数 —— 标记 error 并释放活跃键,避免僵死占位。
|
||||
logger.error(
|
||||
"[roundtable-jobs] job %s claimed but snapshot missing/corrupt; marking error",
|
||||
job_id,
|
||||
)
|
||||
with suppress(Exception):
|
||||
await self._store.update_progress(
|
||||
job_id,
|
||||
lease_owner=self._worker_id,
|
||||
status="error",
|
||||
error="input_snapshot 缺失或损坏,无法在后台重建研讨",
|
||||
)
|
||||
continue
|
||||
self._executor.start_job(params)
|
||||
started += 1
|
||||
logger.info(
|
||||
"[roundtable-jobs] worker=%s claimed job %s (attempt=%s, resume=%s)",
|
||||
self._worker_id, job_id, row.get("attempt"), params.is_resume,
|
||||
)
|
||||
return started
|
||||
|
||||
async def _reap_cancel_requested(self, now: datetime) -> int:
|
||||
"""收口持租 worker 崩溃后遗留的 ``cancel_requested`` 作业。
|
||||
|
||||
有效租约仍在时,必须让持租 worker 的取消看门狗收口;无租约或租约过期时,已经
|
||||
不存在可信的持租者,此处通过 ``finalize_cancel`` 的条件 UPDATE 安全地回收。
|
||||
"""
|
||||
try:
|
||||
candidate_ids = await self._store.list_cancel_reapable(
|
||||
now=now, limit=_CLAIM_BATCH
|
||||
)
|
||||
except Exception: # noqa: BLE001
|
||||
logger.warning("[roundtable-jobs] list_cancel_reapable failed", exc_info=True)
|
||||
return 0
|
||||
|
||||
finalized = 0
|
||||
for job_id in candidate_ids:
|
||||
try:
|
||||
row = await self._store.finalize_cancel(job_id)
|
||||
except Exception: # noqa: BLE001
|
||||
logger.warning(
|
||||
"[roundtable-jobs] finalize orphaned cancellation failed for %s",
|
||||
job_id,
|
||||
exc_info=True,
|
||||
)
|
||||
continue
|
||||
if row is not None:
|
||||
finalized += 1
|
||||
logger.info(
|
||||
"[roundtable-jobs] reaped orphaned cancellation for job %s", job_id
|
||||
)
|
||||
return finalized
|
||||
|
||||
# ── outbox 补偿器 ────────────────────────────────────────────────────────
|
||||
|
||||
def _draft_store_for(self, task_id: str | None):
|
||||
"""task 作业 → task-draft 存储;普通作业 → 个人草稿存储(与执行器同规则)。"""
|
||||
return self._task_draft_store if task_id else self._draft_store
|
||||
|
||||
async def compensate_once(self) -> int:
|
||||
"""把 ``draft_persist_pending=1`` 的作业结果投影回草稿,成功后清标记。
|
||||
|
||||
投影逻辑与执行器 ``_write_run_to_draft`` 的终态写一致:读最新草稿 → 按
|
||||
``run_id=job-<jobId>`` upsert 一个 run(用作业行里的 dialogues/step3/status)→
|
||||
带 ``expected_version`` 乐观锁写回。写成功(或草稿已含该 run 的终态)即清标记。
|
||||
返回本轮成功投影的作业数。
|
||||
"""
|
||||
try:
|
||||
pending = await self._store.list_draft_persist_pending(limit=_CLAIM_BATCH)
|
||||
except Exception: # noqa: BLE001
|
||||
logger.warning("[roundtable-jobs] list_draft_persist_pending failed", exc_info=True)
|
||||
return 0
|
||||
if not pending:
|
||||
return 0
|
||||
|
||||
projected = 0
|
||||
for job in pending:
|
||||
job_id = job.get("id")
|
||||
draft_id = job.get("draft_id")
|
||||
draft_store = self._draft_store_for(job.get("task_id"))
|
||||
if not draft_id or draft_store is None:
|
||||
# 无草稿可投影(如纯 task 作业但 task_store 未配)——直接清标记,避免死循环。
|
||||
with suppress(Exception):
|
||||
await self._store.clear_draft_persist_pending(job_id)
|
||||
continue
|
||||
try:
|
||||
ok = await self._project_job_to_draft(draft_store, job)
|
||||
except Exception: # noqa: BLE001
|
||||
logger.warning(
|
||||
"[roundtable-jobs] compensate project failed for job %s", job_id, exc_info=True
|
||||
)
|
||||
continue
|
||||
if ok:
|
||||
with suppress(Exception):
|
||||
await self._store.clear_draft_persist_pending(job_id)
|
||||
projected += 1
|
||||
logger.info("[roundtable-jobs] compensated draft write for job %s", job_id)
|
||||
return projected
|
||||
|
||||
async def _project_job_to_draft(self, draft_store: Any, job: dict[str, Any]) -> bool:
|
||||
"""把单个作业的最新状态投影成草稿 step2.runs 里的一个 run。成功返回 True。
|
||||
|
||||
与执行器不同:这里没有 ``params``(作业可能跑在别的 worker / 已结束),只能用作业行
|
||||
里已落库的 ``dialogues``/``step3``/``status`` 重建 run —— 这些正是「研讨结果」本体,
|
||||
足够让前端加载草稿时看到完整的后台研讨记录与报告。
|
||||
"""
|
||||
from deerflow.persistence.roundtable_drafts.sql import DraftConcurrentWriteError
|
||||
|
||||
job_id = job.get("id")
|
||||
draft_id = job.get("draft_id")
|
||||
user_id = job.get("user_id")
|
||||
run_id = f"job-{job_id}"
|
||||
status = job.get("status") or "running"
|
||||
dialogues = job.get("dialogues") or []
|
||||
step3 = job.get("step3")
|
||||
|
||||
# task 草稿不分权:get/update 不传 user_id;个人草稿按 user。
|
||||
is_task = bool(job.get("task_id"))
|
||||
|
||||
for _attempt in range(3):
|
||||
try:
|
||||
draft = (
|
||||
await draft_store.get_draft(draft_id)
|
||||
if is_task
|
||||
else await draft_store.get_draft(draft_id, user_id)
|
||||
)
|
||||
except Exception: # noqa: BLE001
|
||||
draft = None
|
||||
if draft is None:
|
||||
# 草稿已被删除 —— 无处投影,视作完成(清标记)。
|
||||
return True
|
||||
expected_version = draft.get("version")
|
||||
|
||||
step2 = dict(draft.get("step2") or {})
|
||||
runs = list(step2.get("runs") or [])
|
||||
idx = next((i for i, r in enumerate(runs) if r.get("id") == run_id), -1)
|
||||
created = runs[idx].get("createdAt") if idx >= 0 else (job.get("created_at") or datetime.now(UTC).isoformat())
|
||||
prev_step3 = runs[idx].get("step3") if idx >= 0 else None
|
||||
effective_step3 = step3 if step3 is not None else prev_step3
|
||||
|
||||
# 从 dialogues 里提取席位信息(尽量还原 selectedAgents)。
|
||||
agents_seen: list[dict[str, str]] = []
|
||||
seen_ids: set[str] = set()
|
||||
for d in dialogues:
|
||||
aid = str(d.get("agentId") or "")
|
||||
if aid and aid not in seen_ids and d.get("role") == "seat":
|
||||
seen_ids.add(aid)
|
||||
agents_seen.append({"agent_id": aid, "name": str(d.get("name") or aid)})
|
||||
|
||||
run = {
|
||||
"id": run_id,
|
||||
"title": "后台研讨",
|
||||
"createdAt": created,
|
||||
"source": job.get("orchestration_mode") or "recommend",
|
||||
"selectedAgents": agents_seen,
|
||||
"threadIds": job.get("thread_ids"),
|
||||
"coordinatorName": job.get("coordinator_name"),
|
||||
"step2RoundtableDialogues": dialogues,
|
||||
"lastLeaderContent": next(
|
||||
(d.get("content") for d in reversed(dialogues) if d.get("role") == "leader"), ""
|
||||
) or "",
|
||||
"hasConsensus": status == "done",
|
||||
"consensusPercentage": int(job.get("consensus_percentage") or (100 if status == "done" else 0)),
|
||||
"budgetLimit": 0,
|
||||
"orchestrationMode": job.get("orchestration_mode") or "recommend",
|
||||
"chain": job.get("chain"),
|
||||
"orchestrationPlan": job.get("orchestration_plan"),
|
||||
"step3": effective_step3,
|
||||
"jobId": job_id,
|
||||
"jobStatus": status,
|
||||
}
|
||||
runs = [run if i == idx else r for i, r in enumerate(runs)] if idx >= 0 else [*runs, run]
|
||||
step2["runs"] = runs
|
||||
step2["activeRunId"] = run_id
|
||||
has_report = effective_step3 is not None
|
||||
update_kwargs: dict[str, Any] = {
|
||||
"step2": step2,
|
||||
"furthest_step": 3 if (status == "done" and has_report) else 2,
|
||||
}
|
||||
if has_report:
|
||||
update_kwargs["step3"] = effective_step3
|
||||
|
||||
try:
|
||||
if is_task:
|
||||
await draft_store.update_draft(
|
||||
draft_id, expected_version=expected_version, **update_kwargs
|
||||
)
|
||||
else:
|
||||
await draft_store.update_draft(
|
||||
draft_id, user_id, expected_version=expected_version, **update_kwargs
|
||||
)
|
||||
return True
|
||||
except DraftConcurrentWriteError:
|
||||
# 并发写冲突 —— 重读最新草稿再合并重试(与执行器同款乐观锁重试)。
|
||||
continue
|
||||
except Exception: # noqa: BLE001
|
||||
logger.warning(
|
||||
"[roundtable-jobs] compensate update_draft failed for %s", draft_id, exc_info=True
|
||||
)
|
||||
return False
|
||||
# 乐观锁重试耗尽:保留 pending 标记,等下一轮补偿器再试。
|
||||
return False
|
||||
@@ -16,9 +16,12 @@ from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
import os
|
||||
import uuid as _uuid
|
||||
from collections.abc import Callable
|
||||
from contextlib import suppress
|
||||
from dataclasses import dataclass, field
|
||||
from datetime import UTC, datetime
|
||||
from datetime import UTC, datetime, timedelta
|
||||
from typing import Any
|
||||
|
||||
from deerflow.agents.roundtable_orchestrator import (
|
||||
@@ -27,10 +30,34 @@ from deerflow.agents.roundtable_orchestrator import (
|
||||
run_orchestration,
|
||||
)
|
||||
from deerflow.persistence.roundtable_diagnostics import DiagnosticsRecorder, get_default_store
|
||||
from deerflow.persistence.roundtable_drafts.sql import DraftConcurrentWriteError
|
||||
from deerflow.runtime.user_context import reset_current_user, set_current_user
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# 本 worker 进程的唯一标识(Phase 3 租约持有者)。pid 保证同机多 worker 进程互不相同,
|
||||
# 追加随机后缀避免 pid 复用导致的极端撞车。跨进程「谁在跑这个作业」由它写进 lease_owner。
|
||||
WORKER_ID = f"w-{os.getpid()}-{_uuid.uuid4().hex[:8]}"
|
||||
|
||||
# 租约 TTL 与心跳间隔(Phase 3)。心跳每 _HEARTBEAT_INTERVAL 秒续租一次,把 lease_until
|
||||
# 推到 now + _LEASE_TTL_SECONDS。只要 worker 活着,租约永远不过期、别的 worker 抢不走;
|
||||
# worker 崩溃后心跳停止,最长 _LEASE_TTL_SECONDS 后租约过期,dispatcher 在其它 worker 上
|
||||
# 重新领取(attempt+1)—— 崩溃可接管的一致性来自 DB 租约而非进程内存。
|
||||
# 约束:_LEASE_TTL_SECONDS 必须明显大于 _HEARTBEAT_INTERVAL(留足续租余量)。
|
||||
_LEASE_TTL_SECONDS = 60
|
||||
_HEARTBEAT_INTERVAL = 20.0
|
||||
|
||||
# 跨进程取消轮询间隔(秒)。cancel 端点把 ``status='cancel_requested'`` 写进 DB(权威意图),
|
||||
# 实际跑作业的 worker 上的看门狗协程定期读 DB 检测到后,收口 ``cancelled`` 终态并对本进程的
|
||||
# 编排 task 调 ``cancel()``。多 worker 下 cancel 请求可能打到非执行 worker,靠此机制最长延迟
|
||||
# 一个轮询周期即可让幽灵作业停下。
|
||||
_CANCEL_POLL_INTERVAL = 5.0
|
||||
|
||||
# 草稿乐观锁重试次数。每次尝试都带 expected_version(冲突则重读重试);重试全部耗尽后
|
||||
# **绝不退化为无版本检查的 last-write-wins 盲写**,而是给作业置 draft_persist_pending
|
||||
# 标记,由 outbox 补偿器(Phase 3)投影结果到草稿——一致性来自 DB 标记而非运气。
|
||||
_DRAFT_WRITE_RETRIES = 3
|
||||
|
||||
# 「后台挂起」的 Step3 自动产出(方案总结报告 md + 结果绘制 HTML)总开关。
|
||||
# 历史:2026-06-05 因后台 start_run 路径报告不稳定临时禁用;2026-06-12 定位到根因是
|
||||
# ``_enforce_agent_access`` 对进程内虚拟 request 的 ``request.state`` 裸访问(AttributeError
|
||||
@@ -203,6 +230,115 @@ class StartParams:
|
||||
# 深链接业务码(rwfx→6BF 等)。透传给 run_orchestration → build_summary_prompt:写实业务额外注入
|
||||
# 「业务链完整性核对」,让后台总结报告核对产出相对业务链完不完整。普通会商为空 → 不注入。
|
||||
business_code: str | None = None
|
||||
# Phase 3 租约持有者(= dispatcher 领取时写入的 worker_id)。执行器据此:① 所有进度落库
|
||||
# 带 lease_owner(update_progress 租约门,非持租者的过期写被拒);② 心跳续租;③ 看门狗
|
||||
# 检测到 cancel_requested 时以持租者身份收口 cancelled。None = 旧路径(不走租约门)。
|
||||
lease_owner: str | None = None
|
||||
|
||||
|
||||
# ── Phase 3:input_snapshot 序列化 / 反序列化 ─────────────────────────────────
|
||||
#
|
||||
# 路由 start/resume 时把整份启动入参序列化成 JSON 存进 ``input_snapshot``;dispatcher
|
||||
# 在**任意 worker** 领取 queued/租约过期作业后,据此无损重建 StartParams —— 这是「跨进程
|
||||
# 可接管 / 崩溃可恢复」的前提(不再依赖发起请求的那个 worker 的进程内存)。
|
||||
#
|
||||
# 快照 schema 版本 ``_SNAPSHOT_VERSION``:将来字段变更时升版并兼容读旧版。
|
||||
_SNAPSHOT_VERSION = 1
|
||||
|
||||
|
||||
def build_start_snapshot(
|
||||
*,
|
||||
user_id: str,
|
||||
agents: list[SeatRef],
|
||||
seed_message: str,
|
||||
model: str | None = None,
|
||||
mode: str = "recommend",
|
||||
intent_text: str = "",
|
||||
thread_ids: dict[str, str] | None = None,
|
||||
coordinator_name: str = "roundtable-coordinator",
|
||||
draft_id: str | None = None,
|
||||
task_id: str | None = None,
|
||||
is_resume: bool = False,
|
||||
chain: dict[str, Any] | None = None,
|
||||
orchestration_plan: dict[str, Any] | None = None,
|
||||
enable_report: bool = ROUNDTABLE_BACKGROUND_REPORT_ENABLED,
|
||||
seat_thinking_enabled: bool | None = None,
|
||||
seat_reasoning_effort: str | None = None,
|
||||
seat_subagent_enabled: bool = False,
|
||||
seat_skill_directives: dict[str, bool] | None = None,
|
||||
seat_modes: dict[str, str] | None = None,
|
||||
gather_first: bool = False,
|
||||
business_code: str | None = None,
|
||||
) -> dict[str, Any]:
|
||||
"""把一次启动入参序列化为可 JSON 落库的快照 dict(snake_case,对齐 StartParams)。"""
|
||||
return {
|
||||
"v": _SNAPSHOT_VERSION,
|
||||
"user_id": user_id,
|
||||
"agents": [{"agent_id": a.agent_id, "name": a.name} for a in agents],
|
||||
"seed_message": seed_message,
|
||||
"model": model,
|
||||
"mode": mode,
|
||||
"intent_text": intent_text,
|
||||
"thread_ids": thread_ids,
|
||||
"coordinator_name": coordinator_name,
|
||||
"draft_id": draft_id,
|
||||
"task_id": task_id,
|
||||
"is_resume": is_resume,
|
||||
"chain": chain,
|
||||
"orchestration_plan": orchestration_plan,
|
||||
"enable_report": enable_report,
|
||||
"seat_thinking_enabled": seat_thinking_enabled,
|
||||
"seat_reasoning_effort": seat_reasoning_effort,
|
||||
"seat_subagent_enabled": seat_subagent_enabled,
|
||||
"seat_skill_directives": dict(seat_skill_directives or {}),
|
||||
"seat_modes": dict(seat_modes or {}),
|
||||
"gather_first": bool(gather_first),
|
||||
"business_code": business_code,
|
||||
}
|
||||
|
||||
|
||||
def params_from_snapshot(
|
||||
snapshot: dict[str, Any] | None, *, job_id: str, lease_owner: str | None
|
||||
) -> StartParams | None:
|
||||
"""从 ``input_snapshot`` 无损重建 StartParams。快照缺失 / 损坏返回 None(调用方跳过)。"""
|
||||
if not snapshot or not isinstance(snapshot, dict):
|
||||
return None
|
||||
try:
|
||||
agents = [
|
||||
SeatRef(str(a.get("agent_id") or ""), str(a.get("name") or a.get("agent_id") or ""))
|
||||
for a in (snapshot.get("agents") or [])
|
||||
if isinstance(a, dict) and a.get("agent_id")
|
||||
]
|
||||
if not agents:
|
||||
return None
|
||||
return StartParams(
|
||||
job_id=job_id,
|
||||
user_id=str(snapshot.get("user_id") or ""),
|
||||
agents=agents,
|
||||
seed_message=str(snapshot.get("seed_message") or ""),
|
||||
model=snapshot.get("model"),
|
||||
mode=str(snapshot.get("mode") or "recommend"),
|
||||
intent_text=str(snapshot.get("intent_text") or ""),
|
||||
thread_ids=snapshot.get("thread_ids"),
|
||||
coordinator_name=str(snapshot.get("coordinator_name") or "roundtable-coordinator"),
|
||||
draft_id=snapshot.get("draft_id"),
|
||||
task_id=snapshot.get("task_id"),
|
||||
is_resume=bool(snapshot.get("is_resume")),
|
||||
chain=snapshot.get("chain"),
|
||||
orchestration_plan=snapshot.get("orchestration_plan"),
|
||||
enable_report=bool(snapshot.get("enable_report", ROUNDTABLE_BACKGROUND_REPORT_ENABLED)),
|
||||
seat_thinking_enabled=snapshot.get("seat_thinking_enabled"),
|
||||
seat_reasoning_effort=snapshot.get("seat_reasoning_effort"),
|
||||
seat_subagent_enabled=bool(snapshot.get("seat_subagent_enabled")),
|
||||
seat_skill_directives=dict(snapshot.get("seat_skill_directives") or {}),
|
||||
seat_modes=dict(snapshot.get("seat_modes") or {}),
|
||||
gather_first=bool(snapshot.get("gather_first")),
|
||||
business_code=snapshot.get("business_code"),
|
||||
lease_owner=lease_owner,
|
||||
)
|
||||
except Exception: # noqa: BLE001 — 快照损坏不应让 dispatcher 崩溃
|
||||
logger.exception("failed to rebuild StartParams from snapshot for job %s", job_id)
|
||||
return None
|
||||
|
||||
|
||||
# 网关工厂:给定一次启动参数,返回一个 RoundtableGateway 实例。
|
||||
@@ -259,7 +395,10 @@ class RoundtableJobExecutor:
|
||||
thread_ids, coordinator_name = await init()
|
||||
try:
|
||||
await self._store.update_progress(
|
||||
params.job_id, thread_ids=thread_ids, coordinator_name=coordinator_name
|
||||
params.job_id,
|
||||
lease_owner=params.lease_owner,
|
||||
thread_ids=thread_ids,
|
||||
coordinator_name=coordinator_name,
|
||||
)
|
||||
except Exception: # noqa: BLE001
|
||||
logger.warning("persist thread_ids for job %s failed", params.job_id, exc_info=True)
|
||||
@@ -277,22 +416,52 @@ class RoundtableJobExecutor:
|
||||
agents=params.agents,
|
||||
on_dialogue=_on_dialogue if (self._draft_store_for(params) and params.draft_id) else None,
|
||||
recorder=diag,
|
||||
# Phase 3:进度落库带租约持有者 —— update_progress 租约门只放行持租者的写,
|
||||
# 非持租者(被接管/已取消)的过期进度写被 DB 条件 UPDATE 拒绝。
|
||||
lease_owner=params.lease_owner,
|
||||
)
|
||||
result = await run_orchestration(
|
||||
gateway=gateway,
|
||||
progress=progress,
|
||||
agents=params.agents,
|
||||
thread_ids=thread_ids,
|
||||
coordinator_name=coordinator_name,
|
||||
model=params.model,
|
||||
seed_message=params.seed_message,
|
||||
mode=params.mode,
|
||||
intent_text=params.intent_text,
|
||||
enable_report=params.enable_report,
|
||||
plan=params.orchestration_plan,
|
||||
gather_first=params.gather_first,
|
||||
business_code=params.business_code,
|
||||
# Phase 3 心跳续租:活着就持续把 lease_until 推后,保证本 worker 的租约不过期、
|
||||
# 别的 worker 抢不走;本协程随编排 task 生死(finally 里 cancel)。崩溃则心跳停,
|
||||
# 租约最长 _LEASE_TTL_SECONDS 后过期 → dispatcher 在其它 worker 接管。
|
||||
main_task = asyncio.current_task()
|
||||
heartbeat = (
|
||||
asyncio.create_task(self._heartbeat(params.job_id, params.lease_owner, main_task))
|
||||
if params.lease_owner
|
||||
else None
|
||||
)
|
||||
# 跨进程取消看门狗:run_orchestration 是一个不透明长 await(多轮 LLM),
|
||||
# 无法在其内部插检查点。改为起一个并行的看门狗协程,定期读 DB 的 job status;
|
||||
# cancel 端点把 status=cancel_requested 写进 DB(权威意图),看门狗检测到后以**持租者**
|
||||
# 身份收口 cancelled 终态(清唯一活跃键 + 租约)并对本编排 task 调 cancel() →
|
||||
# CancelledError 在 run_orchestration 的下一个 await 点抛出并上抛。
|
||||
# 这让 cancel 在多 worker 下跨进程生效——cancel 打到非执行 worker 也无妨。
|
||||
watcher = asyncio.create_task(
|
||||
self._watch_cancellation(params.job_id, main_task, params.lease_owner)
|
||||
)
|
||||
try:
|
||||
result = await run_orchestration(
|
||||
gateway=gateway,
|
||||
progress=progress,
|
||||
agents=params.agents,
|
||||
thread_ids=thread_ids,
|
||||
coordinator_name=coordinator_name,
|
||||
model=params.model,
|
||||
seed_message=params.seed_message,
|
||||
mode=params.mode,
|
||||
intent_text=params.intent_text,
|
||||
enable_report=params.enable_report,
|
||||
plan=params.orchestration_plan,
|
||||
gather_first=params.gather_first,
|
||||
business_code=params.business_code,
|
||||
)
|
||||
finally:
|
||||
watcher.cancel()
|
||||
with suppress(asyncio.CancelledError, Exception):
|
||||
await watcher
|
||||
if heartbeat is not None:
|
||||
heartbeat.cancel()
|
||||
with suppress(asyncio.CancelledError, Exception):
|
||||
await heartbeat
|
||||
# 终态:把最终对话 + step3 + jobStatus 落进同一个 run(含 furthest_step=3)。
|
||||
await self._write_run_to_draft(
|
||||
params,
|
||||
@@ -302,16 +471,18 @@ class RoundtableJobExecutor:
|
||||
step3=result.step3,
|
||||
thread_ids=thread_ids,
|
||||
)
|
||||
# step3 也挂 job(API / 弹窗用)。
|
||||
# step3 也挂 job(API / 弹窗用)。带租约门:仅持租者可写。
|
||||
if result.status == "done" and result.step3:
|
||||
try:
|
||||
await self._store.update_progress(params.job_id, step3=result.step3)
|
||||
await self._store.update_progress(
|
||||
params.job_id, lease_owner=params.lease_owner, step3=result.step3
|
||||
)
|
||||
except Exception: # noqa: BLE001
|
||||
logger.warning("persist step3 to job %s failed", params.job_id, exc_info=True)
|
||||
except asyncio.CancelledError:
|
||||
# 干净停止即可,**不在被取消的任务里写 DB**:取消态下二次 await 会再抛
|
||||
# CancelledError、并可能打断进行中的 sqlite 连接。``cancelled`` 终态由
|
||||
# 路由的 cancel 端点(executor.cancel + store.update_progress)权威写入。
|
||||
# 取消看门狗(或租约过期后的 dispatcher 回收)权威写入。
|
||||
logger.info("roundtable job %s cancelled", params.job_id)
|
||||
raise
|
||||
except Exception as exc: # noqa: BLE001 — 顶层兜底:run_orchestration 内部已尽量标 error
|
||||
@@ -324,6 +495,97 @@ class RoundtableJobExecutor:
|
||||
finally:
|
||||
reset_current_user(token)
|
||||
|
||||
async def _heartbeat(
|
||||
self, job_id: str, lease_owner: str, main_task: asyncio.Task
|
||||
) -> None:
|
||||
"""定期续租:把 ``lease_until`` 推到 now + TTL,声明「本 worker 仍在跑、仍持租」。
|
||||
|
||||
只要主任务活着且续租成功,别的 worker 的 dispatcher 看到租约未过期就不会抢;主任务
|
||||
结束(或本协程被 cancel)后不再续租,租约自然到期。续租失败(租约已被接管 / 状态已变)
|
||||
说明本 worker 已不是持租者,必须立即停止本地编排,避免过期 worker 继续消耗模型资源;
|
||||
其任何迟到进度写也会被租约门拒绝。
|
||||
"""
|
||||
try:
|
||||
while not main_task.done():
|
||||
await asyncio.sleep(_HEARTBEAT_INTERVAL)
|
||||
if main_task.done():
|
||||
return
|
||||
try:
|
||||
ok = await self._store.renew_lease(
|
||||
job_id,
|
||||
lease_owner=lease_owner,
|
||||
lease_until=datetime.now(UTC) + timedelta(seconds=_LEASE_TTL_SECONDS),
|
||||
)
|
||||
except Exception: # noqa: BLE001 — DB 抖动不中断心跳,下轮重试
|
||||
logger.warning("roundtable job %s renew_lease failed", job_id, exc_info=True)
|
||||
continue
|
||||
if not ok:
|
||||
logger.info(
|
||||
"roundtable job %s: renew_lease rejected (lease lost or status changed)",
|
||||
job_id,
|
||||
)
|
||||
if not main_task.done():
|
||||
main_task.cancel()
|
||||
return
|
||||
except asyncio.CancelledError:
|
||||
# 编排结束 → _run 的 finally cancel 了心跳 → 安静退出。
|
||||
return
|
||||
|
||||
async def _watch_cancellation(
|
||||
self, job_id: str, main_task: asyncio.Task, lease_owner: str | None
|
||||
) -> None:
|
||||
"""定期轮询 DB 检测跨进程取消标志(``cancel_requested``),并以持租者身份收口。
|
||||
|
||||
多 worker 下 cancel 请求可能打到**非执行** worker,该 worker 的 ``cancel()`` 在
|
||||
``_tasks`` 里找不到此 job(返回 False)。但 cancel 端点始终把 ``status='cancel_requested'``
|
||||
写进 DB(权威意图)。本看门狗跑在**实际执行作业的 worker** 上,检测到后:
|
||||
|
||||
1. 以**持租者**身份 ``finalize_cancel``——条件 UPDATE 把 cancel_requested 收口为终态
|
||||
``cancelled``,同时清唯一活跃键 + 租约(一致性来自 DB,绝不双收口);
|
||||
2. 对本地编排 task 调 ``cancel()``,让 ``run_orchestration`` 在下一个 await 点抛
|
||||
``CancelledError`` 干净退出。
|
||||
|
||||
DB 读失败时静默重试(不中断看门狗)。兼容旧数据:若 DB 里已是终态 ``cancelled``
|
||||
(旧版 cancel 端点直写),直接取消本地 task。
|
||||
"""
|
||||
try:
|
||||
while not main_task.done():
|
||||
await asyncio.sleep(_CANCEL_POLL_INTERVAL)
|
||||
if main_task.done():
|
||||
return
|
||||
try:
|
||||
# user_id=None → 不按 user 过滤,纯读 status(看门狗不关心归属)。
|
||||
row = await self._store.get(job_id, user_id=None)
|
||||
except Exception: # noqa: BLE001 — DB 读失败不中断看门狗,下轮重试
|
||||
continue
|
||||
status = (row.get("status") or "") if row is not None else ""
|
||||
if status == "cancel_requested":
|
||||
logger.info(
|
||||
"roundtable job %s: detected cancel_requested in DB, finalizing "
|
||||
"cancelled and cancelling orchestration task",
|
||||
job_id,
|
||||
)
|
||||
# 以持租者身份收口;非持租者(理论上不该发生)则退化为无租约条件收口。
|
||||
try:
|
||||
await self._store.finalize_cancel(job_id, lease_owner=lease_owner)
|
||||
except Exception: # noqa: BLE001 — 收口失败不阻断本地取消
|
||||
logger.warning("roundtable job %s finalize_cancel failed", job_id, exc_info=True)
|
||||
if not main_task.done():
|
||||
main_task.cancel()
|
||||
return
|
||||
if status == "cancelled":
|
||||
# 兼容:旧路径已直接把 cancelled 写进 DB —— 本地 task 照样停下。
|
||||
logger.info(
|
||||
"roundtable job %s: detected cancelled in DB, cancelling orchestration task",
|
||||
job_id,
|
||||
)
|
||||
if not main_task.done():
|
||||
main_task.cancel()
|
||||
return
|
||||
except asyncio.CancelledError:
|
||||
# 编排正常结束 → _run 的 finally cancel 了看门狗 → 安静退出。
|
||||
return
|
||||
|
||||
async def _write_run_to_draft(
|
||||
self,
|
||||
params: StartParams,
|
||||
@@ -338,77 +600,106 @@ class RoundtableJobExecutor:
|
||||
|
||||
读现有 step2 → 按 run_id=``job-<jobId>`` upsert(保留其它手动 run)→ 设为 activeRun
|
||||
→ update_draft。前端加载草稿即可像手动跑一样 hydrate 出对话。
|
||||
|
||||
带乐观锁重试:**每次**读-改-写都带上 ``expected_version``,若并发写导致版本冲突
|
||||
(``DraftConcurrentWriteError``)则重新读取最新草稿、重新合并 run 再试。重试全部
|
||||
耗尽后**绝不**退化为无版本检查的 last-write-wins 盲写,而是给作业置
|
||||
``draft_persist_pending`` 标记(由 Phase 3 outbox 补偿器投影结果到草稿)——
|
||||
消除后台作业与前台 PUT 并发时的 step2.runs 丢失更新,且一致性来自 DB 标记而非运气。
|
||||
"""
|
||||
draft_store = self._draft_store_for(params)
|
||||
if draft_store is None or not params.draft_id:
|
||||
return
|
||||
# 这些只依赖 params/dialogues,重试间不变——提前算好。
|
||||
idx_by_agent = {a.agent_id: i for i, a in enumerate(params.agents)}
|
||||
s2_dialogues = [_to_step2_dialogue(d, idx_by_agent) for d in dialogues]
|
||||
last_leader = next(
|
||||
(d.get("content") for d in reversed(dialogues) if d.get("role") == "leader"), ""
|
||||
)
|
||||
run_id = f"job-{params.job_id}"
|
||||
try:
|
||||
draft = await draft_store.get_draft(params.draft_id, params.user_id)
|
||||
except Exception: # noqa: BLE001
|
||||
draft = None
|
||||
step2 = dict((draft or {}).get("step2") or {})
|
||||
runs = list(step2.get("runs") or [])
|
||||
idx = next((i for i, r in enumerate(runs) if r.get("id") == run_id), -1)
|
||||
created = runs[idx].get("createdAt") if idx >= 0 else datetime.now(UTC).isoformat()
|
||||
prev_step3 = runs[idx].get("step3") if idx >= 0 else None
|
||||
# 本次有效 step3:新报告优先,否则保留上次(running 中间态 step3=None 不抹掉已存的)。
|
||||
effective_step3 = step3 if step3 is not None else prev_step3
|
||||
run = {
|
||||
"id": run_id,
|
||||
"title": "后台研讨",
|
||||
"createdAt": created,
|
||||
"source": params.mode,
|
||||
"selectedAgents": [{"agent_id": a.agent_id, "name": a.name} for a in params.agents],
|
||||
# 后台真建的 leader/seat 线程是真实持久的——存进去,前端加载后能像手动跑一样
|
||||
# 按 thread 打开各席位产物(HTTP outputs 兜底)。hydrate 是纯展示、绝不 resume,
|
||||
# 不会触发重跑(init 闸门 + isRoundTableRunning=false 双重拦截)。空则存 None。
|
||||
"threadIds": (thread_ids or None),
|
||||
"coordinatorName": params.coordinator_name,
|
||||
"step2RoundtableDialogues": s2_dialogues,
|
||||
"lastLeaderContent": last_leader or "",
|
||||
"hasConsensus": status == "done",
|
||||
"consensusPercentage": int(consensus),
|
||||
"budgetLimit": 0,
|
||||
# 从各席位 write_file 调用重建 stub,使加载草稿后点开产物的虚拟预览可用。
|
||||
"seatStubMessages": _seat_stub_messages(dialogues),
|
||||
"orchestrationMode": params.mode,
|
||||
"chain": params.chain,
|
||||
# dag 模式分层计划:让前端加载草稿后能像手动跑一样按 stage 画流程图。
|
||||
"orchestrationPlan": params.orchestration_plan,
|
||||
"step3": effective_step3,
|
||||
"jobId": params.job_id,
|
||||
"jobStatus": status,
|
||||
}
|
||||
runs = [run if i == idx else r for i, r in enumerate(runs)] if idx >= 0 else [*runs, run]
|
||||
step2["runs"] = runs
|
||||
step2["activeRunId"] = run_id
|
||||
# 顶层 draft.step3 也写一份(同 Step2 把对话写进 run 的同一次落库一并带上)。
|
||||
# 前端 hydrateStep3 用**顶层** draft.step3 重建报告气泡(结果绘制的聊天记录 + HTML 预览);
|
||||
# 只存 run 内 step3 时,加载会被 hydrateStep3(draft.step3=None) 覆盖成空 → 报告聊天记录丢失。
|
||||
# furthest_step 跟随**是否真有 step3**:done 且有报告→3(落到结果绘制);
|
||||
# 否则停在 2(Step2 研讨)。后台禁用报告时 step3=None → 停在 Step2,用户可在前端手动画报告。
|
||||
has_report = effective_step3 is not None
|
||||
update_kwargs: dict[str, Any] = {"step2": step2, "furthest_step": 3 if (status == "done" and has_report) else 2}
|
||||
if has_report:
|
||||
update_kwargs["step3"] = effective_step3
|
||||
try:
|
||||
await draft_store.update_draft(params.draft_id, params.user_id, **update_kwargs)
|
||||
except Exception as exc: # noqa: BLE001 — 写草稿失败不影响作业本身
|
||||
logger.warning("write run to draft %s failed", params.draft_id, exc_info=True)
|
||||
from deerflow.persistence.roundtable_diagnostics import record_diagnostic
|
||||
|
||||
await record_diagnostic(
|
||||
scope="background", stage="job", level="error", event="draft_write_failed",
|
||||
message=f"研讨结果写回草稿失败(status={status}):{exc}",
|
||||
detail={"type": type(exc).__name__, "error": str(exc)},
|
||||
job_id=params.job_id, draft_id=params.draft_id, task_id=params.task_id, user_id=params.user_id,
|
||||
)
|
||||
for attempt in range(_DRAFT_WRITE_RETRIES):
|
||||
# 读最新草稿(冲突重试时重新读,拿到别的小伙伴刚写的 runs)。
|
||||
try:
|
||||
draft = await draft_store.get_draft(params.draft_id, params.user_id)
|
||||
except Exception: # noqa: BLE001
|
||||
draft = None
|
||||
expected_version = (draft or {}).get("version")
|
||||
|
||||
step2 = dict((draft or {}).get("step2") or {})
|
||||
runs = list(step2.get("runs") or [])
|
||||
idx = next((i for i, r in enumerate(runs) if r.get("id") == run_id), -1)
|
||||
created = runs[idx].get("createdAt") if idx >= 0 else datetime.now(UTC).isoformat()
|
||||
prev_step3 = runs[idx].get("step3") if idx >= 0 else None
|
||||
# 本次有效 step3:新报告优先,否则保留上次(running 中间态 step3=None 不抹掉已存的)。
|
||||
effective_step3 = step3 if step3 is not None else prev_step3
|
||||
run = {
|
||||
"id": run_id,
|
||||
"title": "后台研讨",
|
||||
"createdAt": created,
|
||||
"source": params.mode,
|
||||
"selectedAgents": [{"agent_id": a.agent_id, "name": a.name} for a in params.agents],
|
||||
"threadIds": (thread_ids or None),
|
||||
"coordinatorName": params.coordinator_name,
|
||||
"step2RoundtableDialogues": s2_dialogues,
|
||||
"lastLeaderContent": last_leader or "",
|
||||
"hasConsensus": status == "done",
|
||||
"consensusPercentage": int(consensus),
|
||||
"budgetLimit": 0,
|
||||
"seatStubMessages": _seat_stub_messages(dialogues),
|
||||
"orchestrationMode": params.mode,
|
||||
"chain": params.chain,
|
||||
"orchestrationPlan": params.orchestration_plan,
|
||||
"step3": effective_step3,
|
||||
"jobId": params.job_id,
|
||||
"jobStatus": status,
|
||||
}
|
||||
runs = [run if i == idx else r for i, r in enumerate(runs)] if idx >= 0 else [*runs, run]
|
||||
step2["runs"] = runs
|
||||
step2["activeRunId"] = run_id
|
||||
has_report = effective_step3 is not None
|
||||
update_kwargs: dict[str, Any] = {
|
||||
"step2": step2,
|
||||
"furthest_step": 3 if (status == "done" and has_report) else 2,
|
||||
}
|
||||
if has_report:
|
||||
update_kwargs["step3"] = effective_step3
|
||||
|
||||
# 每次尝试都带 expected_version(乐观锁);绝不降级为无版本检查的盲写。
|
||||
try:
|
||||
await draft_store.update_draft(
|
||||
params.draft_id, params.user_id,
|
||||
expected_version=expected_version,
|
||||
**update_kwargs,
|
||||
)
|
||||
return # 写入成功
|
||||
except DraftConcurrentWriteError:
|
||||
if attempt < _DRAFT_WRITE_RETRIES - 1:
|
||||
logger.debug(
|
||||
"roundtable draft %s version conflict (attempt %d/%d), retrying",
|
||||
params.draft_id, attempt + 1, _DRAFT_WRITE_RETRIES,
|
||||
)
|
||||
continue
|
||||
# 重试全部耗尽:不盲写,置 pending 标记交由 outbox 补偿器投影。
|
||||
logger.warning(
|
||||
"roundtable draft %s: version conflicts exhausted after %d attempts, "
|
||||
"marking job %s draft_persist_pending",
|
||||
params.draft_id, _DRAFT_WRITE_RETRIES, params.job_id,
|
||||
)
|
||||
with suppress(Exception):
|
||||
await self._store.mark_draft_persist_pending(params.job_id)
|
||||
return
|
||||
except Exception as exc: # noqa: BLE001 — 写草稿失败不影响作业本身
|
||||
logger.warning("write run to draft %s failed", params.draft_id, exc_info=True)
|
||||
from deerflow.persistence.roundtable_diagnostics import record_diagnostic
|
||||
|
||||
await record_diagnostic(
|
||||
scope="background", stage="job", level="error", event="draft_write_failed",
|
||||
message=f"研讨结果写回草稿失败(status={status}):{exc}",
|
||||
detail={"type": type(exc).__name__, "error": str(exc)},
|
||||
job_id=params.job_id, draft_id=params.draft_id, task_id=params.task_id, user_id=params.user_id,
|
||||
)
|
||||
return
|
||||
|
||||
def cancel(self, job_id: str) -> bool:
|
||||
"""请求取消一个在跑的作业。返回是否命中在跑任务。"""
|
||||
|
||||
@@ -52,10 +52,14 @@ SSE 协议(与前端 ``streamIntent`` 对齐)
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import hashlib
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import re
|
||||
import uuid
|
||||
from collections.abc import AsyncIterator
|
||||
from datetime import UTC, datetime, timedelta
|
||||
from typing import Any
|
||||
|
||||
import httpx
|
||||
@@ -89,6 +93,37 @@ from deerflow.tools.builtins.clarification_utils import resolve_allow_multiple
|
||||
logger = logging.getLogger(__name__)
|
||||
router = APIRouter(prefix="/api/intent", tags=["intent"])
|
||||
|
||||
# ── Phase 4:意图回合跨 worker 单飞 + 幂等 ─────────────────────────────────────
|
||||
# 进程内 RunManager 是每 worker 内存态,多 worker 下同 thread 的两个请求落到不同
|
||||
# worker 会各启一个 run(无 409)。单飞只能靠 DB:intent_turns 表对 (thread_id,
|
||||
# client_turn_id) 的唯一键 + 条件 UPDATE 抢占。前端每回合生成稳定 client_turn_id,
|
||||
# 重试 / 双击复用,杜绝「同一回合被启两个 run → 双发意图 / 重复澄清卡」。
|
||||
_WORKER_ID = f"w-{os.getpid()}-{uuid.uuid4().hex[:8]}"
|
||||
_INTENT_TURN_LEASE_TTL_SECONDS = 120
|
||||
|
||||
|
||||
def _intent_message_hash(message: str, files: list, task_context: dict | None) -> str:
|
||||
"""回合内容指纹:message + files + task_context 的 sha256。
|
||||
|
||||
检测「同一 client_turn_id 但内容变了」的客户端 bug(→ conflict)。文件按
|
||||
(filename, path) 归一,避免临时字段(size/status)造成假性不一致。
|
||||
"""
|
||||
file_repr = sorted(
|
||||
(str(f.get("filename") or ""), str(f.get("path") or "")) for f in (files or [])
|
||||
)
|
||||
blob = json.dumps(
|
||||
{"message": message, "files": file_repr, "task_context": task_context or {}},
|
||||
ensure_ascii=False,
|
||||
sort_keys=True,
|
||||
)
|
||||
return hashlib.sha256(blob.encode("utf-8")).hexdigest()
|
||||
|
||||
|
||||
def _get_intent_turn_store(request: Request):
|
||||
"""意图回合单飞 store(未配置 DB 时为 None → 跳过单飞,走原流程)。"""
|
||||
return getattr(request.app.state, "intent_turn_store", None)
|
||||
|
||||
|
||||
# 内置 agent 目录名,须与 ``.deer-flow/agents/roundtable-intent/`` 一致。
|
||||
INTENT_AGENT_ID = "roundtable-intent"
|
||||
_SUPPORTED_INTENT_AGENT_IDS = frozenset({INTENT_AGENT_ID, POSITION_INTENT_AGENT_ID})
|
||||
@@ -485,6 +520,60 @@ async def stream_intent(request: Request, payload: dict = Body(...)) -> Streamin
|
||||
# 因此既无须 trust_env=False 防代理,也不存在 loopback 40s 超时这种坑。
|
||||
async with _make_loopback_client(request) as client:
|
||||
try:
|
||||
# —— Phase 4:意图回合跨 worker 单飞 + 幂等 ——
|
||||
# 同一 (thread_id, client_turn_id) 至多一个 run:重复 / 双击 / 网络重发
|
||||
# 命中既有行即幂等返回,绝不重复启 run(杜绝双发意图 / 重复澄清卡)。
|
||||
turn_store = _get_intent_turn_store(request)
|
||||
client_turn_id = str(payload.get("client_turn_id") or "").strip() or uuid.uuid4().hex
|
||||
msg_hash = _intent_message_hash(message, files, task_context)
|
||||
claim_action = "run" # 无 store 时走原流程
|
||||
if turn_store is not None:
|
||||
claim = await turn_store.try_claim(
|
||||
thread_id=thread_id,
|
||||
client_turn_id=client_turn_id,
|
||||
message_hash=msg_hash,
|
||||
lease_owner=_WORKER_ID,
|
||||
lease_until=datetime.now(UTC)
|
||||
+ timedelta(seconds=_INTENT_TURN_LEASE_TTL_SECONDS),
|
||||
)
|
||||
claim_action = claim.get("action") or "run"
|
||||
if claim_action == "replay":
|
||||
cached = (claim.get("row") or {}).get("result_json")
|
||||
if isinstance(cached, dict):
|
||||
yield f"data: {json.dumps(cached, ensure_ascii=False)}\n\n"
|
||||
return
|
||||
# 缓存缺失/损坏 → 降级重跑(极少见)。
|
||||
claim_action = "run"
|
||||
elif claim_action == "duplicate":
|
||||
# 既有回合仍在跑且租约有效:本请求是重复 in-flight,丢弃
|
||||
# (原请求正在流式吐结果)。前端按 duplicate 标记忽略。
|
||||
dup = {"status": "asking", "content": "", "summary": None, "duplicate": True}
|
||||
yield f"data: {json.dumps(dup, ensure_ascii=False)}\n\n"
|
||||
return
|
||||
elif claim_action == "conflict":
|
||||
err_conflict = {
|
||||
"status": "error",
|
||||
"content": "意图回合幂等键冲突:同一 client_turn_id 但消息内容不同。",
|
||||
"summary": None,
|
||||
}
|
||||
yield f"data: {json.dumps(err_conflict, ensure_ascii=False)}\n\n"
|
||||
return
|
||||
|
||||
async def _record_terminal(frame: dict[str, Any]) -> None:
|
||||
"""终态帧落库(仅本 worker 赢得领取时),供后续重复请求幂等回放。"""
|
||||
if turn_store is None or claim_action != "run":
|
||||
return
|
||||
try:
|
||||
await turn_store.record_result(
|
||||
thread_id=thread_id,
|
||||
client_turn_id=client_turn_id,
|
||||
lease_owner=_WORKER_ID,
|
||||
status="error" if frame.get("status") == "error" else "done",
|
||||
result_json=frame,
|
||||
)
|
||||
except Exception:
|
||||
logger.warning("intent_turn record_result failed", exc_info=True)
|
||||
|
||||
# —— 澄清轮次硬上限兜底 ——
|
||||
# 通用会商保持原来的 3 次 ask 工具调用上限;岗位会商允许一个
|
||||
# AI turn 同时产生多张独立协助卡,所以必须按 AI 消息(一次
|
||||
@@ -657,6 +746,7 @@ async def stream_intent(request: Request, payload: dict = Body(...)) -> Streamin
|
||||
"content": ready_text,
|
||||
"summary": summary,
|
||||
}
|
||||
await _record_terminal(final_frame)
|
||||
yield f"data: {json.dumps(final_frame, ensure_ascii=False)}\n\n"
|
||||
return
|
||||
|
||||
@@ -710,6 +800,7 @@ async def stream_intent(request: Request, payload: dict = Body(...)) -> Streamin
|
||||
"content": fallback_msg,
|
||||
"summary": None,
|
||||
}
|
||||
await _record_terminal(final_frame)
|
||||
yield f"data: {json.dumps(final_frame, ensure_ascii=False)}\n\n"
|
||||
return
|
||||
|
||||
@@ -724,6 +815,7 @@ async def stream_intent(request: Request, payload: dict = Body(...)) -> Streamin
|
||||
"clarifications": clarification_payloads,
|
||||
"summary": None,
|
||||
}
|
||||
await _record_terminal(final_frame)
|
||||
yield f"data: {json.dumps(final_frame, ensure_ascii=False)}\n\n"
|
||||
return
|
||||
|
||||
@@ -733,6 +825,7 @@ async def stream_intent(request: Request, payload: dict = Body(...)) -> Streamin
|
||||
"content": content,
|
||||
"summary": None,
|
||||
}
|
||||
await _record_terminal(final_frame)
|
||||
yield f"data: {json.dumps(final_frame, ensure_ascii=False)}\n\n"
|
||||
|
||||
except HTTPException as exc:
|
||||
@@ -766,6 +859,7 @@ async def stream_intent(request: Request, payload: dict = Body(...)) -> Streamin
|
||||
"summary": None,
|
||||
"error_detail": error_detail,
|
||||
}
|
||||
await _record_terminal(err)
|
||||
yield f"data: {json.dumps(err, ensure_ascii=False)}\n\n"
|
||||
except Exception as exc:
|
||||
diagnostic = _diagnose_exception(exc, context="intent_stream")
|
||||
@@ -781,6 +875,7 @@ async def stream_intent(request: Request, payload: dict = Body(...)) -> Streamin
|
||||
"summary": None,
|
||||
"error_detail": diagnostic,
|
||||
}
|
||||
await _record_terminal(err)
|
||||
yield f"data: {json.dumps(err, ensure_ascii=False)}\n\n"
|
||||
|
||||
return StreamingResponse(
|
||||
|
||||
@@ -1116,9 +1116,16 @@ async def init_structure_thread(request: Request) -> dict[str, str]:
|
||||
|
||||
# 回环 /runs/stream 在**响应头阶段**返回这些状态码时,视为「单 worker + 单 sqlite 锁被
|
||||
# 并发打满」造成的瞬时失败,退避重试(详见 _stream_upstream 内注释):
|
||||
# 409 同 thread 抢占竞态;429 限流;500/503/504 高并发下 start_run 里 DB/checkpoint 写抖动。
|
||||
# 429 限流;500/503/504 高并发下 start_run 里 DB/checkpoint 写抖动。
|
||||
# 不含 400/401/403/404/422 等确定性错误——那些重试也没用,立即上报。
|
||||
_UPSTREAM_RETRYABLE_STATUSES = frozenset({409, 429, 500, 502, 503, 504})
|
||||
#
|
||||
# ⚠️ 409 **不在**重试集合里。409 = 同 thread 已有活跃 run(``multitask_strategy=reject``
|
||||
# 拒绝)。此时重发**不是**幂等的:LangGraph 会把同一条 human message 再追加一次 → 用户
|
||||
# 这一轮被处理两遍(重复澄清卡 / 双发意图),或两次退避后仍 502 —— 这正是用户报的
|
||||
# 「确定意图接口报错」。故 409 必须透传:由上层(intent_turns 去重 / 前端按 client_turn_id
|
||||
# 等待)决定是等待还是放弃,而不是盲目重发。其余(rollback)调用方本就靠抢占取消在跑 run,
|
||||
# 几乎不触 409,移除亦无碍。
|
||||
_UPSTREAM_RETRYABLE_STATUSES = frozenset({429, 500, 502, 503, 504})
|
||||
# 重试次数(不含首次)。总尝试 = 1 + _UPSTREAM_MAX_RETRIES。
|
||||
_UPSTREAM_MAX_RETRIES = 2
|
||||
# 指数退避基数(秒):0.4 → 0.8 → …,足够让在跑的席位 run 让出 sqlite 锁,又不至于明显拖慢。
|
||||
|
||||
@@ -14,11 +14,28 @@ import asyncio
|
||||
from datetime import UTC, datetime
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
from uuid import uuid4
|
||||
|
||||
from fastapi import APIRouter, HTTPException, Query, Request
|
||||
from pydantic import BaseModel, Field, field_validator
|
||||
|
||||
from app.gateway.path_utils import aresolve_thread_virtual_path
|
||||
from deerflow.persistence.position_roundtable.delivery import (
|
||||
artifact_signature as _artifact_signature,
|
||||
)
|
||||
from deerflow.persistence.position_roundtable.delivery import (
|
||||
is_intent_position_node as _is_intent_position_by_id,
|
||||
)
|
||||
from deerflow.persistence.position_roundtable.delivery import (
|
||||
is_json_artifact as _is_json_artifact,
|
||||
)
|
||||
from deerflow.persistence.position_roundtable.delivery import (
|
||||
is_markdown_artifact as _is_markdown_artifact,
|
||||
)
|
||||
from deerflow.persistence.position_roundtable.delivery import (
|
||||
session_intent_position_id as _chain_intent_position_id,
|
||||
)
|
||||
from deerflow.persistence.position_roundtable.sql import SessionWriteConflictError
|
||||
from deerflow.runtime.user_context import get_effective_user_id
|
||||
|
||||
# The application mounts this router below both `/api` (canonical) and the
|
||||
@@ -69,6 +86,8 @@ class NodeResponse(BaseModel):
|
||||
artifact_manifest: list[dict[str, Any]] = Field(default_factory=list)
|
||||
conversation_snapshot: dict[str, Any] | None = None
|
||||
revision: int = 0
|
||||
# 节点乐观锁版本号(任意字段变更自增;revision 仅计真实交付)。
|
||||
version: int = 0
|
||||
upstream_revision: dict[str, Any] | None = None
|
||||
rejection_count: int = 0
|
||||
last_rejection: dict[str, Any] | None = None
|
||||
@@ -96,6 +115,8 @@ class SessionResponse(BaseModel):
|
||||
action_plan_snapshot: dict[str, Any] | None = None
|
||||
action_plan_conversation: dict[str, Any] | None = None
|
||||
status: str
|
||||
# Session 级乐观锁版本号:每次命令式写操作自增,前端可回传做 CAS。
|
||||
version: int = 0
|
||||
created_at: str | None = None
|
||||
updated_at: str | None = None
|
||||
|
||||
@@ -132,20 +153,26 @@ class ConversationStateUpdateRequest(BaseModel):
|
||||
|
||||
thread_id: str | None = Field(default=None, max_length=128)
|
||||
conversation_snapshot: dict[str, Any] = Field(default_factory=dict)
|
||||
# Client-generated idempotency key; one click/retry reuses the same id so
|
||||
# the command executes exactly once. Absent → server generates one (legacy).
|
||||
command_id: str | None = Field(default=None, max_length=64)
|
||||
|
||||
|
||||
class ActivateRequest(BaseModel):
|
||||
chain_id: str = Field(min_length=1, max_length=64)
|
||||
command_id: str | None = Field(default=None, max_length=64)
|
||||
|
||||
|
||||
class NodeThreadRequest(BaseModel):
|
||||
thread_id: str = Field(min_length=1, max_length=128)
|
||||
command_id: str | None = Field(default=None, max_length=64)
|
||||
|
||||
|
||||
class CompleteTurnRequest(BaseModel):
|
||||
latest_answer: str = Field(default="", max_length=500_000)
|
||||
artifact_manifest: list[dict[str, Any]] = Field(default_factory=list)
|
||||
upstream_revision: dict[str, Any] | None = None
|
||||
command_id: str | None = Field(default=None, max_length=64)
|
||||
|
||||
|
||||
class RejectNodeRequest(BaseModel):
|
||||
@@ -154,6 +181,7 @@ class RejectNodeRequest(BaseModel):
|
||||
# default so neither field turns a valid rejection into a 422/400.
|
||||
title: str = Field(default="产物驳回", max_length=120)
|
||||
reason: str = Field(default="", max_length=5_000)
|
||||
command_id: str | None = Field(default=None, max_length=64)
|
||||
|
||||
|
||||
class ActionPlanDictionaryOption(BaseModel):
|
||||
@@ -261,6 +289,38 @@ async def _require_writable_session(
|
||||
return store, user_id, session
|
||||
|
||||
|
||||
def _command_id_or_new(command_id: str | None) -> str:
|
||||
"""Use the client's idempotency key, or mint one for legacy clients."""
|
||||
value = (command_id or "").strip()
|
||||
return value or uuid4().hex
|
||||
|
||||
|
||||
def _command_result_or_raise(result: dict[str, Any]) -> dict[str, Any]:
|
||||
"""Return an ok command result, or translate a rejected command into HTTP."""
|
||||
if result.get("ok"):
|
||||
return result
|
||||
raise HTTPException(
|
||||
status_code=int(result.get("http_status") or 409),
|
||||
detail=result.get("detail") or "命令执行被拒绝",
|
||||
)
|
||||
|
||||
|
||||
async def _update_session_or_raise(
|
||||
store: Any,
|
||||
session_id: str,
|
||||
user_id: str,
|
||||
**changes: Any,
|
||||
) -> dict[str, Any]:
|
||||
"""Apply a locked session snapshot write and map stale-state races to 409."""
|
||||
try:
|
||||
row = await store.update_session(session_id, user_id, **changes)
|
||||
except SessionWriteConflictError as exc:
|
||||
raise HTTPException(status_code=409, detail=exc.detail) from exc
|
||||
if row is None:
|
||||
raise HTTPException(status_code=404, detail="Position roundtable session not found")
|
||||
return row
|
||||
|
||||
|
||||
def _project_session(row: dict[str, Any]) -> SessionResponse:
|
||||
return SessionResponse(**row)
|
||||
|
||||
@@ -400,18 +460,15 @@ def _is_intent_position_node(
|
||||
node: dict[str, Any],
|
||||
intent_position_id: str | None = None,
|
||||
) -> bool:
|
||||
position_id = str(node.get("position_id") or "").strip()
|
||||
if intent_position_id:
|
||||
return position_id == intent_position_id
|
||||
# Existing persisted sessions predate the configurable directory. Retain
|
||||
# their historical interpretation until they are archived/recreated.
|
||||
return position_id.lower() in {"intelligence", "intel", "intent"}
|
||||
"""Node-dict wrapper over the shared harness decision (see delivery.py)."""
|
||||
return _is_intent_position_by_id(
|
||||
str(node.get("position_id") or "").strip(), intent_position_id
|
||||
)
|
||||
|
||||
|
||||
def _session_intent_position_id(session: dict[str, Any]) -> str | None:
|
||||
chain = session.get("chain_snapshot") if isinstance(session.get("chain_snapshot"), dict) else {}
|
||||
value = chain.get("_intent_position_id")
|
||||
return value.strip() if isinstance(value, str) and value.strip() else None
|
||||
"""Session-dict wrapper over the shared harness decision (see delivery.py)."""
|
||||
return _chain_intent_position_id(session.get("chain_snapshot"))
|
||||
|
||||
|
||||
async def _configured_intent_position_id(request: Request) -> str:
|
||||
@@ -437,62 +494,11 @@ async def _configured_intent_position_id(request: Request) -> str:
|
||||
return "intelligence"
|
||||
|
||||
|
||||
def _is_markdown_artifact(artifact: Any) -> bool:
|
||||
if not isinstance(artifact, dict):
|
||||
return False
|
||||
path = str(artifact.get("path") or artifact.get("artifact_url") or "").strip().lower()
|
||||
name = str(artifact.get("name") or artifact.get("filename") or "").strip().lower()
|
||||
mime = str(artifact.get("mime_type") or artifact.get("mimeType") or "").strip().lower()
|
||||
if path and not path.replace("\\", "/").lstrip("/").startswith("mnt/user-data/outputs/"):
|
||||
return False
|
||||
return (
|
||||
"markdown" in mime
|
||||
or path.endswith(".md")
|
||||
or path.endswith(".markdown")
|
||||
or name.endswith(".md")
|
||||
or name.endswith(".markdown")
|
||||
)
|
||||
|
||||
|
||||
def _node_has_markdown_delivery(node: dict[str, Any]) -> bool:
|
||||
artifacts = node.get("artifact_manifest")
|
||||
return isinstance(artifacts, list) and any(_is_markdown_artifact(item) for item in artifacts)
|
||||
|
||||
|
||||
def _markdown_delivery_changed(
|
||||
previous_artifacts: Any,
|
||||
incoming_artifacts: list[dict[str, Any]],
|
||||
) -> bool:
|
||||
"""Whether this turn rewrote a Markdown deliverable, not just chatted.
|
||||
|
||||
A completed node can receive normal follow-up Q&A. That must not bump its
|
||||
effective delivery revision or invalidate every downstream stage unless a
|
||||
Markdown report really changed. The standard artifact listing includes
|
||||
`modified_at` and `size_bytes`, which together make a stable lightweight
|
||||
version marker without reading a potentially large file again.
|
||||
"""
|
||||
old = previous_artifacts if isinstance(previous_artifacts, list) else []
|
||||
old_signatures = {
|
||||
(
|
||||
str(item.get("path") or item.get("artifact_url") or ""),
|
||||
str(item.get("modified_at") or item.get("modifiedAt") or ""),
|
||||
str(item.get("size_bytes") or item.get("sizeBytes") or ""),
|
||||
)
|
||||
for item in old
|
||||
if isinstance(item, dict) and _is_markdown_artifact(item)
|
||||
}
|
||||
new_signatures = {
|
||||
(
|
||||
str(item.get("path") or item.get("artifact_url") or ""),
|
||||
str(item.get("modified_at") or item.get("modifiedAt") or ""),
|
||||
str(item.get("size_bytes") or item.get("sizeBytes") or ""),
|
||||
)
|
||||
for item in incoming_artifacts
|
||||
if isinstance(item, dict) and _is_markdown_artifact(item)
|
||||
}
|
||||
return bool(new_signatures - old_signatures)
|
||||
|
||||
|
||||
def _intent_string_list(intent: dict[str, Any], key: str) -> list[str]:
|
||||
"""Return a clean, display-safe string list from a persisted intent field."""
|
||||
value = intent.get(key)
|
||||
@@ -907,26 +913,6 @@ def _source_revisions(nodes: list[dict[str, Any]]) -> dict[str, int]:
|
||||
return {str(node["node_key"]): int(node.get("revision") or 0) for node in _delivered_nodes(nodes)}
|
||||
|
||||
|
||||
def _is_json_artifact(artifact: Any) -> bool:
|
||||
if not isinstance(artifact, dict):
|
||||
return False
|
||||
path = str(artifact.get("path") or artifact.get("artifact_url") or "").strip().lower()
|
||||
name = str(artifact.get("name") or artifact.get("filename") or "").strip().lower()
|
||||
mime = str(artifact.get("mime_type") or artifact.get("mimeType") or "").strip().lower()
|
||||
if path and not path.replace("\\", "/").lstrip("/").startswith("mnt/user-data/outputs/"):
|
||||
return False
|
||||
return "json" in mime or path.endswith(".json") or name.endswith(".json")
|
||||
|
||||
|
||||
def _artifact_signature(artifact: dict[str, Any]) -> tuple[str, str, str]:
|
||||
"""Use server-returned path/mtime/size to detect an actually rewritten file."""
|
||||
return (
|
||||
str(artifact.get("path") or artifact.get("artifact_url") or ""),
|
||||
str(artifact.get("modified_at") or artifact.get("modifiedAt") or ""),
|
||||
str(artifact.get("size_bytes") or artifact.get("sizeBytes") or ""),
|
||||
)
|
||||
|
||||
|
||||
def _artifact_manifest_refs(artifacts: list[dict[str, Any]]) -> list[dict[str, Any]]:
|
||||
"""Persist display metadata plus any database recovery payload."""
|
||||
refs: list[dict[str, Any]] = []
|
||||
@@ -1276,7 +1262,8 @@ async def update_task_snapshot(
|
||||
request: Request, session_id: str, body: TaskSnapshotUpdateRequest
|
||||
) -> SessionResponse:
|
||||
store, user_id, current = await _require_writable_session(request, session_id)
|
||||
row = await store.update_session(
|
||||
row = await _update_session_or_raise(
|
||||
store,
|
||||
session_id,
|
||||
user_id,
|
||||
task_snapshot=_task_snapshot_for_scope(
|
||||
@@ -1284,9 +1271,8 @@ async def update_task_snapshot(
|
||||
body.task_snapshot,
|
||||
task_scoped=bool(current.get("task_scoped")),
|
||||
),
|
||||
allowed_statuses={"intent_pending"},
|
||||
)
|
||||
if row is None:
|
||||
raise HTTPException(status_code=404, detail="Position roundtable session not found")
|
||||
return _project_session(row)
|
||||
|
||||
|
||||
@@ -1295,15 +1281,15 @@ async def update_intent_snapshot(
|
||||
request: Request, session_id: str, body: IntentUpdateRequest
|
||||
) -> SessionResponse:
|
||||
store, user_id, _ = await _require_writable_session(request, session_id)
|
||||
row = await store.update_session(
|
||||
row = await _update_session_or_raise(
|
||||
store,
|
||||
session_id,
|
||||
user_id,
|
||||
intent_thread_id=body.intent_thread_id,
|
||||
intent_snapshot=body.intent_snapshot,
|
||||
intent_conversation=body.conversation_snapshot,
|
||||
allowed_statuses={"intent_pending"},
|
||||
)
|
||||
if row is None:
|
||||
raise HTTPException(status_code=404, detail="Position roundtable session not found")
|
||||
return _project_session(row)
|
||||
|
||||
|
||||
@@ -1312,14 +1298,13 @@ async def update_summary_conversation(
|
||||
request: Request, session_id: str, body: ConversationStateUpdateRequest
|
||||
) -> SessionResponse:
|
||||
store, user_id, _ = await _require_writable_session(request, session_id)
|
||||
row = await store.update_session(
|
||||
row = await _update_session_or_raise(
|
||||
store,
|
||||
session_id,
|
||||
user_id,
|
||||
summary_thread_id=body.thread_id,
|
||||
summary_conversation=body.conversation_snapshot,
|
||||
)
|
||||
if row is None:
|
||||
raise HTTPException(status_code=404, detail="Position roundtable session not found")
|
||||
return _project_session(row)
|
||||
|
||||
|
||||
@@ -1328,14 +1313,13 @@ async def update_action_plan_conversation(
|
||||
request: Request, session_id: str, body: ConversationStateUpdateRequest
|
||||
) -> SessionResponse:
|
||||
store, user_id, _ = await _require_writable_session(request, session_id)
|
||||
row = await store.update_session(
|
||||
row = await _update_session_or_raise(
|
||||
store,
|
||||
session_id,
|
||||
user_id,
|
||||
action_plan_thread_id=body.thread_id,
|
||||
action_plan_conversation=body.conversation_snapshot,
|
||||
)
|
||||
if row is None:
|
||||
raise HTTPException(status_code=404, detail="Position roundtable session not found")
|
||||
return _project_session(row)
|
||||
|
||||
|
||||
@@ -1374,34 +1358,60 @@ async def activate_session(
|
||||
current.get("intent_snapshot"),
|
||||
intent_position_id=intent_position_id,
|
||||
)
|
||||
activated = await store.activate_session(
|
||||
session_id,
|
||||
user_id,
|
||||
chain_id=body.chain_id,
|
||||
chain_snapshot=chain_snapshot,
|
||||
nodes=nodes,
|
||||
# 命令式状态机:锁 session + 锁内判定「已激活→链相同幂等/链不同 409」,
|
||||
# 消除原 activate_session 的 check-then-insert 竞态(并发双击 → 唯一键冲突 500)。
|
||||
result = _command_result_or_raise(
|
||||
await store.apply_session_command(
|
||||
session_id,
|
||||
user_id,
|
||||
command_id=_command_id_or_new(body.command_id),
|
||||
command_type="activate",
|
||||
payload={
|
||||
"chain_id": body.chain_id,
|
||||
"chain_snapshot": chain_snapshot,
|
||||
"nodes": nodes,
|
||||
},
|
||||
)
|
||||
)
|
||||
return SessionDetailResponse(
|
||||
**result["session"],
|
||||
nodes=[_project_node(node) for node in result["nodes"]],
|
||||
)
|
||||
if activated is None:
|
||||
raise HTTPException(status_code=404, detail="Position roundtable session not found")
|
||||
row, node_rows = activated
|
||||
return SessionDetailResponse(**row, nodes=[_project_node(node) for node in node_rows])
|
||||
|
||||
|
||||
@router.post("/sessions/{session_id}/archive", response_model=SessionResponse)
|
||||
async def archive_session(request: Request, session_id: str) -> SessionResponse:
|
||||
row = await _get_store(request).archive_session(session_id, _current_user_id(request))
|
||||
if row is None:
|
||||
raise HTTPException(status_code=404, detail="Position roundtable session not found")
|
||||
return _project_session(row)
|
||||
store = _get_store(request)
|
||||
user_id = _current_user_id(request)
|
||||
# 命令式状态机:已归档幂等返回,锁内串行化并发归档。每次操作一个稳定的
|
||||
# command_id(URL 无 body),用 session 级固定派生键,重复请求天然重放。
|
||||
result = _command_result_or_raise(
|
||||
await store.apply_session_command(
|
||||
session_id,
|
||||
user_id,
|
||||
command_id=f"archive-{session_id}",
|
||||
command_type="archive",
|
||||
payload={},
|
||||
)
|
||||
)
|
||||
return _project_session(result["session"])
|
||||
|
||||
|
||||
@router.delete("/sessions/{session_id}")
|
||||
async def delete_session(request: Request, session_id: str) -> dict[str, bool]:
|
||||
deleted = await _get_store(request).delete_session(
|
||||
session_id, _current_user_id(request)
|
||||
store = _get_store(request)
|
||||
user_id = _current_user_id(request)
|
||||
# 命令式状态机:命令记录无外键(保留供重放),删除后同 command_id 重试 → 重放
|
||||
# ``{deleted: True}``,避免已删会话再次请求报 404 让前端误判失败。
|
||||
_command_result_or_raise(
|
||||
await store.apply_session_command(
|
||||
session_id,
|
||||
user_id,
|
||||
command_id=f"delete-{session_id}",
|
||||
command_type="delete",
|
||||
payload={},
|
||||
)
|
||||
)
|
||||
if not deleted:
|
||||
raise HTTPException(status_code=404, detail="Position roundtable session not found")
|
||||
return {"deleted": True}
|
||||
|
||||
|
||||
@@ -1418,23 +1428,19 @@ async def bind_node_thread(
|
||||
request: Request, session_id: str, node_key: str, body: NodeThreadRequest
|
||||
) -> NodeResponse:
|
||||
store, user_id, _ = await _require_writable_session(request, session_id)
|
||||
current = await store.get_node(session_id, node_key, user_id)
|
||||
if current is None:
|
||||
raise HTTPException(status_code=404, detail="Position roundtable node not found")
|
||||
if current["status"] == "locked":
|
||||
raise HTTPException(status_code=409, detail="上游节点尚未完成,当前节点未解锁")
|
||||
if current.get("invalidated_by"):
|
||||
raise HTTPException(status_code=409, detail="上游产物已被驳回或重做,当前节点需等待前序节点重新完成")
|
||||
if current.get("thread_id") and current["thread_id"] != body.thread_id:
|
||||
raise HTTPException(status_code=409, detail="该节点已绑定另一条对话线程")
|
||||
row = await store.update_node(
|
||||
session_id,
|
||||
node_key,
|
||||
user_id,
|
||||
thread_id=body.thread_id,
|
||||
status="running" if current.get("status") == "ready" else current.get("status", "locked"),
|
||||
# 命令式状态机:锁内判定「是否解锁 / 是否已被其它线程占用」并绑定。
|
||||
# 原实现在事务外读取节点状态做判定(TOCTOU),并发下两个请求可同时通过校验。
|
||||
result = _command_result_or_raise(
|
||||
await store.apply_node_command(
|
||||
session_id,
|
||||
node_key,
|
||||
user_id,
|
||||
command_id=_command_id_or_new(body.command_id),
|
||||
command_type="bind",
|
||||
payload={"thread_id": body.thread_id},
|
||||
)
|
||||
)
|
||||
return _project_node(row)
|
||||
return _project_node(result["node"])
|
||||
|
||||
|
||||
@router.put(
|
||||
@@ -1448,25 +1454,21 @@ async def update_node_conversation(
|
||||
body: ConversationStateUpdateRequest,
|
||||
) -> NodeResponse:
|
||||
store, user_id, session = await _require_writable_session(request, session_id)
|
||||
current = await store.get_node(session_id, node_key, user_id)
|
||||
if current is None:
|
||||
raise HTTPException(status_code=404, detail="Position roundtable node not found")
|
||||
if (
|
||||
body.thread_id
|
||||
and current.get("thread_id")
|
||||
and current["thread_id"] != body.thread_id
|
||||
):
|
||||
raise HTTPException(status_code=409, detail="Position node is bound to another thread")
|
||||
row = await store.update_node(
|
||||
session_id,
|
||||
node_key,
|
||||
user_id,
|
||||
thread_id=body.thread_id if body.thread_id else current.get("thread_id"),
|
||||
conversation_snapshot=body.conversation_snapshot,
|
||||
# 命令式状态机:线程归属校验移入 FOR UPDATE 锁内。
|
||||
result = _command_result_or_raise(
|
||||
await store.apply_node_command(
|
||||
session_id,
|
||||
node_key,
|
||||
user_id,
|
||||
command_id=_command_id_or_new(body.command_id),
|
||||
command_type="conversation",
|
||||
payload={
|
||||
"thread_id": body.thread_id,
|
||||
"conversation_snapshot": body.conversation_snapshot,
|
||||
},
|
||||
)
|
||||
)
|
||||
if row is None:
|
||||
raise HTTPException(status_code=404, detail="Position roundtable node not found")
|
||||
return _project_node(row)
|
||||
return _project_node(result["node"])
|
||||
|
||||
|
||||
@router.post("/sessions/{session_id}/nodes/{node_key}/context")
|
||||
@@ -1483,126 +1485,56 @@ async def complete_node_turn(
|
||||
node = await store.get_node(session_id, node_key, user_id)
|
||||
if node is None:
|
||||
raise HTTPException(status_code=404, detail="Position roundtable node not found")
|
||||
if node["status"] == "locked":
|
||||
raise HTTPException(status_code=409, detail="上游节点尚未完成,当前节点未解锁")
|
||||
if node.get("invalidated_by"):
|
||||
raise HTTPException(status_code=409, detail="上游产物已被驳回或重做,当前节点需等待前序节点重新完成")
|
||||
# Artifact listing is best-effort on the client. Never erase a previously
|
||||
# delivered report merely because one listing request transiently failed.
|
||||
effective_artifacts = body.artifact_manifest or list(node.get("artifact_manifest") or [])
|
||||
# 产物内容持久化是文件 IO,保持在数据库事务之外(避免长事务)。清单为空表示
|
||||
# 「本轮无新清单」,由锁内回退到节点当前清单——绝不在事务外用过期快照兜底。
|
||||
persisted: list[dict[str, Any]] | None = None
|
||||
if body.artifact_manifest:
|
||||
effective_artifacts = await _persist_artifact_contents(
|
||||
persisted = await _persist_artifact_contents(
|
||||
node.get("thread_id"),
|
||||
effective_artifacts,
|
||||
body.artifact_manifest,
|
||||
)
|
||||
has_markdown = any(_is_markdown_artifact(item) for item in effective_artifacts)
|
||||
markdown_changed = _markdown_delivery_changed(node.get("artifact_manifest"), effective_artifacts)
|
||||
requires_fresh_delivery = node.get("status") in {"rejected", "stale"}
|
||||
next_status = (
|
||||
"done"
|
||||
if _is_intent_position_node(node, _session_intent_position_id(session))
|
||||
else "done"
|
||||
if has_markdown and (not requires_fresh_delivery or markdown_changed)
|
||||
else "running"
|
||||
)
|
||||
# Effective revisions are delivery revisions, not every conversational
|
||||
# answer. This prevents a harmless follow-up on a completed seat from
|
||||
# invalidating all later stages just because its old Markdown file still
|
||||
# appears in the artifact list.
|
||||
new_effective_delivery = (
|
||||
next_status == "done"
|
||||
and (
|
||||
_is_intent_position_node(node, _session_intent_position_id(session))
|
||||
or node.get("status") != "done"
|
||||
or markdown_changed
|
||||
# 命令式状态机:能否完成 / 是否新交付 / revision+1 / 失效下游 / 解锁下一 stage,
|
||||
# 全部在单一 FOR UPDATE 事务内基于锁定行判定并原子落库(apply_node_command)。
|
||||
# 原实现先读快照算好 next_status 再进事务,两个并发请求会基于同一过期快照各自
|
||||
# 判定,后写覆盖先写 → 下一 stage 卡 locked / invalidated_by 残留 → 断链。
|
||||
result = _command_result_or_raise(
|
||||
await store.apply_node_command(
|
||||
session_id,
|
||||
node_key,
|
||||
user_id,
|
||||
command_id=_command_id_or_new(body.command_id),
|
||||
command_type="complete",
|
||||
payload={
|
||||
"latest_answer": body.latest_answer,
|
||||
"artifact_manifest": persisted,
|
||||
"upstream_revision": body.upstream_revision,
|
||||
},
|
||||
)
|
||||
)
|
||||
row = await store.update_node(
|
||||
session_id,
|
||||
node_key,
|
||||
user_id,
|
||||
status=next_status,
|
||||
latest_answer=body.latest_answer,
|
||||
artifact_manifest=effective_artifacts,
|
||||
upstream_revision=body.upstream_revision,
|
||||
invalidated_by=None,
|
||||
increment_revision=new_effective_delivery,
|
||||
)
|
||||
if row is None:
|
||||
raise HTTPException(status_code=404, detail="Position roundtable node not found")
|
||||
|
||||
# A node has no coordinator. Its effective delivery alone drives the frozen
|
||||
# chain: all parallel seats in the current stage must finish before the
|
||||
# next stage is made writable. A plain conversational follow-up keeps the
|
||||
# existing downstream deliveries valid; only a newly written Markdown
|
||||
# report advances the revision and makes later stages stale.
|
||||
nodes = await store.list_nodes(session_id, user_id)
|
||||
if nodes is None:
|
||||
raise HTTPException(status_code=404, detail="Position roundtable session not found")
|
||||
if new_effective_delivery and node.get("revision", 0) > 0:
|
||||
for downstream in nodes:
|
||||
if (
|
||||
downstream["stage_index"] > row["stage_index"]
|
||||
and downstream["status"] in {"ready", "running", "done", "stale", "rejected", "error"}
|
||||
):
|
||||
has_material = (
|
||||
downstream.get("revision", 0) > 0
|
||||
or bool(downstream.get("latest_answer"))
|
||||
or bool(downstream.get("artifact_manifest"))
|
||||
)
|
||||
await store.update_node(
|
||||
session_id,
|
||||
downstream["node_key"],
|
||||
user_id,
|
||||
status="stale" if has_material else "locked",
|
||||
invalidated_by=row["node_key"],
|
||||
)
|
||||
nodes = await store.list_nodes(session_id, user_id) or []
|
||||
|
||||
current_stage = [item for item in nodes if item["stage_index"] == row["stage_index"]]
|
||||
later_stages = sorted(
|
||||
{item["stage_index"] for item in nodes if item["stage_index"] > row["stage_index"]}
|
||||
)
|
||||
if row["status"] == "done" and current_stage and all(item["status"] == "done" for item in current_stage) and later_stages:
|
||||
next_stage = later_stages[0]
|
||||
for next_node in nodes:
|
||||
if next_node["stage_index"] == next_stage and next_node["status"] in {"locked", "stale"}:
|
||||
await store.update_node(
|
||||
session_id,
|
||||
next_node["node_key"],
|
||||
user_id,
|
||||
status="ready" if next_node["status"] == "locked" else "stale",
|
||||
invalidated_by=None,
|
||||
)
|
||||
return _project_node(row)
|
||||
return _project_node(result["node"])
|
||||
|
||||
|
||||
@router.post("/sessions/{session_id}/nodes/{node_key}/reject", response_model=SessionDetailResponse)
|
||||
async def reject_node(
|
||||
request: Request, session_id: str, node_key: str, body: RejectNodeRequest
|
||||
) -> SessionDetailResponse:
|
||||
title = body.title.strip() or "产物驳回"
|
||||
reason = body.reason.strip()
|
||||
store, user_id, _ = await _require_writable_session(request, session_id)
|
||||
current = await store.get_node(session_id, node_key, user_id)
|
||||
if current is None:
|
||||
raise HTTPException(status_code=404, detail="Position roundtable node not found")
|
||||
if current["status"] == "locked":
|
||||
raise HTTPException(status_code=409, detail="当前节点尚未解锁,暂无可驳回的交付产物")
|
||||
if current.get("revision", 0) <= 0 and not current.get("latest_answer") and not current.get("artifact_manifest"):
|
||||
raise HTTPException(status_code=409, detail="当前节点暂无可驳回的交付产物")
|
||||
rejected = await store.reject_node(
|
||||
session_id,
|
||||
node_key,
|
||||
user_id,
|
||||
title=title,
|
||||
reason=reason,
|
||||
rejected_by=user_id,
|
||||
# 命令式状态机:可驳回性判定 + 驳回 + 下游失效在同一 FOR UPDATE 事务内完成,
|
||||
# 并 bump session/node version。同 command_id 的重复驳回原样重放、不重复计数。
|
||||
result = _command_result_or_raise(
|
||||
await store.apply_node_command(
|
||||
session_id,
|
||||
node_key,
|
||||
user_id,
|
||||
command_id=_command_id_or_new(body.command_id),
|
||||
command_type="reject",
|
||||
payload={"title": body.title, "reason": body.reason},
|
||||
)
|
||||
)
|
||||
return SessionDetailResponse(
|
||||
**result["session"],
|
||||
nodes=[_project_node(node) for node in result["nodes"]],
|
||||
)
|
||||
if rejected is None:
|
||||
raise HTTPException(status_code=404, detail="Position roundtable node not found")
|
||||
row, node_rows = rejected
|
||||
return SessionDetailResponse(**row, nodes=[_project_node(node) for node in node_rows])
|
||||
|
||||
|
||||
async def _prepare_builtin_run(
|
||||
@@ -1706,7 +1638,8 @@ async def _complete_builtin_run(
|
||||
_artifact_signature(previous_primary) != _artifact_signature(new_primary)
|
||||
)
|
||||
if kind == "summary":
|
||||
row = await store.update_session(
|
||||
row = await _update_session_or_raise(
|
||||
store,
|
||||
session_id,
|
||||
user_id,
|
||||
summary_thread_id=body.thread_id,
|
||||
@@ -1717,15 +1650,14 @@ async def _complete_builtin_run(
|
||||
status="active",
|
||||
)
|
||||
else:
|
||||
row = await store.update_session(
|
||||
row = await _update_session_or_raise(
|
||||
store,
|
||||
session_id,
|
||||
user_id,
|
||||
action_plan_thread_id=body.thread_id,
|
||||
action_plan_snapshot=snapshot,
|
||||
status="completed",
|
||||
)
|
||||
if row is None:
|
||||
raise HTTPException(status_code=404, detail="Position roundtable session not found")
|
||||
node_rows = await store.list_nodes(session_id, user_id) or []
|
||||
return SessionDetailResponse(**row, nodes=[_project_node(node) for node in node_rows])
|
||||
|
||||
|
||||
+40
-12
@@ -19,6 +19,7 @@ from pydantic import AliasChoices, BaseModel, ConfigDict, Field, StrictInt, Stri
|
||||
|
||||
from app.gateway.deps import get_current_user
|
||||
from deerflow.persistence.roundtable_artifact_submissions import RoundtableArtifactSubmissionStore
|
||||
from deerflow.persistence.roundtable_artifact_submissions.sql import SubmissionVersionConflict
|
||||
|
||||
router = APIRouter(prefix="/api/roundtable/tasks", tags=["roundtable-artifact-submissions"])
|
||||
|
||||
@@ -53,6 +54,7 @@ class ArtifactSubmissionRequest(BaseModel):
|
||||
submitted: bool
|
||||
submission_id: StrictStr | None = Field(default=None, max_length=64, validation_alias=AliasChoices("submissionId", "submission_id"))
|
||||
task_delivery_id: StrictStr | None = Field(default=None, max_length=255, validation_alias=AliasChoices("taskDeliveryId", "task_delivery_id"))
|
||||
expected_version: int | None = Field(default=None, ge=0, validation_alias=AliasChoices("expectedVersion", "expected_version"))
|
||||
artifact: ArtifactReferenceInput
|
||||
|
||||
@field_validator("task_id", "submission_id", "task_delivery_id", mode="before")
|
||||
@@ -82,6 +84,7 @@ class ArtifactSubmissionResponse(BaseModel):
|
||||
|
||||
id: str
|
||||
task_id: str = Field(serialization_alias="taskId")
|
||||
version: int = Field(default=0)
|
||||
submitted: bool
|
||||
submitted_at: str | None = Field(default=None, serialization_alias="submittedAt")
|
||||
cancelled_at: str | None = Field(default=None, serialization_alias="cancelledAt")
|
||||
@@ -131,6 +134,7 @@ def _response(row: dict[str, Any]) -> ArtifactSubmissionResponse:
|
||||
return ArtifactSubmissionResponse(
|
||||
id=str(row["id"]),
|
||||
task_id=str(row["task_id"]),
|
||||
version=int(row.get("version", 0)),
|
||||
submitted=bool(row["submitted"]),
|
||||
submitted_at=row.get("submitted_at"),
|
||||
cancelled_at=row.get("cancelled_at"),
|
||||
@@ -178,19 +182,43 @@ async def set_artifact_submission(
|
||||
# a thread reference a saved state could never be previewed correctly.
|
||||
if not artifact["thread_id"]:
|
||||
raise HTTPException(status_code=422, detail="artifact.threadId is required when submitted is true")
|
||||
row = await store.submit(
|
||||
path_task_id,
|
||||
artifact,
|
||||
task_delivery_id=body.task_delivery_id,
|
||||
actor_id=actor_id,
|
||||
)
|
||||
try:
|
||||
row = await store.submit(
|
||||
path_task_id,
|
||||
artifact,
|
||||
task_delivery_id=body.task_delivery_id,
|
||||
actor_id=actor_id,
|
||||
expected_version=body.expected_version,
|
||||
)
|
||||
except SubmissionVersionConflict as conflict:
|
||||
raise HTTPException(
|
||||
status_code=409,
|
||||
detail={
|
||||
"code": "version_conflict",
|
||||
"message": "提交产物版本冲突:另一写入已先提交,请刷新后重试。",
|
||||
"current_version": conflict.row.get("version", 0),
|
||||
"submission": _response(conflict.row).model_dump(by_alias=True, exclude_none=False),
|
||||
},
|
||||
) from conflict
|
||||
else:
|
||||
row = await store.cancel(
|
||||
path_task_id,
|
||||
artifact,
|
||||
submission_id=body.submission_id,
|
||||
actor_id=actor_id,
|
||||
)
|
||||
try:
|
||||
row = await store.cancel(
|
||||
path_task_id,
|
||||
artifact,
|
||||
submission_id=body.submission_id,
|
||||
actor_id=actor_id,
|
||||
expected_version=body.expected_version,
|
||||
)
|
||||
except SubmissionVersionConflict as conflict:
|
||||
raise HTTPException(
|
||||
status_code=409,
|
||||
detail={
|
||||
"code": "version_conflict",
|
||||
"message": "取消产物版本冲突:另一写入已先提交,旧取消不能撤销新提交。",
|
||||
"current_version": conflict.row.get("version", 0),
|
||||
"submission": _response(conflict.row).model_dump(by_alias=True, exclude_none=False),
|
||||
},
|
||||
) from conflict
|
||||
if row is None:
|
||||
raise HTTPException(status_code=404, detail="Artifact submission not found")
|
||||
return _response(row)
|
||||
@@ -32,6 +32,7 @@ from uuid import uuid4
|
||||
from fastapi import APIRouter, HTTPException, Request
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from deerflow.persistence.roundtable_drafts.sql import DraftConcurrentWriteError
|
||||
from deerflow.runtime.user_context import get_effective_user_id
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -47,6 +48,8 @@ class DraftMetaResponse(BaseModel):
|
||||
task_id: str | None = None
|
||||
title: str = ""
|
||||
furthest_step: int = 1
|
||||
# 乐观锁版本号:PUT 时经 expected_version 回传,冲突 → 409 + current_version。
|
||||
version: int = 0
|
||||
# 本次会商所用业务链名(历史卡片展示);无链条(自由/recommend 模式)时为 None。
|
||||
chain_title: str | None = None
|
||||
created_at: datetime | str | None = None
|
||||
@@ -88,6 +91,9 @@ class DraftUpdateRequest(BaseModel):
|
||||
step1: dict[str, Any] | None = None
|
||||
step2: dict[str, Any] | None = None
|
||||
step3: dict[str, Any] | None = None
|
||||
# 乐观锁:客户端基于的草稿版本号。缺省(旧客户端)= 盲写兼容;
|
||||
# 提供时版本不匹配 → 409,body 带 current_version 供重拉合并。
|
||||
expected_version: int | None = Field(default=None, ge=0)
|
||||
|
||||
|
||||
class RecommendPickSchema(BaseModel):
|
||||
@@ -204,7 +210,18 @@ async def update_draft(request: Request, draft_id: str, body: DraftUpdateRequest
|
||||
# Build kwargs that distinguish "omitted" from "explicitly None". The store
|
||||
# uses a sentinel; here we pass only the fields the client actually sent.
|
||||
fields = body.model_dump(exclude_unset=True)
|
||||
row = await store.update_draft(draft_id, user_id, **fields)
|
||||
try:
|
||||
row = await store.update_draft(draft_id, user_id, **fields)
|
||||
except DraftConcurrentWriteError as err:
|
||||
# 版本冲突:detail 里带 current_version,前端据此重拉最新草稿、本地合并后重试。
|
||||
raise HTTPException(
|
||||
status_code=409,
|
||||
detail={
|
||||
"error": "draft_version_conflict",
|
||||
"message": f"草稿已被并发修改(expected={err.expected}, actual={err.actual})",
|
||||
"current_version": err.actual,
|
||||
},
|
||||
) from err
|
||||
if row is None:
|
||||
raise HTTPException(status_code=404, detail="Draft not found")
|
||||
return DraftResponse(**row)
|
||||
|
||||
@@ -18,6 +18,7 @@ Routes(prefix ``/api/roundtable-jobs``)::
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import hashlib
|
||||
import json
|
||||
import logging
|
||||
import time
|
||||
@@ -25,11 +26,11 @@ from datetime import UTC, datetime, timedelta
|
||||
from typing import Any
|
||||
from uuid import uuid4
|
||||
|
||||
from fastapi import APIRouter, HTTPException, Request
|
||||
from fastapi import APIRouter, HTTPException, Request, Response
|
||||
from fastapi.responses import StreamingResponse
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from app.gateway.roundtable_job_executor import StartParams
|
||||
from app.gateway.roundtable_job_executor import build_start_snapshot
|
||||
from deerflow.agents.roundtable_orchestrator import SeatRef
|
||||
from deerflow.runtime.user_context import get_effective_user_id
|
||||
|
||||
@@ -54,6 +55,10 @@ class JobAgent(BaseModel):
|
||||
|
||||
|
||||
class StartJobRequest(BaseModel):
|
||||
# Phase 3 客户端幂等键:前端为每次「后台挂起」生成一个 requestId,网络重试**复用同一
|
||||
# requestId**。服务端据此审计 / 排查;「同草稿不双跑」本身由 active_dedupe_key 唯一索引
|
||||
# 保证(requestId 只是锦上添花的请求级指纹)。缺省(旧前端)→ 服务端生成一个。
|
||||
requestId: str | None = Field(default=None, max_length=64)
|
||||
draftId: str | None = Field(default=None, max_length=64)
|
||||
# 外部任务 id(taskId 深链会商)。非空 = task 作业:按 task_id 共享、不按 user 分权,
|
||||
# 其 draft_id 指向独立的 roundtable_task_drafts 表(最终报告写回那里)。
|
||||
@@ -151,6 +156,20 @@ def _get_executor(request: Request):
|
||||
return executor
|
||||
|
||||
|
||||
def _get_dispatcher(request: Request):
|
||||
"""Phase 3 dispatcher(可能为 None:store 未配置时)。nudge 前先判空。"""
|
||||
return getattr(request.app.state, "roundtable_job_dispatcher", None)
|
||||
|
||||
|
||||
def _request_hash(body: StartJobRequest) -> str:
|
||||
"""启动入参指纹(sha256)——审计/排查「同 requestId 但入参变了」的异常重试。"""
|
||||
canonical = json.dumps(
|
||||
body.model_dump(mode="json", exclude={"requestId"}),
|
||||
ensure_ascii=False, sort_keys=True, default=str,
|
||||
)
|
||||
return hashlib.sha256(canonical.encode("utf-8")).hexdigest()
|
||||
|
||||
|
||||
def _pending_nodes(agents: list[JobAgent]) -> list[dict[str, Any]]:
|
||||
"""启动即落一份「全 pending」的派活链路,使 GET / 首帧 SSE 立刻能渲染流程图。"""
|
||||
return [
|
||||
@@ -194,32 +213,63 @@ def _seat_refs_from_nodes(nodes: list[dict[str, Any]] | None) -> list[SeatRef]:
|
||||
|
||||
|
||||
@router.post("", response_model=RoundtableJobResponse, status_code=201)
|
||||
async def start_job(request: Request, body: StartJobRequest) -> RoundtableJobResponse:
|
||||
async def start_job(
|
||||
request: Request, body: StartJobRequest, response: Response
|
||||
) -> RoundtableJobResponse:
|
||||
"""启动一个后台作业(Phase 3:唯一活跃键去重 + dispatcher 领取执行)。
|
||||
|
||||
流程:
|
||||
1. 把整份启动入参序列化成 ``input_snapshot`` 落库(跨 worker 重建 StartParams 的前提);
|
||||
2. ``try_create_or_get_active`` 直接 INSERT 并占用 ``active_dedupe_key`` 唯一索引 ——
|
||||
冲突(同草稿已有活跃作业)即幂等返回它(``created=False`` → HTTP 200),不再新建;
|
||||
3. 新建成功(HTTP 201,status=queued)后 ``nudge`` dispatcher,由它 ``claim_job``
|
||||
原子领取并在本(或其它)worker 执行 —— **不再由路由直接拉起进程内任务**。
|
||||
|
||||
「同一草稿至多一个活跃作业」由 DB 唯一索引强制,跨 worker / 并发 / 重试天然互斥,
|
||||
不依赖 GET_LOCK 命名锁或进程内 asyncio。
|
||||
"""
|
||||
store = _get_store(request)
|
||||
executor = _get_executor(request)
|
||||
user_id = _current_user_id(request)
|
||||
|
||||
if not body.agents:
|
||||
raise HTTPException(status_code=400, detail="agents is required")
|
||||
|
||||
# 防重复挂起(服务端幂等闸门):同一草稿已有未终态作业(queued/running/awaiting_input)
|
||||
# 时直接返回它、不再新建。前端的「手动挂起按钮 / 离开页面自动挂起 / beforeunload 刷新
|
||||
# 兜底」可能对同一研讨多次触发本端点 —— 客户端去重只能挡住同页内的重复,跨触发源 /
|
||||
# 跨标签页的并发只能靠这里兜住,否则同一研讨会被双跑。
|
||||
task_id = (body.taskId or "").strip() or None
|
||||
if body.draftId:
|
||||
# task 作业按 task_id 去重(跨用户共享);个人作业按 user 去重。
|
||||
existing = await store.get_by_draft(body.draftId, user_id=user_id, task_id=task_id)
|
||||
if existing is not None and (existing.get("status") or "") not in _TERMINAL:
|
||||
return _to_response(existing)
|
||||
|
||||
draft_id = (body.draftId or "").strip() or None
|
||||
agents = [SeatRef(a.agentId, a.name or a.agentId) for a in body.agents]
|
||||
seed_message = (body.seedMessage or body.intent or "").strip()
|
||||
|
||||
# 整份启动入参快照 —— dispatcher 在任意 worker 领取后据此无损重建 StartParams。
|
||||
snapshot = build_start_snapshot(
|
||||
user_id=user_id,
|
||||
agents=agents,
|
||||
seed_message=seed_message,
|
||||
model=body.model,
|
||||
mode=body.orchestrationMode,
|
||||
intent_text=body.intent,
|
||||
thread_ids=body.threadIds,
|
||||
coordinator_name=body.coordinatorName,
|
||||
draft_id=draft_id,
|
||||
task_id=task_id,
|
||||
is_resume=False,
|
||||
chain=body.chain,
|
||||
orchestration_plan=body.orchestrationPlan,
|
||||
seat_thinking_enabled=body.thinking_enabled,
|
||||
seat_reasoning_effort=body.reasoning_effort,
|
||||
seat_subagent_enabled=body.subagent_enabled,
|
||||
seat_skill_directives=dict(body.seat_skill_directives or {}),
|
||||
seat_modes=dict(body.seat_modes or {}),
|
||||
gather_first=bool(body.gather_first),
|
||||
business_code=(body.business_code or None),
|
||||
)
|
||||
request_id = (body.requestId or "").strip() or uuid4().hex
|
||||
|
||||
job_id = uuid4().hex
|
||||
row = await store.create(
|
||||
id=job_id,
|
||||
draft_id=body.draftId,
|
||||
row, created = await store.try_create_or_get_active(
|
||||
draft_id=draft_id,
|
||||
user_id=user_id,
|
||||
task_id=task_id,
|
||||
id=job_id,
|
||||
orchestration_mode=body.orchestrationMode,
|
||||
coordinator_name=body.coordinatorName,
|
||||
thread_ids=body.threadIds,
|
||||
@@ -227,32 +277,21 @@ async def start_job(request: Request, body: StartJobRequest) -> RoundtableJobRes
|
||||
orchestration_plan=body.orchestrationPlan,
|
||||
dispatch_chain=_pending_nodes(body.agents),
|
||||
status="queued",
|
||||
request_id=request_id,
|
||||
request_hash=_request_hash(body),
|
||||
input_snapshot=snapshot,
|
||||
)
|
||||
|
||||
executor.start_job(
|
||||
StartParams(
|
||||
job_id=job_id,
|
||||
user_id=user_id,
|
||||
agents=agents,
|
||||
seed_message=(body.seedMessage or body.intent or "").strip(),
|
||||
model=body.model,
|
||||
mode=body.orchestrationMode,
|
||||
intent_text=body.intent,
|
||||
thread_ids=body.threadIds,
|
||||
coordinator_name=body.coordinatorName,
|
||||
draft_id=body.draftId,
|
||||
task_id=task_id,
|
||||
chain=body.chain,
|
||||
orchestration_plan=body.orchestrationPlan,
|
||||
seat_thinking_enabled=body.thinking_enabled,
|
||||
seat_reasoning_effort=body.reasoning_effort,
|
||||
seat_subagent_enabled=body.subagent_enabled,
|
||||
seat_skill_directives=dict(body.seat_skill_directives or {}),
|
||||
seat_modes=dict(body.seat_modes or {}),
|
||||
gather_first=bool(body.gather_first),
|
||||
business_code=(body.business_code or None),
|
||||
)
|
||||
)
|
||||
if created:
|
||||
# 新建(queued):交给 dispatcher 领取执行。
|
||||
response.status_code = 201
|
||||
else:
|
||||
# 命中已有活跃作业:幂等返回(不重复建 / 不重复跑)。
|
||||
response.status_code = 200
|
||||
# 任一分支都唤醒 dispatcher:新建需被领取;幂等命中时若既有作业还在 queued、未被任何
|
||||
# worker 领走(如 dispatcher 正在轮询间隙),nudge 确保它被立刻 claim,无感延迟。
|
||||
dispatcher = _get_dispatcher(request)
|
||||
if dispatcher is not None:
|
||||
dispatcher.nudge()
|
||||
return _to_response(row)
|
||||
|
||||
|
||||
@@ -310,48 +349,73 @@ async def stream_job(request: Request, job_id: str, task_id: str | None = None)
|
||||
async def resume_job(
|
||||
request: Request, job_id: str, body: ResumeJobRequest, task_id: str | None = None
|
||||
) -> RoundtableJobResponse:
|
||||
"""回答澄清后续跑(Phase 3:条件 UPDATE 串行 + dispatcher 领取)。
|
||||
|
||||
``queue_for_resume`` 用**原子条件 UPDATE**(WHERE status='awaiting_input')把作业翻成
|
||||
queued —— 并发 / 重复的 resume 只有一个能成功(其余返回 None → 409),杜绝「同一澄清
|
||||
被续跑两次」的双跑。成功后清租约 + 写含续跑语义的新 snapshot,``nudge`` dispatcher 由
|
||||
它在任意 worker 领取执行;**路由不再直接拉起进程内任务**。
|
||||
"""
|
||||
store = _get_store(request)
|
||||
executor = _get_executor(request)
|
||||
tid = (task_id or "").strip() or None
|
||||
user_id = _current_user_id(request)
|
||||
|
||||
row = await store.get(job_id, user_id=user_id, task_id=tid)
|
||||
if row is None:
|
||||
raise HTTPException(status_code=404, detail="Job not found")
|
||||
if (row.get("status") or "") != "awaiting_input":
|
||||
raise HTTPException(status_code=409, detail="Job is not awaiting input")
|
||||
|
||||
answer = body.answer.strip()
|
||||
if not answer:
|
||||
raise HTTPException(status_code=400, detail="answer is required")
|
||||
|
||||
# task 作业不分权:放开写入归属校验(user_id=None);并以**原作业所有者**身份续跑,
|
||||
# 保证编排上下文 / 写回 task-draft 用一致的 user 上下文。
|
||||
# task 作业不分权:放开写入归属校验(user_id=None)。
|
||||
write_user_id = None if tid else user_id
|
||||
run_user_id = row.get("user_id") or user_id
|
||||
updated = await store.update_progress(
|
||||
job_id, user_id=write_user_id, status="running", pending_clarification=None
|
||||
)
|
||||
|
||||
executor.start_job(
|
||||
StartParams(
|
||||
job_id=job_id,
|
||||
user_id=run_user_id,
|
||||
agents=_seat_refs_from_nodes(row.get("dispatch_chain")),
|
||||
seed_message=_RESUME_PREFIX + answer,
|
||||
model=None,
|
||||
mode=row.get("orchestration_mode") or "recommend",
|
||||
intent_text=answer,
|
||||
thread_ids=row.get("thread_ids"),
|
||||
coordinator_name=row.get("coordinator_name") or "roundtable-coordinator",
|
||||
draft_id=row.get("draft_id"),
|
||||
task_id=tid or row.get("task_id"),
|
||||
is_resume=True,
|
||||
chain=row.get("chain"),
|
||||
orchestration_plan=row.get("orchestration_plan"),
|
||||
)
|
||||
# 续跑 snapshot:优先复用原 input_snapshot(含席位/模式/链条等完整入参),覆盖续跑三件套
|
||||
# (is_resume / seed_message / intent_text),并用作业行里**最新**的 thread_ids /
|
||||
# coordinator_name(首跑建线程后由执行器写回,比原 snapshot 里的更新)。旧作业无 snapshot
|
||||
# 时从作业行字段兜底重建。
|
||||
base_snapshot = row.get("input_snapshot") or {}
|
||||
if not isinstance(base_snapshot, dict):
|
||||
base_snapshot = {}
|
||||
resume_snapshot: dict[str, Any] = {
|
||||
"v": base_snapshot.get("v", 1),
|
||||
"user_id": run_user_id,
|
||||
"agents": base_snapshot.get("agents")
|
||||
or [{"agent_id": n.get("agentId"), "name": n.get("name")} for n in (row.get("dispatch_chain") or [])],
|
||||
"seed_message": _RESUME_PREFIX + answer,
|
||||
"model": base_snapshot.get("model"),
|
||||
"mode": row.get("orchestration_mode") or base_snapshot.get("mode") or "recommend",
|
||||
"intent_text": answer,
|
||||
"thread_ids": row.get("thread_ids"),
|
||||
"coordinator_name": row.get("coordinator_name") or "roundtable-coordinator",
|
||||
"draft_id": row.get("draft_id"),
|
||||
"task_id": tid or row.get("task_id"),
|
||||
"is_resume": True,
|
||||
"chain": row.get("chain"),
|
||||
"orchestration_plan": row.get("orchestration_plan"),
|
||||
"enable_report": base_snapshot.get("enable_report", True),
|
||||
"seat_thinking_enabled": base_snapshot.get("seat_thinking_enabled"),
|
||||
"seat_reasoning_effort": base_snapshot.get("seat_reasoning_effort"),
|
||||
"seat_subagent_enabled": base_snapshot.get("seat_subagent_enabled", False),
|
||||
"seat_skill_directives": base_snapshot.get("seat_skill_directives") or {},
|
||||
"seat_modes": base_snapshot.get("seat_modes") or {},
|
||||
"gather_first": base_snapshot.get("gather_first", False),
|
||||
"business_code": base_snapshot.get("business_code"),
|
||||
}
|
||||
|
||||
updated = await store.queue_for_resume(
|
||||
job_id, input_snapshot=resume_snapshot, user_id=write_user_id, task_id=tid
|
||||
)
|
||||
return _to_response(updated or row)
|
||||
if updated is None:
|
||||
# 条件 UPDATE 未命中:作业已不在 awaiting_input(被并发 resume 抢先 / 已续跑 / 已终态)。
|
||||
raise HTTPException(status_code=409, detail="Job is not awaiting input")
|
||||
|
||||
dispatcher = _get_dispatcher(request)
|
||||
if dispatcher is not None:
|
||||
dispatcher.nudge()
|
||||
return _to_response(updated)
|
||||
|
||||
|
||||
async def _require_admin(request: Request) -> None:
|
||||
@@ -395,8 +459,19 @@ async def admin_cleanup(request: Request, body: CleanupRequest) -> CleanupRespon
|
||||
|
||||
@router.post("/{job_id}/cancel", response_model=RoundtableJobResponse)
|
||||
async def cancel_job(request: Request, job_id: str, task_id: str | None = None) -> RoundtableJobResponse:
|
||||
"""取消作业(Phase 3:两段式 cancel)。
|
||||
|
||||
1. ``request_cancel``:原子条件 UPDATE(可取消态 → ``cancel_requested``)—— 权威意图落库,
|
||||
重复 cancel 幂等(已 cancel_requested/cancelled/终态时返回 None,直接回现状)。
|
||||
2. **无持租者**(queued / awaiting_input,执行器未在跑)→ 路由立刻 ``finalize_cancel``
|
||||
收口为终态 ``cancelled``(清唯一活跃键 + 租约)。
|
||||
3. **有持租者**(running)→ 保留 ``cancel_requested``,由持租者的 DB 看门狗收口
|
||||
``cancelled``;本接口先回 ``cancel_requested``(SSE 随后见翻转)。租约过期前未能
|
||||
收口(例如 worker 崩溃)时,dispatcher 会回收该取消请求。
|
||||
|
||||
「已取消」终态永远由持租者或 finalize 收口,避免与执行器的进度写竞态。
|
||||
"""
|
||||
store = _get_store(request)
|
||||
executor = _get_executor(request)
|
||||
tid = (task_id or "").strip() or None
|
||||
user_id = _current_user_id(request)
|
||||
|
||||
@@ -404,7 +479,20 @@ async def cancel_job(request: Request, job_id: str, task_id: str | None = None)
|
||||
if row is None:
|
||||
raise HTTPException(status_code=404, detail="Job not found")
|
||||
|
||||
executor.cancel(job_id)
|
||||
# task 作业不分权:放开写入归属校验。
|
||||
updated = await store.update_progress(job_id, user_id=(None if tid else user_id), status="cancelled")
|
||||
return _to_response(updated or row)
|
||||
write_user_id = None if tid else user_id
|
||||
requested = await store.request_cancel(job_id, user_id=write_user_id, task_id=tid)
|
||||
if requested is None:
|
||||
# 已不可取消(终态 / 已 cancel_requested):幂等返回现状,不当作错误。
|
||||
fresh = await store.get(job_id, user_id=user_id, task_id=tid)
|
||||
return _to_response(fresh or row)
|
||||
|
||||
if not requested.get("lease_owner"):
|
||||
# 无持租者:路由直接收口(queued / awaiting_input)。
|
||||
finalized = await store.finalize_cancel(job_id)
|
||||
return _to_response(finalized or requested)
|
||||
|
||||
# 有持租者(running):不要直接 task.cancel()。直接取消会中断当前 worker 的看门狗,
|
||||
# 使 DB 永久遗留在 cancel_requested;让 DB 看门狗以持租者身份收口,崩溃时由 dispatcher
|
||||
# 在租约到期后回收,才能保证跨进程取消最终完成。
|
||||
return _to_response(requested)
|
||||
@@ -28,6 +28,7 @@ from uuid import uuid4
|
||||
from fastapi import APIRouter, HTTPException, Request
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from deerflow.persistence.roundtable_drafts.sql import DraftConcurrentWriteError
|
||||
from deerflow.runtime.user_context import get_effective_user_id
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -42,6 +43,8 @@ class TaskDraftMetaResponse(BaseModel):
|
||||
task_id: str
|
||||
title: str = ""
|
||||
furthest_step: int = 1
|
||||
# 乐观锁版本号:PUT 时经 expected_version 回传,冲突 → 409 + current_version。
|
||||
version: int = 0
|
||||
# 本次会商所用业务链名(历史卡片展示);无链条(自由/recommend 模式)时为 None。
|
||||
chain_title: str | None = None
|
||||
created_at: datetime | str | None = None
|
||||
@@ -77,6 +80,9 @@ class TaskDraftUpdateRequest(BaseModel):
|
||||
step1: dict[str, Any] | None = None
|
||||
step2: dict[str, Any] | None = None
|
||||
step3: dict[str, Any] | None = None
|
||||
# 乐观锁:客户端基于的草稿版本号。缺省(旧客户端)= 盲写兼容;
|
||||
# 提供时版本不匹配 → 409,body 带 current_version 供重拉合并。
|
||||
expected_version: int | None = Field(default=None, ge=0)
|
||||
|
||||
|
||||
# ── helpers ──────────────────────────────────────────────────────────────
|
||||
@@ -175,7 +181,18 @@ async def get_draft(request: Request, draft_id: str) -> TaskDraftResponse:
|
||||
async def update_draft(request: Request, draft_id: str, body: TaskDraftUpdateRequest) -> TaskDraftResponse:
|
||||
store = _get_store(request)
|
||||
fields = body.model_dump(exclude_unset=True)
|
||||
row = await store.update_draft(draft_id, **fields)
|
||||
try:
|
||||
row = await store.update_draft(draft_id, **fields)
|
||||
except DraftConcurrentWriteError as err:
|
||||
# 版本冲突:detail 里带 current_version,前端据此重拉最新草稿、本地合并后重试。
|
||||
raise HTTPException(
|
||||
status_code=409,
|
||||
detail={
|
||||
"error": "draft_version_conflict",
|
||||
"message": f"草稿已被并发修改(expected={err.expected}, actual={err.actual})",
|
||||
"current_version": err.actual,
|
||||
},
|
||||
) from err
|
||||
if row is None:
|
||||
raise HTTPException(status_code=404, detail="Draft not found")
|
||||
return TaskDraftResponse(**row)
|
||||
|
||||
Reference in new issue
Block a user