feat(roundtable): taskId 会商记录独立存储(不分权) + 第三步生成过程对话/步骤条入库

taskId 深链会商存储独立化:
- 新增独立表 roundtable_task_drafts + 路由 /api/roundtable-task-drafts,只按 task_id
  归属、完全不按 user 分权(created_by 仅审计),与个人 roundtable_drafts 物理分离;
  个人草稿表恢复严格按用户隔离(去掉原 task 放权)。
- 后台研讨作业按 taskId 共享:roundtable_jobs 加 task_id,get/get_by_draft/stream/
  resume/cancel 支持 ?task_id= 跨用户读取与控制,作业报告写回 task-draft 存储。
- 前端:新增 api/task-drafts.ts 客户端,useDraftPersistence / useRoundtableDrafts /
  roundtable-jobs / DispatchChainJobModal / RoundtablePlanningPage 按 taskId 路由读写。
- 迁移 20260618_01 + column_additions_mysql.sql(jobs.task_id) + 数据搬迁脚本
  scripts/migrate_roundtable_task_drafts.py(把历史 task_id 行迁出个人表)。

第三步聊天记录像第二步一样存全:
- 总结报告 / 经典看板 / 大屏 三条流的生成过程对话(中间文字 + 步骤条 + stub)落库,
  加载草稿整段回显,而非只剩最终 md / 流程图 / HTML。DraftStep3Snapshot 扩展
  dialogues/seatStubMessages 槽位;存稿走页面 render 期 ref,恢复走各 hook hydrateSaved
  (旧草稿无字段则回退单条「历史记录」气泡)。

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
fengxiangdebaoziandClaude Opus 4.8 committed 2026-06-18 15:11:50 +08:00
1 parent 500a562bb6
commit a8da6c2632
29 files changed
+1191 -263

No files matched your search

@@ -0,0 +1,79 @@
"""Separate, unpartitioned storage for taskId-deeplink roundtable 聊天记录.
Two changes:
1. New table ``roundtable_task_drafts`` — the dedicated store for roundtable
sessions opened via a taskId deep-link (无界嵌入抽屉). Keyed **only by
``task_id``** (one task → many records), never partitioned by user
(``created_by`` is audit-only). Physically separate from ``roundtable_drafts``
so personal history and task records never mix. (On a brand-new DB the table
is created by ``create_all``; this revision patches existing DBs.)
2. ``roundtable_jobs.task_id`` column — marks a background job as task-scoped so
its progress/dialogues are shared by task (read 不分权) and its final report is
written back to the ``roundtable_task_drafts`` store.
The matching data move (existing ``roundtable_drafts`` rows with a ``task_id`` →
``roundtable_task_drafts``) is a one-off, run via
``scripts/migrate_roundtable_task_drafts.py`` (alembic auto-upgrade is disabled at
runtime — see ``persistence/engine.py``).
Revision ID: 20260618_01
Revises: 20260617_01
Create Date: 2026-06-18
"""
from __future__ import annotations
import sqlalchemy as sa
from alembic import op
from sqlalchemy import inspect
revision = "20260618_01"
down_revision = "20260617_01"
branch_labels = None
depends_on = None
def _has_table(table_name: str) -> bool:
inspector = inspect(op.get_bind())
return table_name in inspector.get_table_names()
def _has_column(table_name: str, column_name: str) -> bool:
if not _has_table(table_name):
return False
inspector = inspect(op.get_bind())
return any(col["name"] == column_name for col in inspector.get_columns(table_name))
def upgrade() -> None:
if not _has_table("roundtable_task_drafts"):
op.create_table(
"roundtable_task_drafts",
sa.Column("id", sa.String(64), primary_key=True),
sa.Column("task_id", sa.String(128), nullable=False),
sa.Column("created_by", sa.String(64), nullable=True),
sa.Column("title", sa.String(512), nullable=False, server_default=""),
sa.Column("furthest_step", sa.Integer, nullable=False, server_default="1"),
sa.Column("step1", sa.Text().with_variant(sa.dialects.mysql.LONGTEXT(), "mysql"), nullable=True),
sa.Column("step2", sa.Text().with_variant(sa.dialects.mysql.LONGTEXT(), "mysql"), nullable=True),
sa.Column("step3", sa.Text().with_variant(sa.dialects.mysql.LONGTEXT(), "mysql"), nullable=True),
sa.Column("created_at", sa.DateTime(), nullable=False),
sa.Column("updated_at", sa.DateTime(), nullable=False),
)
op.create_index("ix_roundtable_task_drafts_task_id", "roundtable_task_drafts", ["task_id"])
if _has_table("roundtable_jobs") and not _has_column("roundtable_jobs", "task_id"):
op.add_column("roundtable_jobs", sa.Column("task_id", sa.String(128), nullable=True))
op.create_index("ix_roundtable_jobs_task_id", "roundtable_jobs", ["task_id"])
def downgrade() -> None:
if _has_column("roundtable_jobs", "task_id"):
op.drop_index("ix_roundtable_jobs_task_id", table_name="roundtable_jobs")
op.drop_column("roundtable_jobs", "task_id")
if _has_table("roundtable_task_drafts"):
op.drop_index("ix_roundtable_task_drafts_task_id", table_name="roundtable_task_drafts")
op.drop_table("roundtable_task_drafts")
@@ -54,6 +54,7 @@ from deerflow.persistence.roundtable_drafts.model import (
RoundtableRecommendHistoryRow,
)
from deerflow.persistence.roundtable_jobs.model import RoundtableJobRow
from deerflow.persistence.roundtable_task_drafts.model import RoundtableTaskDraftRow
from deerflow.persistence.run.model import RunRow
from deerflow.persistence.sensitive_words.model import SensitiveWordRow
from deerflow.persistence.scheduled_tasks.model import (
@@ -106,6 +107,7 @@ __all__ = [
"RoundtableDraftShareRow",
"RoundtableJobRow",
"RoundtableRecommendHistoryRow",
"RoundtableTaskDraftRow",
"RunEventRow",
"RunRow",
"ScheduledTaskDeliveryProfileRow",
@@ -82,39 +82,12 @@ class RoundtableDraftRepository:
async def get_draft(self, draft_id: str, user_id: str) -> dict[str, Any] | None:
async with self._sf() as session:
row = await session.get(RoundtableDraftRow, draft_id)
# task 绑定的草稿共享:放开 user 归属校验(不分权);非 task 草稿仍按 user 隔离。
if row is None or (row.user_id != user_id and not row.task_id):
# 个人草稿严格按 user 隔离。taskId 深链聊天记录已迁出到独立的
# ``roundtable_task_drafts`` 表(不分权),不再走这里。
if row is None or row.user_id != user_id:
return None
return self._draft_to_dict(row)
async def get_latest_draft_by_task(self, task_id: str) -> dict[str, Any] | None:
"""按外部任务 id 取最新一条草稿(**忽略 user_id**,不分权)。无则 None。"""
stmt = (
select(RoundtableDraftRow)
.where(RoundtableDraftRow.task_id == task_id)
.order_by(RoundtableDraftRow.updated_at.desc())
.limit(1)
)
async with self._sf() as session:
result = await session.execute(stmt)
row = result.scalars().first()
return self._draft_to_dict(row) if row is not None else None
async def list_drafts_by_task(self, task_id: str, *, limit: int = 100) -> list[dict[str, Any]]:
"""按外部任务 id 列出全部草稿(轻量 meta,newest first,**忽略 user_id** 不分权)。
taskId 深链模式下历史下拉用:一个 taskId 可有多条会商记录。
"""
stmt = (
select(RoundtableDraftRow)
.where(RoundtableDraftRow.task_id == task_id)
.order_by(RoundtableDraftRow.updated_at.desc())
.limit(limit)
)
async with self._sf() as session:
result = await session.execute(stmt)
return [self._draft_to_meta(row) for row in result.scalars()]
async def create_draft(self, user_id: str, data: dict[str, Any]) -> dict[str, Any]:
now = datetime.now(UTC)
row = RoundtableDraftRow(
@@ -149,8 +122,8 @@ class RoundtableDraftRepository:
) -> dict[str, Any] | None:
async with self._sf() as session:
row = await session.get(RoundtableDraftRow, draft_id)
# task 绑定的草稿共享:放开 user 归属校验(不分权)。
if row is None or (row.user_id != user_id and not row.task_id):
# 个人草稿严格按 user 隔离(task 聊天记录已迁出到独立存储)。
if row is None or row.user_id != user_id:
return None
if task_id is not _UNSET and task_id is not None:
row.task_id = str(task_id)
@@ -172,8 +145,8 @@ class RoundtableDraftRepository:
async def delete_draft(self, draft_id: str, user_id: str) -> bool:
async with self._sf() as session:
row = await session.get(RoundtableDraftRow, draft_id)
# task 绑定的草稿共享:放开 user 归属校验(不分权)。
if row is None or (row.user_id != user_id and not row.task_id):
# 个人草稿严格按 user 隔离(task 聊天记录已迁出到独立存储)。
if row is None or row.user_id != user_id:
return False
await session.delete(row)
await session.commit()
@@ -33,6 +33,10 @@ class RoundtableJobRow(Base):
# The draft/session this job runs for. Indexed for "active job of draft" lookups.
draft_id: Mapped[str | None] = mapped_column(String(64), nullable=True, index=True)
user_id: Mapped[str | None] = mapped_column(String(64), nullable=True, index=True)
# 外部任务 id(taskId 深链会商)。非空 = 这是一条 task 作业:读取按 task_id 共享、
# **不按 user 分权**(任何人打开同一 task 都能看在跑的研讨);其 draft_id 指向独立的
# ``roundtable_task_drafts`` 表。为空 = 普通个人作业(按 user 分权)。
task_id: Mapped[str | None] = mapped_column(String(128), nullable=True, index=True)
# Job status machine (§2.1): queued / running / awaiting_input / done / error / cancelled.
status: Mapped[str] = mapped_column(String(32), nullable=False, default="queued", index=True)
# Fine-grained phase (§2.2): initializing / leader_thinking / dispatching /
@@ -67,6 +67,7 @@ class RoundtableJobRepository:
id: str,
draft_id: str | None = None,
user_id: str | None | _AutoSentinel = AUTO,
task_id: str | None = None,
orchestration_mode: str = "recommend",
coordinator_name: str | None = None,
thread_ids: dict[str, str] | None = None,
@@ -82,6 +83,7 @@ class RoundtableJobRepository:
id=id,
draft_id=draft_id,
user_id=resolved_user_id,
task_id=(task_id or None),
status=status,
phase=phase,
cycle=0,
@@ -167,7 +169,16 @@ class RoundtableJobRepository:
job_id: str,
*,
user_id: str | None | _AutoSentinel = AUTO,
task_id: str | None = None,
) -> dict[str, Any] | None:
# task 作业按 task_id 共享、**不按 user 分权**:给定 task_id 时只校验作业属于该
# task(忽略 user_id)。否则走原「按 user 分权」逻辑。
if task_id is not None:
async with self._sf() as session:
row = await session.get(RoundtableJobRow, job_id)
if row is None or row.task_id != task_id:
return None
return self._row_to_dict(row)
resolved_user_id = resolve_user_id(user_id, method_name="RoundtableJobRepository.get")
async with self._sf() as session:
row = await session.get(RoundtableJobRow, job_id)
@@ -182,12 +193,20 @@ class RoundtableJobRepository:
draft_id: str,
*,
user_id: str | None | _AutoSentinel = AUTO,
task_id: str | None = None,
) -> dict[str, Any] | None:
"""Return the most-recently-updated job for a draft (or None)."""
resolved_user_id = resolve_user_id(user_id, method_name="RoundtableJobRepository.get_by_draft")
"""Return the most-recently-updated job for a draft (or None).
``task_id`` set → task 作业查询:按 ``draft_id`` + ``task_id`` 过滤,**不按 user
分权**(同一 task 的作业跨用户共享)。否则按 user 分权。
"""
stmt = select(RoundtableJobRow).where(RoundtableJobRow.draft_id == draft_id)
if resolved_user_id is not None:
stmt = stmt.where(RoundtableJobRow.user_id == resolved_user_id)
if task_id is not None:
stmt = stmt.where(RoundtableJobRow.task_id == task_id)
else:
resolved_user_id = resolve_user_id(user_id, method_name="RoundtableJobRepository.get_by_draft")
if resolved_user_id is not None:
stmt = stmt.where(RoundtableJobRow.user_id == resolved_user_id)
stmt = stmt.order_by(RoundtableJobRow.updated_at.desc()).limit(1)
async with self._sf() as session:
result = await session.execute(stmt)
@@ -0,0 +1,18 @@
"""Task-scoped roundtable-draft persistence (separate, unpartitioned store)."""
from __future__ import annotations
from deerflow.persistence.roundtable_task_drafts.model import RoundtableTaskDraftRow
from deerflow.persistence.roundtable_task_drafts.sql import RoundtableTaskDraftRepository
__all__ = [
"RoundtableTaskDraftRepository",
"RoundtableTaskDraftRow",
"make_roundtable_task_draft_store",
]
def make_roundtable_task_draft_store(session_factory):
if session_factory is None:
return None
return RoundtableTaskDraftRepository(session_factory)
@@ -0,0 +1,52 @@
"""ORM model for **task-scoped** roundtable-planning drafts.
This is the dedicated, separate store for roundtable 聊天记录 opened through a
taskId deep-link (无界嵌入抽屉). Unlike :mod:`roundtable_drafts` (per-login-user
isolation), task drafts are keyed **purely by ``task_id``** and are **never
partitioned by user** — one ``task_id`` may have many records, all shared across
whoever opens that task. ``created_by`` is audit-only (never used for filtering).
Kept physically separate from ``roundtable_drafts`` so the two never mix: a
user's personal history (``GET /api/roundtable-drafts``) holds only their own
drafts, and task records live entirely here.
``step1`` / ``step2`` / ``step3`` are JSON serialized into ``PortableLongText``
(MySQL ``LONGTEXT``) — the Step 2 transcript / Step 3 report HTML easily exceed
MySQL's 64 KB ``TEXT`` cap — mirroring ``roundtable_drafts``.
"""
from __future__ import annotations
from datetime import UTC, datetime
from sqlalchemy import Integer, String
from sqlalchemy.orm import Mapped, mapped_column
from deerflow.persistence.base import Base
from deerflow.persistence.types import BeijingDateTime, PortableLongText
class RoundtableTaskDraftRow(Base):
__tablename__ = "roundtable_task_drafts"
id: Mapped[str] = mapped_column(String(64), primary_key=True)
# 外部任务 id(无界嵌入抽屉传入)。这是唯一的归属维度:所有读/写/删只按它过滤,
# 一个 task_id 可有多条记录、跨用户共享。必填、建索引。
task_id: Mapped[str] = mapped_column(String(128), nullable=False, index=True)
# 仅审计:建档时的登录用户。绝不参与任何过滤/鉴权(task 记录不分权)。
created_by: Mapped[str | None] = mapped_column(String(64), nullable=True)
# Auto-derived task summary (objective → first user message → timestamp).
title: Mapped[str] = mapped_column(String(512), nullable=False, default="")
# Furthest step reached when this snapshot was taken (1 | 2 | 3).
furthest_step: Mapped[int] = mapped_column(Integer, nullable=False, default=1)
# JSON blobs matching the frontend Step1Snapshot / Step2Snapshot / Step3 shapes.
step1: Mapped[str | None] = mapped_column(PortableLongText, nullable=True) # JSON object
step2: Mapped[str | None] = mapped_column(PortableLongText, nullable=True) # JSON object
step3: Mapped[str | None] = mapped_column(PortableLongText, nullable=True) # JSON object
created_at: Mapped[datetime] = mapped_column(BeijingDateTime(), nullable=False, default=lambda: datetime.now(UTC))
updated_at: Mapped[datetime] = mapped_column(
BeijingDateTime(),
nullable=False,
default=lambda: datetime.now(UTC),
onupdate=lambda: datetime.now(UTC),
)
@@ -0,0 +1,166 @@
"""SQLAlchemy-backed **task-scoped** roundtable-draft store.
Mirrors :mod:`roundtable_drafts` but every read/write/delete is keyed **only by
``task_id`` / row id — never by ``user_id``** (task records are shared, not
partitioned by user). ``created_by`` is stored for audit but never filtered on.
``get_draft`` / ``update_draft`` keep a ``user_id`` positional purely for
signature-compatibility with :class:`RoundtableDraftRepository` (so the job
executor can call either store uniformly); the value is ignored here.
JSON columns (``step1`` / ``step2`` / ``step3``) are stored as serialized strings
in ``PortableLongText`` and round-tripped with ``json.dumps`` / ``json.loads``.
"""
from __future__ import annotations
import json
from datetime import UTC, datetime
from typing import Any
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
from deerflow.persistence.roundtable_task_drafts.model import RoundtableTaskDraftRow
_UNSET = object()
def _loads(value: Any) -> Any:
if not isinstance(value, str):
return value
if not value:
return None
try:
return json.loads(value)
except Exception:
return None
def _dumps(value: Any) -> str | None:
if value is None:
return None
return json.dumps(value, ensure_ascii=False)
class RoundtableTaskDraftRepository:
def __init__(self, session_factory: async_sessionmaker[AsyncSession]) -> None:
self._sf = session_factory
@staticmethod
def _to_meta(row: RoundtableTaskDraftRow) -> dict[str, Any]:
"""Lightweight projection for the history dropdown (no big blobs)."""
return {
"id": row.id,
"task_id": row.task_id,
"title": row.title,
"furthest_step": row.furthest_step,
"created_at": row.created_at.isoformat() if isinstance(row.created_at, datetime) else row.created_at,
"updated_at": row.updated_at.isoformat() if isinstance(row.updated_at, datetime) else row.updated_at,
}
@classmethod
def _to_dict(cls, row: RoundtableTaskDraftRow) -> dict[str, Any]:
d = cls._to_meta(row)
d["step1"] = _loads(row.step1)
d["step2"] = _loads(row.step2)
d["step3"] = _loads(row.step3)
return d
# ── reads (all keyed by task_id / id only) ───────────────────────────────
async def list_drafts_by_task(self, task_id: str, *, limit: int = 100) -> list[dict[str, Any]]:
"""All drafts for a task (lightweight meta, newest first). 不按 user 分权。"""
stmt = (
select(RoundtableTaskDraftRow)
.where(RoundtableTaskDraftRow.task_id == task_id)
.order_by(RoundtableTaskDraftRow.updated_at.desc())
.limit(limit)
)
async with self._sf() as session:
result = await session.execute(stmt)
return [self._to_meta(row) for row in result.scalars()]
async def get_latest_draft_by_task(self, task_id: str) -> dict[str, Any] | None:
"""Newest draft for a task (full), or None."""
stmt = (
select(RoundtableTaskDraftRow)
.where(RoundtableTaskDraftRow.task_id == task_id)
.order_by(RoundtableTaskDraftRow.updated_at.desc())
.limit(1)
)
async with self._sf() as session:
result = await session.execute(stmt)
row = result.scalars().first()
return self._to_dict(row) if row is not None else None
async def get_draft(self, draft_id: str, user_id: str | None = None) -> dict[str, Any] | None:
"""Full draft by id. ``user_id`` is ignored (signature-compat only)."""
async with self._sf() as session:
row = await session.get(RoundtableTaskDraftRow, draft_id)
return self._to_dict(row) if row is not None else None
# ── writes ───────────────────────────────────────────────────────────────
async def create_draft(self, data: dict[str, Any], *, created_by: str | None = None) -> dict[str, Any]:
task_id = (data.get("task_id") or "").strip()
if not task_id:
raise ValueError("task_id is required for a task draft")
now = datetime.now(UTC)
row = RoundtableTaskDraftRow(
id=data["id"],
task_id=task_id,
created_by=(created_by or None),
title=(data.get("title") or "").strip(),
furthest_step=int(data.get("furthest_step") or 1),
step1=_dumps(data.get("step1")),
step2=_dumps(data.get("step2")),
step3=_dumps(data.get("step3")),
created_at=now,
updated_at=now,
)
async with self._sf() as session:
session.add(row)
await session.commit()
await session.refresh(row)
return self._to_dict(row)
async def update_draft(
self,
draft_id: str,
user_id: str | None = None,
*,
title=_UNSET,
furthest_step=_UNSET,
step1=_UNSET,
step2=_UNSET,
step3=_UNSET,
) -> dict[str, Any] | None:
"""Partial update by id. ``user_id`` is ignored (signature-compat only)."""
async with self._sf() as session:
row = await session.get(RoundtableTaskDraftRow, draft_id)
if row is None:
return None
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()
await session.refresh(row)
return self._to_dict(row)
async def delete_draft(self, draft_id: str) -> bool:
async with self._sf() as session:
row = await session.get(RoundtableTaskDraftRow, draft_id)
if row is None:
return False
await session.delete(row)
await session.commit()
return True