feat: 深度研究支持独立新报告与原文改写,并完善沙箱、模型管理与全文改写

新报告不再覆盖当前文件卡片,写入时打开沙箱;步骤条仅在改写原文时位于卡片上方。同时补齐改写本地回退、模型管理与会商第三步相关改动。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
董宏凯andCursor committed 2026-08-21 21:46:04 +08:00
1 parent 11f928bc95
commit a5a9c952e8
63 files changed
+4113 -929

No files matched your search

@@ -42,6 +42,7 @@ from deerflow.agents.deep_research.types import (
AdapterBundle,
DeepResearchRequest,
DeepResearchResult,
ResearchMaterial,
)
from deerflow.runtime.user_context import reset_current_user, set_current_user
from deerflow.utils.stream_text import InlineThinkTagFilter
@@ -361,7 +362,6 @@ class DeepResearchJobExecutor:
from app.gateway.deep_research_report_rewrite import (
heading_count,
stream_full_report_rewrite,
stream_full_report_rewrite_requirements,
validate_markdown,
visible_char_count,
)
@@ -374,6 +374,7 @@ class DeepResearchJobExecutor:
report = str(snapshot.get("report") or "")
instruction = str(snapshot.get("instruction") or "").strip()
operation = "generate" if snapshot.get("operation") == "generate" else "rewrite"
style = str(snapshot.get("style") or "formal_analysis").strip()
model_name = str(snapshot.get("model_name") or "").strip() or None
sources_raw = snapshot.get("sources")
@@ -381,6 +382,7 @@ class DeepResearchJobExecutor:
session_snapshot = snapshot.get("session")
session_for_model = dict(session_snapshot) if isinstance(session_snapshot, dict) else {}
session_for_model.setdefault("config_snapshot", snapshot.get("config") or {})
session_for_model.setdefault("query", snapshot.get("query") or "")
if not report.strip() or not instruction:
await self._mark_document_rewrite_failed(job_id, session_id, lease_owner, "INVALID_SNAPSHOT", "改写任务快照不完整,无法继续执行。")
@@ -413,70 +415,36 @@ class DeepResearchJobExecutor:
payload={"type": "requirements_started"},
)
await self._job_store.update_progress(job_id, lease_owner=lease_owner, phase="planning", progress=20)
requirement_parts: list[str] = []
# Persist the first fragment too: the HTTP client opens its SSE stream
# just after queuing the job, so this closes the tiny subscribe race
# without storing every token in the durable event log.
last_requirements_checkpoint = 0.0
async def on_requirement_delta(delta: str) -> None:
nonlocal last_requirements_checkpoint
if not delta:
return
requirement_parts.append(delta)
requirement_draft = "".join(requirement_parts)
await events.publish_live(
"document_rewrite",
phase="planning",
payload={"type": "requirements_delta", "delta": delta},
)
if time.monotonic() - last_requirements_checkpoint >= 0.8:
await events.emit(
"document_rewrite",
phase="planning",
payload={"type": "requirements_checkpoint", "content": requirement_draft},
)
last_requirements_checkpoint = time.monotonic()
async def on_requirement_thinking(delta: str) -> None:
if delta:
await events.publish_live(
"document_rewrite",
phase="planning",
payload={"type": "thinking", "chunk": delta},
)
requirements_fallback = False
try:
requirements_plan = await stream_full_report_rewrite_requirements(
session=session_for_model,
report=report,
instruction=instruction,
style=style_instruction,
model_name=model_name,
on_delta=on_requirement_delta,
on_reasoning=on_requirement_thinking,
)
except asyncio.CancelledError:
raise
except Exception: # noqa: BLE001
logger.exception("deep-research report rewrite requirements planning failed for %s", job_id)
requirements_fallback = True
requirements_plan = ""
requirements_plan = requirements_plan.strip()[:3_000]
if not requirements_plan:
requirements_fallback = True
# Requirement planning used to make a second full model request before
# the writer could start. Thinking-capable providers can spend a long
# time on that request without yielding visible text, then the actual
# writer makes another equally long request. The inputs are already
# structured (user instruction + selected style), so build the stable
# plan locally and publish it immediately. The sole model stream now
# writes the report straight away while still receiving this exact plan
# as a constraint.
if operation == "generate":
cleaned_instruction = " ".join(instruction.split())[:2_400]
requirements_plan = "\n".join(
[
f"- 生成目标:{cleaned_instruction}",
f"- 表达策略:{style_instruction}",
"- 结构原则:严格按用户选择的新报告结构组织全文,不沿用来源报告章节。",
"- 资料边界:仅使用本次已选资料,不联网,不补充没有依据的事实、数字或引用。",
]
)[:3_000]
else:
requirements_plan = build_fallback_rewrite_requirements_plan(
instruction=instruction,
style_instruction=style_instruction,
)
)[:3_000]
await events.emit(
"document_rewrite",
phase="planning",
payload={
"type": "requirements_completed",
"content": requirements_plan,
"fallback": requirements_fallback,
"fallback": False,
},
)
await events.emit(
@@ -544,6 +512,7 @@ class DeepResearchJobExecutor:
on_delta=on_delta,
on_reasoning=on_thinking,
requirements_plan=requirements_plan,
operation=operation,
)
except asyncio.CancelledError:
raise
@@ -618,6 +587,7 @@ class DeepResearchJobExecutor:
return
version_id: str | None = None
version_snapshot_failed = False
if self._document_rewrite_version_store is not None:
try:
version_id = f"drv_{_uuid.uuid4().hex}"
@@ -633,35 +603,26 @@ class DeepResearchJobExecutor:
instruction=instruction,
model_name=model_name,
)
except Exception as exc: # noqa: BLE001
# The report must never stay changed without the promised undo
# snapshot. Restore only if another edit did not win first.
rolled_back = await self._session_store.replace_report_if_unchanged(
session_id,
user_id=user_id or None,
expected_report=rewritten,
report_markdown=report,
)
message = "保存改写版本失败,报告已恢复原文。" if rolled_back is not None else ("保存改写版本失败,且报告随后被修改,未再覆盖。")
await self._mark_document_rewrite_failed(
job_id,
session_id,
lease_owner,
type(exc).__name__,
message,
events=events,
stage="commit",
)
return
try:
await self._document_rewrite_version_store.prune_source(
user_id=user_id or None,
source_type="deep_research_report",
source_id=session_id,
source_path="report.md",
)
except Exception: # noqa: BLE001
logger.exception("could not prune deep-research rewrite versions for %s", session_id)
# The document commit already succeeded. A best-effort undo
# snapshot must never make the report disappear again; record
# the operational failure and let the user continue working.
logger.exception(
"could not persist deep-research rewrite version for %s; keeping rewritten report",
session_id,
)
version_id = None
version_snapshot_failed = True
else:
try:
await self._document_rewrite_version_store.prune_source(
user_id=user_id or None,
source_type="deep_research_report",
source_id=session_id,
source_path="report.md",
)
except Exception: # noqa: BLE001
logger.exception("could not prune deep-research rewrite versions for %s", session_id)
result_snapshot = {
"kind": "document_rewrite",
@@ -669,6 +630,7 @@ class DeepResearchJobExecutor:
"content": rewritten,
"committedHash": committed_hash,
"versionId": version_id,
"versionSnapshotFailed": version_snapshot_failed,
"summary": summary,
"requirementsPlan": requirements_plan,
}
@@ -680,12 +642,18 @@ class DeepResearchJobExecutor:
"content": rewritten,
"committedHash": committed_hash,
"versionId": version_id,
"versionSnapshotFailed": version_snapshot_failed,
},
)
await events.emit(
"document_rewrite",
phase="done",
payload={"type": "done", "committedHash": committed_hash, "versionId": version_id},
payload={
"type": "done",
"committedHash": committed_hash,
"versionId": version_id,
"versionSnapshotFailed": version_snapshot_failed,
},
)
# Mark terminal only after both terminal UI events are durable. The
# stream endpoint stops polling on a terminal job, so reversing this
@@ -1229,6 +1197,7 @@ class DeepResearchJobExecutor:
try:
await asyncio.to_thread(atomic_write_text, actual_path, rewritten)
version_id: str | None = None
version_snapshot_failed = False
if self._document_rewrite_version_store is not None:
try:
version_id = f"drv_{_uuid.uuid4().hex}"
@@ -1244,20 +1213,25 @@ class DeepResearchJobExecutor:
instruction=instruction,
model_name=resolved_name,
)
except Exception:
current_after_write = await asyncio.to_thread(actual_path.read_text, encoding="utf-8")
if content_hash(current_after_write) == committed_hash:
await asyncio.to_thread(atomic_write_text, actual_path, original)
raise
try:
await self._document_rewrite_version_store.prune_source(
user_id=user_id or None,
source_type="artifact",
source_id=thread_id,
source_path=virtual_path,
)
except Exception: # noqa: BLE001
logger.exception("could not prune artifact rewrite versions for %s", actual_path)
# The file is already committed. Keep it available even
# if its optional undo snapshot cannot be persisted.
logger.exception(
"could not persist artifact rewrite version for %s; keeping rewritten file",
actual_path,
)
version_id = None
version_snapshot_failed = True
else:
try:
await self._document_rewrite_version_store.prune_source(
user_id=user_id or None,
source_type="artifact",
source_id=thread_id,
source_path=virtual_path,
)
except Exception: # noqa: BLE001
logger.exception("could not prune artifact rewrite versions for %s", actual_path)
except Exception as exc: # noqa: BLE001
logger.exception("artifact document rewrite %s commit failed", job_id)
await fail(type(exc).__name__, str(exc), stage="commit")
@@ -1270,6 +1244,7 @@ class DeepResearchJobExecutor:
"originalContent": original,
"committedHash": committed_hash,
"versionId": version_id,
"versionSnapshotFailed": version_snapshot_failed,
"summary": summary,
"requirementsPlan": requirements_plan,
}
@@ -1281,12 +1256,18 @@ class DeepResearchJobExecutor:
"content": rewritten,
"committedHash": committed_hash,
"versionId": version_id,
"versionSnapshotFailed": version_snapshot_failed,
},
)
await events.emit(
"document_rewrite",
phase="done",
payload={"type": "done", "committedHash": committed_hash, "versionId": version_id},
payload={
"type": "done",
"committedHash": committed_hash,
"versionId": version_id,
"versionSnapshotFailed": version_snapshot_failed,
},
)
await self._job_store.update_progress(
job_id,
@@ -1383,6 +1364,13 @@ class DeepResearchJobExecutor:
events=recording_sink,
cancellation=cancel_token,
)
elif entry == "regenerate":
material_provider = await self._build_selected_materials(
session_id=session_id,
user_id=user_id,
query=query,
cancellation=cancel_token,
)
else:
material_provider = DeerFlowMaterialProvider(
user_id=user_id,
@@ -1420,7 +1408,7 @@ class DeepResearchJobExecutor:
result = await engine.run(request, adapters=bundle, cancellation=cancel_token)
# Chat entry extra: a short model-written digest shown under the report.
if entry == "chat" and result.report_markdown:
if entry in ("chat", "regenerate") and result.report_markdown:
await self._generate_report_summary(
llm=llm_backend,
report_markdown=result.report_markdown,
@@ -1432,6 +1420,56 @@ class DeepResearchJobExecutor:
result = result.model_copy(update={"usage": llm_backend.usage_totals})
return result
async def _build_selected_materials(
self,
*,
session_id: str,
user_id: str,
query: str,
cancellation: CancellationToken,
):
"""Serve a report variant's cloned evidence through the normal engine."""
from deerflow.agents.deep_research.collection.thread_harvester import (
StaticHarvestMaterialProvider,
)
cancellation.raise_if_cancelled()
if self._source_store is None:
raise RuntimeError("Deep-research source store is unavailable")
rows = await self._source_store.list_by_session(
session_id,
user_id=user_id,
selected=True,
limit=500,
include_content=True,
)
allowed_types = {"web", "knowledge", "skill", "mcp", "file", "other"}
materials = [
ResearchMaterial(
id=str(row.get("id") or ""),
query=str(row.get("first_seen_query") or query),
title=str(row.get("title") or ""),
url=row.get("url"),
raw_content=str(row.get("raw_content") or row.get("snippet") or ""),
snippet=row.get("snippet"),
source=str(row.get("source") or ""),
source_type=(
str(row.get("source_type"))
if str(row.get("source_type")) in allowed_types
else "other"
),
published_at=row.get("published_at"),
relevance_score=row.get("relevance_score"),
rec_uuid=row.get("rec_uuid"),
content_hash=str(row.get("content_hash") or ""),
)
for row in rows
if row.get("id")
]
if not materials:
raise RuntimeError("No selected source material is available for regeneration")
return StaticHarvestMaterialProvider(materials)
async def _build_chat_materials(
self,
*,
@@ -1511,6 +1549,12 @@ class DeepResearchJobExecutor:
async def on_delta(delta: str) -> None:
await events.publish_live("summary_delta", phase="summarizing", payload={"delta": delta})
async def on_reasoning(delta: str) -> None:
# Live-only: reasoning models often think for a long time before the
# first digest token. Dropping that stream left the UI blank after
# the report file was already written.
await events.publish_live("summary_thinking", phase="summarizing", payload={"delta": delta})
lang_note = "中文" if str(language).startswith("zh") else "the report's language"
try:
result = await llm.stream_complete(
@@ -1527,6 +1571,7 @@ class DeepResearchJobExecutor:
},
],
on_delta=on_delta,
on_reasoning=on_reasoning,
)
except ResearchCancelled:
raise