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
@@ -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