增强 Deep Research 可靠性,并接入 8BF 任务卡片与会商上下文。

修复 MySQL 读写分离下的持久化与 SSE 重连问题,报告完成前先落库;8BF 嵌入页支持 taskId 任务卡与独立意图 metadata;前端统一 createClientId 并更新 runtime 配置。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
董宏凯andCursor committed 2026-08-17 18:16:05 +08:00
1 parent 47faa9bcdf
commit 771957429a
36 files changed
+2038 -300

No files matched your search

@@ -61,6 +61,8 @@ WORKER_ID = f"w-{os.getpid()}-{_uuid.uuid4().hex[:8]}"
_LEASE_TTL_SECONDS = 60
_HEARTBEAT_INTERVAL = 20.0
_CANCEL_POLL_INTERVAL = 5.0
_REPORT_PROJECTION_RETRY_ATTEMPTS = 3
_REPORT_PROJECTION_RETRY_DELAY_SECONDS = 0.5
# Phase → progress% mapping for the job timeline.
_PHASE_PROGRESS: dict[str, int] = {
@@ -1372,7 +1374,17 @@ class DeepResearchJobExecutor:
async def _mark_completed(
self, job_id: str, session_id: str, lease_owner: str | None, result: DeepResearchResult
) -> None:
# Job row → completed.
# Persist the report projection before exposing a terminal completed
# job. The session row is the durable report itself; marking the job
# complete first and suppressing a session-write failure can otherwise
# make a successfully generated report disappear after refresh.
await self._persist_completed_report_projection(
job_id=job_id,
session_id=session_id,
result=result,
)
# Job row → completed only after the report is durable.
await self._job_store.update_progress(
job_id,
lease_owner=lease_owner,
@@ -1385,26 +1397,62 @@ class DeepResearchJobExecutor:
"plan": result.plan,
},
)
# Session row → completed + report.
source_count = len(result.source_ids)
with suppress(Exception): # noqa: BLE001
await self._session_store.update(
session_id,
status="completed",
report_markdown=result.report_markdown,
report_html=result.report_html,
plan_snapshot=result.plan,
# Keep the terminal job id alongside model usage. The
# frontend uses it only to replay the persisted research trace
# after a refresh; it does not affect billing or job state.
usage_snapshot={**(result.usage or {}), "lastJobId": job_id},
source_count=source_count,
active_job_id=None,
)
# Emit a terminal event for SSE consumers.
with suppress(Exception): # noqa: BLE001
await self._emit_terminal(job_id, session_id, "job_completed")
async def _persist_completed_report_projection(
self,
*,
job_id: str,
session_id: str,
result: DeepResearchResult,
) -> None:
"""Durably save the report before publishing job completion.
A write can succeed while a short-lived MySQL proxy/readback hiccup
makes its caller fail. Retry the full owner-scoped update a few times;
if it still cannot be confirmed, propagate the error so ``_run`` marks
the job failed instead of advertising a report that cannot be reopened.
"""
last_error: Exception | None = None
for attempt in range(1, _REPORT_PROJECTION_RETRY_ATTEMPTS + 1):
try:
updated = await self._session_store.update(
session_id,
status="completed",
report_markdown=result.report_markdown,
report_html=result.report_html,
plan_snapshot=result.plan,
# Keep the terminal job id alongside model usage. The
# frontend uses it only to replay the persisted research
# trace after a refresh; it does not affect billing.
usage_snapshot={**(result.usage or {}), "lastJobId": job_id},
source_count=len(result.source_ids),
active_job_id=None,
)
if updated is not None:
return
last_error = RuntimeError(f"Deep Research session {session_id} no longer exists")
except asyncio.CancelledError:
raise
except Exception as exc: # noqa: BLE001
last_error = exc
if attempt < _REPORT_PROJECTION_RETRY_ATTEMPTS:
logger.warning(
"deep-research report projection failed for %s (attempt %s/%s); retrying",
job_id,
attempt,
_REPORT_PROJECTION_RETRY_ATTEMPTS,
exc_info=True,
)
await asyncio.sleep(_REPORT_PROJECTION_RETRY_DELAY_SECONDS * attempt)
raise RuntimeError(
f"研究报告未能保存到会话 {session_id},任务不会标记为完成"
) from last_error
async def _mark_awaiting(
self, job_id: str, session_id: str, pause: ResearchAwaitingInput
) -> None: