feat: 岗位会商命令幂等与圆桌后台作业租约调度

岗位会商写操作引入 command_id 幂等与版本锁,前端支持同键重试;圆桌后台作业改为 DB 租约 dispatcher、去重与草稿 outbox 补偿,并持久化 intent turns。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
董宏凯andCursor committed 2026-08-14 11:08:12 +08:00
1 parent 4f536d9b39
commit 75963c9ad9
51 files changed
+4449 -670

No files matched your search

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