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
+70
-17
@@ -15,7 +15,7 @@ import re
|
||||
from datetime import UTC, datetime
|
||||
from typing import Any
|
||||
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy import case, select, update
|
||||
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
|
||||
|
||||
from deerflow.persistence.roundtable_drafts.model import (
|
||||
@@ -66,6 +66,23 @@ def _dumps(value: Any) -> str | None:
|
||||
return json.dumps(value, ensure_ascii=False)
|
||||
|
||||
|
||||
class DraftConcurrentWriteError(Exception):
|
||||
"""乐观锁冲突:``update_draft(expected_version=...)`` 时版本号不匹配。
|
||||
|
||||
由后台作业 ``_write_run_to_draft`` 的读-改-写重试循环捕获:冲突时重新读取最新草稿、
|
||||
重新合并 ``step2.runs[]``、再试。防多用户/多 worker 并发写同一草稿时后写覆盖先写。
|
||||
"""
|
||||
|
||||
def __init__(self, draft_id: str, *, expected: int | None = None, actual: int | None = None) -> None:
|
||||
self.draft_id = draft_id
|
||||
self.expected = expected
|
||||
self.actual = actual
|
||||
super().__init__(
|
||||
f"concurrent write detected on roundtable draft {draft_id} "
|
||||
f"(expected version {expected}, got {actual})"
|
||||
)
|
||||
|
||||
|
||||
class RoundtableDraftRepository:
|
||||
def __init__(self, session_factory: async_sessionmaker[AsyncSession]) -> None:
|
||||
self._sf = session_factory
|
||||
@@ -80,6 +97,8 @@ class RoundtableDraftRepository:
|
||||
"task_id": row.task_id,
|
||||
"title": row.title,
|
||||
"furthest_step": row.furthest_step,
|
||||
# 乐观锁版本号:列表也携带,供前端本地副本对账(冲突时重拉合并)。
|
||||
"version": row.version,
|
||||
# 所用业务链名(历史卡片展示用);无链条 → None → 前端显示「自由模式」。
|
||||
"chain_title": _extract_chain_title(row.step2),
|
||||
"created_at": row.created_at.isoformat() if isinstance(row.created_at, datetime) else row.created_at,
|
||||
@@ -145,26 +164,60 @@ class RoundtableDraftRepository:
|
||||
step2=_UNSET,
|
||||
step3=_UNSET,
|
||||
task_id=_UNSET,
|
||||
expected_version: int | None = None,
|
||||
) -> dict[str, Any] | None:
|
||||
async with self._sf() as session:
|
||||
row = await session.get(RoundtableDraftRow, draft_id)
|
||||
"""原子条件 UPDATE:版本判定与字段写入发生在**同一条 SQL** 里。
|
||||
|
||||
一致性只来自数据库:``UPDATE ... WHERE id AND user_id AND version=`` 配合
|
||||
rowcount 判定,多 worker 并发下不存在「读-改-写」的检查后竞争窗口。
|
||||
``rowcount == 0`` 时再回读区分「不存在/非本人」(返回 None)与
|
||||
「版本冲突」(抛 :class:`DraftConcurrentWriteError`)。
|
||||
"""
|
||||
values: dict[str, Any] = {
|
||||
"version": RoundtableDraftRow.version + 1,
|
||||
"updated_at": datetime.now(UTC),
|
||||
}
|
||||
if task_id is not _UNSET and task_id is not None:
|
||||
values["task_id"] = str(task_id)
|
||||
if title is not _UNSET and title is not None:
|
||||
values["title"] = str(title).strip()
|
||||
if furthest_step is not _UNSET and furthest_step is not None:
|
||||
# furthest_step 单调不回退:迟到的自动保存不得把已推进的步骤拉回去。
|
||||
step_value = int(furthest_step)
|
||||
values["furthest_step"] = case(
|
||||
(RoundtableDraftRow.furthest_step >= step_value, RoundtableDraftRow.furthest_step),
|
||||
else_=step_value,
|
||||
)
|
||||
if step1 is not _UNSET:
|
||||
values["step1"] = _dumps(step1)
|
||||
if step2 is not _UNSET:
|
||||
values["step2"] = _dumps(step2)
|
||||
if step3 is not _UNSET:
|
||||
values["step3"] = _dumps(step3)
|
||||
|
||||
where = [
|
||||
RoundtableDraftRow.id == draft_id,
|
||||
# 个人草稿严格按 user 隔离(task 聊天记录已迁出到独立存储)。
|
||||
if row is None or row.user_id != user_id:
|
||||
RoundtableDraftRow.user_id == user_id,
|
||||
]
|
||||
if expected_version is not None:
|
||||
where.append(RoundtableDraftRow.version == expected_version)
|
||||
|
||||
async with self._sf() as session:
|
||||
result = await session.execute(
|
||||
update(RoundtableDraftRow).where(*where).values(**values)
|
||||
)
|
||||
if result.rowcount == 0:
|
||||
if expected_version is not None:
|
||||
# 回读区分冲突与不存在(回读在冲突分支,正常路径零额外开销)。
|
||||
existing = await session.get(RoundtableDraftRow, draft_id)
|
||||
if existing is not None and existing.user_id == user_id:
|
||||
raise DraftConcurrentWriteError(
|
||||
draft_id, expected=expected_version, actual=existing.version
|
||||
)
|
||||
return None
|
||||
if task_id is not _UNSET and task_id is not None:
|
||||
row.task_id = str(task_id)
|
||||
if title is not _UNSET and title is not None:
|
||||
row.title = str(title).strip()
|
||||
if furthest_step is not _UNSET and furthest_step is not None:
|
||||
row.furthest_step = int(furthest_step)
|
||||
if step1 is not _UNSET:
|
||||
row.step1 = _dumps(step1)
|
||||
if step2 is not _UNSET:
|
||||
row.step2 = _dumps(step2)
|
||||
if step3 is not _UNSET:
|
||||
row.step3 = _dumps(step3)
|
||||
row.updated_at = datetime.now(UTC)
|
||||
await session.commit()
|
||||
row = await session.get(RoundtableDraftRow, draft_id)
|
||||
await session.refresh(row)
|
||||
return self._draft_to_dict(row)
|
||||
|
||||
|
||||
Reference in new issue
Block a user