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

@@ -189,6 +189,11 @@ export interface DraftMeta {
/** Auto-derived task summary (objective → first user message → timestamp). */
title: string;
furthestStep: 1 | 2 | 3;
/**
* 乐观锁版本号:写回时经 `expectedVersion` 回传;并发冲突(409)时用它识别
* 本地副本已过期,重拉最新草稿合并后重试。
*/
version: number;
/**
* 本次会商所用业务链名(历史卡片展示)。无业务链(自由/recommend 模式)→ null/undefined,
* 由前端展示为「自由模式」。后端从 step2 快照里抽取(见 roundtable_drafts/sql.py)。
@@ -234,6 +239,33 @@ export interface DraftUpdatePayload {
step1?: DraftStep1Snapshot;
step2?: DraftStep2Snapshot;
step3?: DraftStep3Snapshot | null;
/**
* 本地副本基于的草稿版本号(乐观锁)。提供时服务端做原子条件 UPDATE;
* 版本不匹配 → 409(`DraftVersionConflictError`),调用方应重拉合并后重试。
* 省略 = 盲写(向后兼容旧调用点,不推荐)。
*/
expectedVersion?: number;
}
/**
* 乐观锁冲突(HTTP 409):服务端草稿版本比本地副本新。
* `currentVersion` 是服务端最新版本,调用方应重拉最新草稿、合并本地改动后用
* 新版本号重试——一致性来自 DB,不靠前端运气。
*/
export class DraftVersionConflictError extends Error {
readonly draftId: string;
readonly currentVersion: number | null;
constructor(draftId: string, currentVersion: number | null) {
super(`Draft ${draftId} was modified concurrently (server version ${currentVersion})`);
this.name = "DraftVersionConflictError";
this.draftId = draftId;
this.currentVersion = currentVersion;
}
}
export function isDraftVersionConflictError(error: unknown): error is DraftVersionConflictError {
return error instanceof DraftVersionConflictError;
}
const BASE = () => `${getBackendBaseURL()}/api/roundtable-drafts`;
@@ -256,6 +288,7 @@ export interface ServerDraftMeta {
task_id?: string | null;
title: string;
furthest_step: number;
version?: number;
chain_title?: string | null;
created_at: string | null;
updated_at: string | null;
@@ -276,6 +309,7 @@ export function toMeta(s: ServerDraftMeta): DraftMeta {
taskId: s.task_id ?? null,
title: s.title ?? "",
furthestStep: step,
version: s.version ?? 0,
chainTitle: s.chain_title ?? null,
createdAt: s.created_at ?? "",
updatedAt: s.updated_at ?? "",
@@ -313,6 +347,7 @@ export function fromUpdate(p: DraftUpdatePayload) {
if (p.step1 !== undefined) out.step1 = p.step1;
if (p.step2 !== undefined) out.step2 = p.step2;
if (p.step3 !== undefined) out.step3 = p.step3;
if (p.expectedVersion !== undefined) out.expected_version = p.expectedVersion;
return out;
}
@@ -366,6 +401,23 @@ export async function createDraft(payload: DraftCreatePayload): Promise<DraftRec
return record;
}
/**
* 把 409 乐观锁冲突转成类型化错误(解析服务端最新版本号)。个人与任务草稿客户端共用,
* 保证两条 PUT 路径的冲突语义完全一致。非 409 原样放行。
*/
export async function throwIfDraftConflict(res: Response, id: string): Promise<void> {
if (res.status !== 409) return;
const raw = await res.text().catch(() => "");
let currentVersion: number | null = null;
try {
const parsed = JSON.parse(raw) as { detail?: { current_version?: number } };
currentVersion = parsed?.detail?.current_version ?? null;
} catch {
currentVersion = null;
}
throw new DraftVersionConflictError(id, currentVersion);
}
export async function updateDraft(id: string, patch: DraftUpdatePayload): Promise<DraftRecord | null> {
const res = await apiFetch(`${BASE()}/${encodeURIComponent(id)}`, {
method: "PUT",
@@ -373,6 +425,7 @@ export async function updateDraft(id: string, patch: DraftUpdatePayload): Promis
body: JSON.stringify(fromUpdate(patch)),
});
if (res.status === 404) return null;
await throwIfDraftConflict(res, id);
const data = await readJson<ServerDraftFull>(res, "update draft");
const record = toRecord(data);
writeYLog({
@@ -190,6 +190,12 @@ export interface StreamIntentRequest {
model?: string;
/** Explicit built-in Step-1 agent. Omit to retain the multi-agent default. */
intentAgentId?: string;
/**
* 本回合幂等键(Phase 4 单飞):同一 (threadId, clientTurnId) 在后端至多一个 run。
* 缺省由调用方(useStep1Intent)每条消息生成;同一回合的重试 / 双击复用它,后端据此
* 去重(在跑→丢弃重复请求 / 已完成→幂等回放),杜绝并发下双发意图 / 重复澄清卡。
*/
clientTurnId?: string;
/** Optional hidden business/task context injected into the model run. */
taskContext?: Record<string, unknown> | null;
/** Step 1 用户上传的参考文件(可选)。 */
@@ -403,6 +409,8 @@ export async function streamIntent(req: StreamIntentRequest): Promise<StreamInte
model: req.model,
intent_agent_id: req.intentAgentId || undefined,
task_context: req.taskContext ?? undefined,
// Phase 4 单飞幂等键:缺省由后端兜底生成,但调用方应始终传(useStep1Intent 每条消息生成)。
client_turn_id: req.clientTurnId || undefined,
// 仅在有文件时带上,空数组后端也安全忽略。
files: req.files && req.files.length > 0 ? req.files : undefined,
}),
@@ -11,6 +11,8 @@
* 增量事件归并。
*/
import { nanoid } from "nanoid";
import { apiFetch } from "@/core/api/fetch-client";
import { getBackendBaseURL } from "@/core/config";
import { OptType, writeYLog } from "@/core/log/y-log";
@@ -87,6 +89,9 @@ function toStartBody(p: StartJobPayload) {
// 缺省(未选模式)时不带这三字段 → 后端按 policy 默认(≈pro),向后兼容。
const seat = p.seatMode ? seatModeToRunParams(p.seatMode) : null;
return {
// 幂等请求 id:缺省现生成;「同一草稿至多一个活跃作业」由后端作用域唯一键保证,
// requestId 仅作审计/排查线索(重试时复用可让后端识别「同 id 但入参变了」的异常重试)。
requestId: p.requestId ?? nanoid(),
draftId: p.draftId ?? null,
// taskId 非空 → task 作业(按 task 共享、不分权,报告写回独立 task-draft 存储)。
...(p.taskId ? { taskId: p.taskId } : {}),
@@ -18,6 +18,7 @@ import {
fromCreate,
fromUpdate,
readJson,
throwIfDraftConflict,
toMeta,
toRecord,
type DraftCreatePayload,
@@ -103,6 +104,7 @@ export async function updateTaskDraft(id: string, patch: DraftUpdatePayload): Pr
body: JSON.stringify(fromUpdate(patch)),
});
if (res.status === 404) return null;
await throwIfDraftConflict(res, id);
const data = await readJson<ServerDraftFull>(res, "update task draft");
const record = toRecord(data);
writeYLog({