feat: 新增报告协作页,并强化 Deep Research 运行诊断与报告兜底
支持浏览器侧素材快照撰写、拒绝报告读时修复、Mermaid PNG 导出与 Word 导出加固。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
65d6b61d5d
commit
f109a95535
65 files changed
+10342
-245
No files matched your search
@@ -22,8 +22,10 @@ full result without extra joins.
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import hashlib
|
||||
import logging
|
||||
import os
|
||||
import re
|
||||
import time
|
||||
import uuid as _uuid
|
||||
from collections.abc import Awaitable, Callable
|
||||
@@ -59,6 +61,7 @@ logger = logging.getLogger(__name__)
|
||||
# ── constants ───────────────────────────────────────────────────────────────
|
||||
|
||||
WORKER_ID = f"w-{os.getpid()}-{_uuid.uuid4().hex[:8]}"
|
||||
EXECUTOR_REVISION = "deep-research-browser-db-checkpoint-20260903-v6"
|
||||
|
||||
_LEASE_TTL_SECONDS = 60
|
||||
_HEARTBEAT_INTERVAL = 20.0
|
||||
@@ -66,6 +69,86 @@ _CANCEL_POLL_INTERVAL = 5.0
|
||||
_REPORT_PROJECTION_RETRY_ATTEMPTS = 3
|
||||
_REPORT_PROJECTION_RETRY_DELAY_SECONDS = 0.5
|
||||
|
||||
# This validation deliberately lives in the executor, not in an individual
|
||||
# runner. Every mode and every entry must pass this gate immediately before a
|
||||
# session can be marked completed. Keep the semantic spans generous: the
|
||||
# production refusal often contains a long assessment between “资料不足” and
|
||||
# “无法生成报告”, so a short literal-marker check is not sufficient.
|
||||
_TERMINAL_REPORT_REFUSAL_PATTERNS = (
|
||||
re.compile(
|
||||
r"(?:无法|不能|未能|不可以|不宜|难以|不具备条件|不足以)"
|
||||
r".{0,120}?(?:生成|撰写|产出|输出|完成|提供).{0,40}?(?:研究)?报告",
|
||||
re.S,
|
||||
),
|
||||
re.compile(
|
||||
r"(?:未收集到|未找到|没有|缺少|缺乏|不足).{0,120}?(?:资料|材料|信息|来源|证据)"
|
||||
r".{0,180}?(?:无法|不能|未能|不可以|不宜|难以).{0,80}?(?:生成|撰写|产出|输出|完成|提供).{0,40}?(?:研究)?报告",
|
||||
re.S,
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
def _terminal_report_validation_error(report: str | None) -> str | None:
|
||||
"""Return why a model response cannot be persisted as a completed report."""
|
||||
text = (report or "").strip()
|
||||
compact = re.sub(r"\s+", "", text)
|
||||
if not compact:
|
||||
return "empty_report"
|
||||
headings = re.findall(r"(?m)^#{1,6}\s+\S+", text)
|
||||
# A real report may quote or discuss a refusal/material-gap sentence in a
|
||||
# limitations section. Only replace short or unstructured refusal output;
|
||||
# a substantive multi-section report is accepted unchanged.
|
||||
if (
|
||||
(len(compact) < 800 or len(headings) < 2)
|
||||
and any(pattern.search(compact) for pattern in _TERMINAL_REPORT_REFUSAL_PATTERNS)
|
||||
):
|
||||
return "refusal_semantics"
|
||||
return None
|
||||
|
||||
|
||||
def _materials_from_browser_snapshot(
|
||||
values: list[dict[str, Any]] | None,
|
||||
*,
|
||||
query: str,
|
||||
) -> list[ResearchMaterial]:
|
||||
"""Validate and bound the evidence frozen by the browser."""
|
||||
allowed_types = {"web", "knowledge", "skill", "mcp", "file", "other"}
|
||||
materials: list[ResearchMaterial] = []
|
||||
seen: set[str] = set()
|
||||
for value in values or []:
|
||||
if not isinstance(value, dict):
|
||||
continue
|
||||
title = str(value.get("title") or "").strip()[:500]
|
||||
url = str(value.get("url") or "").strip()[:2000] or None
|
||||
content = str(value.get("raw_content") or value.get("snippet") or "").strip()[:6000]
|
||||
if not title and not url and not content:
|
||||
continue
|
||||
raw_key = str(value.get("id") or value.get("rec_uuid") or url or f"{title}\n{content[:240]}")
|
||||
digest = hashlib.sha256(raw_key.encode("utf-8", errors="ignore")).hexdigest()
|
||||
material_id = str(value.get("id") or f"src_{digest[:16]}")[:128]
|
||||
if material_id in seen:
|
||||
continue
|
||||
seen.add(material_id)
|
||||
source_type = str(value.get("source_type") or "other")
|
||||
materials.append(
|
||||
ResearchMaterial(
|
||||
id=material_id,
|
||||
query=query,
|
||||
title=title or "研究来源",
|
||||
url=url,
|
||||
raw_content=content,
|
||||
snippet=str(value.get("snippet") or content[:500]).strip()[:500] or None,
|
||||
source=str(value.get("source") or "browser_collector").strip()[:200],
|
||||
source_type=source_type if source_type in allowed_types else "other",
|
||||
rec_uuid=str(value.get("rec_uuid") or "").strip()[:128] or None,
|
||||
content_hash=digest[:32],
|
||||
metadata={"retrieval_channel": "browser_material_snapshot"},
|
||||
)
|
||||
)
|
||||
if len(materials) >= 80:
|
||||
break
|
||||
return materials
|
||||
|
||||
# Phase → progress% mapping for the job timeline.
|
||||
_PHASE_PROGRESS: dict[str, int] = {
|
||||
"initializing": 0,
|
||||
@@ -83,6 +166,21 @@ _PHASE_PROGRESS: dict[str, int] = {
|
||||
}
|
||||
|
||||
|
||||
def _session_diagnostic_error(diagnostics: list[dict[str, Any]]) -> str | None:
|
||||
"""Render real degraded-run causes into the session API's error field."""
|
||||
lines: list[str] = []
|
||||
for item in diagnostics[-20:]:
|
||||
code = str(item.get("code") or "deep_research_degraded")
|
||||
stage = str(item.get("stage") or "unknown")
|
||||
error_type = str(item.get("errorType") or item.get("outcome") or "Warning")
|
||||
detail = str(item.get("detail") or "").strip()
|
||||
line = f"[{code}] stage={stage} type={error_type}"
|
||||
if detail:
|
||||
line += f": {detail}"
|
||||
lines.append(line[:1200])
|
||||
return "\n".join(lines)[:8000] or None
|
||||
|
||||
|
||||
class _JobUser:
|
||||
"""Minimal CurrentUser: satisfies user_context's ``.id`` protocol."""
|
||||
|
||||
@@ -124,10 +222,19 @@ class RecordingEventSink:
|
||||
self._session_id = session_id
|
||||
self._user_id = user_id
|
||||
self._lease_owner = lease_owner
|
||||
self.diagnostics: list[dict[str, Any]] = []
|
||||
|
||||
async def emit(self, type, *, phase="initializing", payload=None): # noqa: A002
|
||||
# Delegate first (persist-then-stream ordering).
|
||||
ev = await self._delegate.emit(type, phase=phase, payload=payload or {})
|
||||
diagnostic = (payload or {}).get("diagnostic")
|
||||
if isinstance(diagnostic, dict):
|
||||
self.diagnostics.append(
|
||||
{
|
||||
"code": str((payload or {}).get("code") or type),
|
||||
**diagnostic,
|
||||
}
|
||||
)
|
||||
|
||||
# Side-effects.
|
||||
if type == "source_added" and self._source_store is not None:
|
||||
@@ -140,8 +247,17 @@ class RecordingEventSink:
|
||||
user_id=self._user_id,
|
||||
**src,
|
||||
)
|
||||
except Exception: # noqa: BLE001
|
||||
logger.debug("source upsert failed for %s", src.get("id"), exc_info=True)
|
||||
except Exception as exc: # noqa: BLE001
|
||||
self.diagnostics.append(
|
||||
{
|
||||
"code": "source_projection_failed",
|
||||
"stage": "collecting",
|
||||
"errorType": exc.__class__.__name__,
|
||||
"detail": str(exc)[:1200] or exc.__class__.__name__,
|
||||
"sourceId": str(src.get("id") or ""),
|
||||
}
|
||||
)
|
||||
logger.warning("source upsert failed for %s", src.get("id"), exc_info=True)
|
||||
|
||||
elif type == "phase_changed":
|
||||
new_phase = (payload or {}).get("to", phase)
|
||||
@@ -303,18 +419,140 @@ class DeepResearchJobExecutor:
|
||||
lease_owner=lease_owner,
|
||||
)
|
||||
return
|
||||
result = await self._execute(
|
||||
job_id=job_id,
|
||||
session_id=session_id,
|
||||
from deerflow.agents.deep_research.runners.basic import _build_report_fallback
|
||||
|
||||
entry = str(snapshot.get("entry") or "legacy")
|
||||
snapshot_projection_diagnostics: list[dict[str, Any]] = []
|
||||
# A structural regeneration has its own cloned durable source pool.
|
||||
# The browser snapshot is intentionally kept frozen in the job
|
||||
# input until the final fallback step. Persisting it here would
|
||||
# contaminate the database channel and make it impossible to tell
|
||||
# whether the runtime/checkpoint or durable-source path worked.
|
||||
try:
|
||||
result = await self._execute(
|
||||
job_id=job_id,
|
||||
session_id=session_id,
|
||||
user_id=user_id,
|
||||
query=query,
|
||||
title=title,
|
||||
config=config,
|
||||
lease_owner=lease_owner,
|
||||
cancel_token=cancel_token,
|
||||
entry=entry,
|
||||
collector_materials=(snapshot.get("collector_materials") or []) if entry == "chat" else None,
|
||||
)
|
||||
except (ResearchAwaitingInput, ResearchCancelled):
|
||||
raise
|
||||
except Exception as exc: # noqa: BLE001 - complete without a second model call
|
||||
# Do not repeat a full model generation. Preserve any durable
|
||||
# sources and complete with a deterministic report so one
|
||||
# failed model/checkpointer/event path cannot stop the job.
|
||||
logger.exception(
|
||||
"deep-research job %s execution failed; completing from durable materials",
|
||||
job_id,
|
||||
)
|
||||
rows: list[dict[str, Any]] = []
|
||||
source_read_error: Exception | None = None
|
||||
snapshot_materials: list[ResearchMaterial] = []
|
||||
snapshot_read_error: Exception | None = None
|
||||
if entry == "chat":
|
||||
try:
|
||||
snapshot_materials = _materials_from_browser_snapshot(
|
||||
snapshot.get("collector_materials") or [],
|
||||
query=query,
|
||||
)
|
||||
except Exception as snapshot_exc: # noqa: BLE001
|
||||
snapshot_read_error = snapshot_exc
|
||||
if not snapshot_materials and self._source_store is not None:
|
||||
try:
|
||||
rows = await self._source_store.list_by_session(
|
||||
session_id,
|
||||
user_id=user_id,
|
||||
selected=None,
|
||||
limit=500,
|
||||
include_content=True,
|
||||
)
|
||||
except Exception as read_exc: # noqa: BLE001
|
||||
source_read_error = read_exc
|
||||
fallback_parts: list[str] = []
|
||||
source_ids: list[str] = []
|
||||
seen_source_ids: set[str] = set()
|
||||
for index, row in enumerate(rows, 1):
|
||||
source_id = str(row.get("id") or "")
|
||||
if source_id and source_id in seen_source_ids:
|
||||
continue
|
||||
if source_id:
|
||||
seen_source_ids.add(source_id)
|
||||
source_ids.append(source_id)
|
||||
source_url = str(row.get("url") or "").strip()
|
||||
source_header = f"[{len(source_ids) or index}] {str(row.get('title') or '研究来源')}"
|
||||
if source_url:
|
||||
source_header += f"({source_url})"
|
||||
source_content = str(row.get("raw_content") or row.get("snippet") or "")
|
||||
fallback_parts.append(f"{source_header}\n{source_content}")
|
||||
for material in snapshot_materials:
|
||||
if material.id in seen_source_ids:
|
||||
continue
|
||||
seen_source_ids.add(material.id)
|
||||
source_ids.append(material.id)
|
||||
source_header = f"[{len(source_ids)}] {material.title or '研究来源'}"
|
||||
if material.url:
|
||||
source_header += f"({material.url})"
|
||||
fallback_parts.append(f"{source_header}\n{material.raw_content or material.snippet or ''}")
|
||||
diagnostics = [
|
||||
{
|
||||
"code": "execution_failed_report_fallback",
|
||||
"stage": "execution",
|
||||
"errorType": exc.__class__.__name__,
|
||||
"detail": str(exc)[:1200] or exc.__class__.__name__,
|
||||
}
|
||||
]
|
||||
if source_read_error is not None:
|
||||
diagnostics.append(
|
||||
{
|
||||
"code": "fallback_source_read_failed",
|
||||
"stage": "collecting",
|
||||
"errorType": source_read_error.__class__.__name__,
|
||||
"detail": str(source_read_error)[:1200]
|
||||
or source_read_error.__class__.__name__,
|
||||
}
|
||||
)
|
||||
if snapshot_read_error is not None:
|
||||
diagnostics.append(
|
||||
{
|
||||
"code": "fallback_browser_snapshot_read_failed",
|
||||
"stage": "collecting",
|
||||
"errorType": snapshot_read_error.__class__.__name__,
|
||||
"detail": str(snapshot_read_error)[:1200]
|
||||
or snapshot_read_error.__class__.__name__,
|
||||
}
|
||||
)
|
||||
result = DeepResearchResult(
|
||||
report_markdown=_build_report_fallback(
|
||||
query,
|
||||
"\n\n---\n\n".join(fallback_parts),
|
||||
len(source_ids),
|
||||
),
|
||||
source_ids=source_ids,
|
||||
diagnostics=diagnostics,
|
||||
)
|
||||
if snapshot_projection_diagnostics:
|
||||
result = result.model_copy(
|
||||
update={
|
||||
"diagnostics": [
|
||||
*result.diagnostics,
|
||||
*snapshot_projection_diagnostics,
|
||||
]
|
||||
}
|
||||
)
|
||||
await self._mark_completed(
|
||||
job_id,
|
||||
session_id,
|
||||
lease_owner,
|
||||
result,
|
||||
user_id=user_id,
|
||||
query=query,
|
||||
title=title,
|
||||
config=config,
|
||||
lease_owner=lease_owner,
|
||||
cancel_token=cancel_token,
|
||||
entry=str(snapshot.get("entry") or "legacy"),
|
||||
)
|
||||
await self._mark_completed(job_id, session_id, lease_owner, result)
|
||||
|
||||
except ResearchAwaitingInput as pause:
|
||||
logger.info("deep-research job %s awaiting human plan review", job_id)
|
||||
@@ -1324,6 +1562,7 @@ class DeepResearchJobExecutor:
|
||||
lease_owner: str | None,
|
||||
cancel_token: CancellationToken,
|
||||
entry: str = "legacy",
|
||||
collector_materials: list[dict[str, Any]] | None = None,
|
||||
) -> DeepResearchResult:
|
||||
from deerflow.agents.deep_research.adapters.artifacts import ThreadOutputsArtifactWriter
|
||||
from deerflow.agents.deep_research.adapters.context import DeerFlowContextCompressor
|
||||
@@ -1359,10 +1598,14 @@ class DeepResearchJobExecutor:
|
||||
# from the collector-agent conversation; legacy keeps the search tiers.
|
||||
if entry == "chat":
|
||||
material_provider = await self._build_chat_materials(
|
||||
job_id=job_id,
|
||||
session_id=session_id,
|
||||
user_id=user_id,
|
||||
runtime_thread_id=runtime_thread_id,
|
||||
query=query,
|
||||
events=recording_sink,
|
||||
cancellation=cancel_token,
|
||||
collector_materials=collector_materials,
|
||||
)
|
||||
elif entry == "regenerate":
|
||||
material_provider = await self._build_selected_materials(
|
||||
@@ -1419,6 +1662,12 @@ class DeepResearchJobExecutor:
|
||||
)
|
||||
# Fold the summary call's tokens into the billed totals.
|
||||
result = result.model_copy(update={"usage": llm_backend.usage_totals})
|
||||
diagnostics = [
|
||||
*getattr(base_sink, "diagnostics", []),
|
||||
*recording_sink.diagnostics,
|
||||
]
|
||||
if diagnostics:
|
||||
result = result.model_copy(update={"diagnostics": diagnostics})
|
||||
return result
|
||||
|
||||
async def _build_selected_materials(
|
||||
@@ -1444,6 +1693,19 @@ class DeepResearchJobExecutor:
|
||||
limit=500,
|
||||
include_content=True,
|
||||
)
|
||||
if not rows:
|
||||
# Before a first report is written, collector material may already
|
||||
# be durable but not yet carry the runner's later curation flag.
|
||||
# Treat every source in this session as the static input pool. A
|
||||
# genuinely empty session remains valid and is handled by the
|
||||
# runner's zero-material report path.
|
||||
rows = await self._source_store.list_by_session(
|
||||
session_id,
|
||||
user_id=user_id,
|
||||
selected=None,
|
||||
limit=500,
|
||||
include_content=True,
|
||||
)
|
||||
allowed_types = {"web", "knowledge", "skill", "mcp", "file", "other"}
|
||||
materials = [
|
||||
ResearchMaterial(
|
||||
@@ -1468,16 +1730,24 @@ class DeepResearchJobExecutor:
|
||||
if row.get("id")
|
||||
]
|
||||
if not materials:
|
||||
raise RuntimeError("No selected source material is available for regeneration")
|
||||
logger.warning(
|
||||
"deep-research static report writer has no persisted sources; continuing with no-material fallback "
|
||||
"(session_id=%s)",
|
||||
session_id,
|
||||
)
|
||||
return StaticHarvestMaterialProvider(materials)
|
||||
|
||||
async def _build_chat_materials(
|
||||
self,
|
||||
*,
|
||||
job_id: str,
|
||||
session_id: str,
|
||||
user_id: str,
|
||||
runtime_thread_id: str,
|
||||
query: str,
|
||||
events,
|
||||
cancellation: CancellationToken,
|
||||
collector_materials: list[dict[str, Any]] | None = None,
|
||||
):
|
||||
"""Harvest the collector thread into a static material pool.
|
||||
|
||||
@@ -1485,22 +1755,207 @@ class DeepResearchJobExecutor:
|
||||
the sources panel fills exactly like the legacy channel (the recording
|
||||
sink persists them to the source store).
|
||||
"""
|
||||
from deerflow.agents.deep_research.adapters.material import dedupe_materials
|
||||
from deerflow.agents.deep_research.collection.thread_harvester import (
|
||||
MAX_HARVESTED_MATERIALS,
|
||||
StaticHarvestMaterialProvider,
|
||||
harvest_thread_materials,
|
||||
)
|
||||
|
||||
cancellation.raise_if_cancelled()
|
||||
if self._checkpointer is None:
|
||||
raise RuntimeError("Chat-entry deep-research jobs require a checkpointer")
|
||||
if not runtime_thread_id:
|
||||
raise RuntimeError("Chat-entry deep-research job has no runtime thread")
|
||||
harvested: list[ResearchMaterial] = []
|
||||
agent_summary = ""
|
||||
browser_material_count = 0
|
||||
checkpoint_material_count = 0
|
||||
durable_material_count = 0
|
||||
material_channel = "none"
|
||||
browser_snapshot_persisted = False
|
||||
channel_failures: list[dict[str, str]] = []
|
||||
|
||||
def persisted_rows_to_materials(rows: list[dict[str, Any]]) -> list[ResearchMaterial]:
|
||||
allowed_types = {"web", "knowledge", "skill", "mcp", "file", "other"}
|
||||
return [
|
||||
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")
|
||||
]
|
||||
|
||||
async def try_browser_snapshot() -> None:
|
||||
nonlocal browser_material_count, browser_snapshot_persisted, harvested, material_channel
|
||||
if not collector_materials:
|
||||
channel_failures.append(
|
||||
{
|
||||
"channel": "browser_snapshot",
|
||||
"outcome": "absent",
|
||||
"detail": "浏览器未传入素材快照",
|
||||
}
|
||||
)
|
||||
return
|
||||
try:
|
||||
browser_materials = _materials_from_browser_snapshot(
|
||||
collector_materials,
|
||||
query=query,
|
||||
)
|
||||
browser_material_count = len(browser_materials)
|
||||
harvested = dedupe_materials(browser_materials)[:MAX_HARVESTED_MATERIALS]
|
||||
if not harvested:
|
||||
channel_failures.append(
|
||||
{
|
||||
"channel": "browser_snapshot",
|
||||
"outcome": "empty",
|
||||
"detail": "浏览器快照中没有可用素材",
|
||||
}
|
||||
)
|
||||
return
|
||||
material_channel = "browser_snapshot"
|
||||
if self._source_store is None:
|
||||
return
|
||||
browser_snapshot_persisted = True
|
||||
for material in harvested:
|
||||
try:
|
||||
await self._source_store.upsert(
|
||||
session_id=session_id,
|
||||
job_id=job_id,
|
||||
user_id=user_id or None,
|
||||
**material.to_persistence_dict(material.query),
|
||||
)
|
||||
except Exception as exc: # noqa: BLE001 - report can still use the snapshot
|
||||
browser_snapshot_persisted = False
|
||||
channel_failures.append(
|
||||
{
|
||||
"channel": "browser_snapshot_projection",
|
||||
"outcome": "error",
|
||||
"errorType": exc.__class__.__name__,
|
||||
"detail": str(exc)[:1200] or exc.__class__.__name__,
|
||||
}
|
||||
)
|
||||
logger.warning(
|
||||
"browser snapshot source projection failed for %s",
|
||||
material.id,
|
||||
exc_info=True,
|
||||
)
|
||||
except Exception as exc: # noqa: BLE001 - later material channels remain valid
|
||||
channel_failures.append(
|
||||
{
|
||||
"channel": "browser_snapshot",
|
||||
"outcome": "error",
|
||||
"errorType": exc.__class__.__name__,
|
||||
"detail": str(exc)[:1200] or exc.__class__.__name__,
|
||||
}
|
||||
)
|
||||
logger.warning(
|
||||
"deep-research browser material snapshot could not be normalized",
|
||||
exc_info=True,
|
||||
)
|
||||
|
||||
# First-report recovery is deliberately sequential, not additive:
|
||||
# browser snapshot → durable source rows → runtime checkpoint. Once a
|
||||
# channel provides material, later channels are not opened.
|
||||
await try_browser_snapshot()
|
||||
if not harvested:
|
||||
try:
|
||||
if self._checkpointer is None:
|
||||
raise RuntimeError("Deep Research checkpointer is unavailable")
|
||||
if not runtime_thread_id:
|
||||
raise RuntimeError("Deep Research collector thread is unavailable")
|
||||
checkpoint_materials, checkpoint_summary = await harvest_thread_materials(
|
||||
checkpointer=self._checkpointer,
|
||||
runtime_thread_id=runtime_thread_id,
|
||||
query=query,
|
||||
)
|
||||
checkpoint_material_count = len(checkpoint_materials)
|
||||
agent_summary = checkpoint_summary or ""
|
||||
harvested = dedupe_materials(checkpoint_materials)[:MAX_HARVESTED_MATERIALS]
|
||||
if harvested:
|
||||
material_channel = "runtime_checkpoint"
|
||||
else:
|
||||
channel_failures.append(
|
||||
{
|
||||
"channel": "runtime_checkpoint",
|
||||
"outcome": "empty",
|
||||
"detail": "运行时会话未返回可用素材",
|
||||
}
|
||||
)
|
||||
except ResearchCancelled:
|
||||
raise
|
||||
except Exception as exc: # noqa: BLE001 - all earlier channels remain available
|
||||
channel_failures.append(
|
||||
{
|
||||
"channel": "runtime_checkpoint",
|
||||
"outcome": "error",
|
||||
"errorType": exc.__class__.__name__,
|
||||
"detail": str(exc)[:1200] or exc.__class__.__name__,
|
||||
}
|
||||
)
|
||||
logger.warning(
|
||||
"deep-research collector checkpoint harvest failed; no earlier material channel was usable "
|
||||
"(thread=%s, error=%s)",
|
||||
runtime_thread_id,
|
||||
exc.__class__.__name__,
|
||||
)
|
||||
|
||||
if not harvested:
|
||||
if self._source_store is None:
|
||||
channel_failures.append(
|
||||
{
|
||||
"channel": "durable_sources",
|
||||
"outcome": "unavailable",
|
||||
"detail": "素材存储库不可用",
|
||||
}
|
||||
)
|
||||
else:
|
||||
try:
|
||||
rows = await self._source_store.list_by_session(
|
||||
session_id,
|
||||
user_id=user_id,
|
||||
selected=None,
|
||||
limit=500,
|
||||
include_content=True,
|
||||
)
|
||||
persisted_materials = persisted_rows_to_materials(rows)
|
||||
durable_material_count = len(persisted_materials)
|
||||
harvested = dedupe_materials(persisted_materials)[:MAX_HARVESTED_MATERIALS]
|
||||
if harvested:
|
||||
material_channel = "durable_sources"
|
||||
else:
|
||||
channel_failures.append(
|
||||
{
|
||||
"channel": "durable_sources",
|
||||
"outcome": "empty",
|
||||
"detail": "数据库中没有可用素材",
|
||||
}
|
||||
)
|
||||
except Exception as exc: # noqa: BLE001 - no-material report remains valid
|
||||
channel_failures.append(
|
||||
{
|
||||
"channel": "durable_sources",
|
||||
"outcome": "error",
|
||||
"errorType": exc.__class__.__name__,
|
||||
"detail": str(exc)[:1200] or exc.__class__.__name__,
|
||||
}
|
||||
)
|
||||
logger.warning(
|
||||
"chat deep-research durable material read failed; entering no-material route if needed",
|
||||
exc_info=True,
|
||||
)
|
||||
|
||||
harvested, agent_summary = await harvest_thread_materials(
|
||||
checkpointer=self._checkpointer,
|
||||
runtime_thread_id=runtime_thread_id,
|
||||
query=query,
|
||||
)
|
||||
# Freeze the harvested list before publishing any progress. Source
|
||||
# event persistence is intentionally best-effort: a bad payload or a
|
||||
# temporarily unavailable event table must never prevent the writer
|
||||
@@ -1511,42 +1966,80 @@ class DeepResearchJobExecutor:
|
||||
"collection_completed",
|
||||
phase="collecting",
|
||||
payload={
|
||||
"source": "chat_thread",
|
||||
"source": material_channel,
|
||||
"materialCount": material_provider.material_count,
|
||||
"browserMaterialCount": browser_material_count,
|
||||
"checkpointMaterialCount": checkpoint_material_count,
|
||||
"durableMaterialCount": durable_material_count,
|
||||
"agentSummaryChars": len(agent_summary),
|
||||
},
|
||||
)
|
||||
except Exception: # noqa: BLE001 - progress telemetry must not lose evidence
|
||||
logger.warning("chat deep-research collection event failed; retaining harvested materials", exc_info=True)
|
||||
for material in harvested:
|
||||
# A last-resort browser snapshot is written directly above. Avoid a
|
||||
# second source-added projection only when every direct write succeeded.
|
||||
if material_channel != "browser_snapshot" or not browser_snapshot_persisted:
|
||||
for material in harvested:
|
||||
try:
|
||||
await events.emit(
|
||||
"source_added",
|
||||
phase="collecting",
|
||||
payload={
|
||||
"sourceId": material.id,
|
||||
"title": material.title,
|
||||
"url": material.url,
|
||||
"sourceType": material.source_type,
|
||||
"source": material.to_persistence_dict(material.query),
|
||||
},
|
||||
)
|
||||
except Exception: # noqa: BLE001 - source rows are a projection, not the input
|
||||
logger.warning(
|
||||
"chat deep-research source event failed for %s; retaining harvested materials",
|
||||
material.id,
|
||||
exc_info=True,
|
||||
)
|
||||
if material_provider.material_count and material_channel != "browser_snapshot":
|
||||
try:
|
||||
await events.emit(
|
||||
"source_added",
|
||||
"warning",
|
||||
phase="collecting",
|
||||
payload={
|
||||
"sourceId": material.id,
|
||||
"title": material.title,
|
||||
"url": material.url,
|
||||
"sourceType": material.source_type,
|
||||
"source": material.to_persistence_dict(material.query),
|
||||
"code": "material_channel_fallback_used",
|
||||
"message": f"前序素材通道不可用,已使用{material_channel}继续撰写报告",
|
||||
"recoverable": True,
|
||||
"diagnostic": {
|
||||
"stage": "collecting",
|
||||
"outcome": "sequential_material_fallback",
|
||||
"selectedChannel": material_channel,
|
||||
"browserMaterialCount": browser_material_count,
|
||||
"checkpointMaterialCount": checkpoint_material_count,
|
||||
"durableMaterialCount": durable_material_count,
|
||||
"runtimeThreadId": runtime_thread_id,
|
||||
"failures": channel_failures,
|
||||
},
|
||||
},
|
||||
)
|
||||
except Exception: # noqa: BLE001 - source rows are a projection, not the input
|
||||
logger.warning(
|
||||
"chat deep-research source event failed for %s; retaining harvested materials",
|
||||
material.id,
|
||||
exc_info=True,
|
||||
except Exception: # noqa: BLE001 - diagnostics never block writing
|
||||
logger.warning("failed to publish material fallback diagnostic", exc_info=True)
|
||||
if not material_provider.material_count:
|
||||
try:
|
||||
await events.emit(
|
||||
"warning",
|
||||
phase="collecting",
|
||||
payload={
|
||||
"code": "chat_harvest_empty",
|
||||
"message": "所有素材通道均为空,已继续进入无素材报告流程",
|
||||
"recoverable": True,
|
||||
"diagnostic": {
|
||||
"stage": "collecting",
|
||||
"outcome": "all_material_channels_empty",
|
||||
"runtimeThreadId": runtime_thread_id,
|
||||
"failures": channel_failures,
|
||||
},
|
||||
},
|
||||
)
|
||||
if not harvested:
|
||||
await events.emit(
|
||||
"warning",
|
||||
phase="collecting",
|
||||
payload={
|
||||
"code": "chat_harvest_empty",
|
||||
"message": "收集对话中没有可用的检索材料,报告将仅基于收集员小结生成。",
|
||||
"recoverable": True,
|
||||
},
|
||||
)
|
||||
except Exception: # noqa: BLE001 - diagnostics never block writing
|
||||
logger.warning("failed to publish empty-material diagnostic", exc_info=True)
|
||||
return material_provider
|
||||
|
||||
async def _generate_report_summary(
|
||||
@@ -1617,7 +2110,78 @@ class DeepResearchJobExecutor:
|
||||
|
||||
# ── terminal-state writers ──────────────────────────────────────────────
|
||||
|
||||
async def _mark_completed(self, job_id: str, session_id: str, lease_owner: str | None, result: DeepResearchResult) -> None:
|
||||
async def _mark_completed(
|
||||
self,
|
||||
job_id: str,
|
||||
session_id: str,
|
||||
lease_owner: str | None,
|
||||
result: DeepResearchResult,
|
||||
*,
|
||||
user_id: str,
|
||||
query: str,
|
||||
) -> None:
|
||||
# Final persistence gate. Even if a runner/provider takes an
|
||||
# unexpected path, refusal prose must never become the durable report.
|
||||
# Keep the original model output as a visible diagnostic and replace
|
||||
# it with a conservative report assembled from every persisted source.
|
||||
from deerflow.agents.deep_research.runners.basic import _build_report_fallback
|
||||
|
||||
validation_error = _terminal_report_validation_error(result.report_markdown)
|
||||
if validation_error is not None:
|
||||
invalid_output = result.report_markdown
|
||||
rows: list[dict[str, Any]] = []
|
||||
source_load_error: Exception | None = None
|
||||
if self._source_store is not None:
|
||||
try:
|
||||
rows = await self._source_store.list_by_session(
|
||||
session_id,
|
||||
user_id=user_id,
|
||||
selected=None,
|
||||
limit=500,
|
||||
include_content=True,
|
||||
)
|
||||
except Exception as exc: # noqa: BLE001 - fallback must still complete
|
||||
source_load_error = exc
|
||||
|
||||
fallback_parts: list[str] = []
|
||||
for index, row in enumerate(rows, 1):
|
||||
source_url = str(row.get("url") or "").strip()
|
||||
source_header = f"[{index}] {str(row.get('title') or '研究来源')}"
|
||||
if source_url:
|
||||
source_header += f"({source_url})"
|
||||
source_content = str(row.get("raw_content") or row.get("snippet") or "")
|
||||
fallback_parts.append(f"{source_header}\n{source_content}")
|
||||
|
||||
diagnostics = [
|
||||
*result.diagnostics,
|
||||
{
|
||||
"code": "terminal_invalid_report_replaced",
|
||||
"stage": "persisting",
|
||||
"errorType": "InvalidReportOutput",
|
||||
"detail": f"{validation_error}: {invalid_output[:1200]}",
|
||||
},
|
||||
]
|
||||
if source_load_error is not None:
|
||||
diagnostics.append(
|
||||
{
|
||||
"code": "terminal_fallback_source_load_failed",
|
||||
"stage": "persisting",
|
||||
"errorType": source_load_error.__class__.__name__,
|
||||
"detail": str(source_load_error)[:1200],
|
||||
}
|
||||
)
|
||||
result = result.model_copy(
|
||||
update={
|
||||
"report_markdown": _build_report_fallback(
|
||||
query or "研究课题",
|
||||
"\n\n---\n\n".join(fallback_parts),
|
||||
len(rows) or len(result.source_ids),
|
||||
),
|
||||
"report_html": None,
|
||||
"diagnostics": diagnostics,
|
||||
}
|
||||
)
|
||||
|
||||
# 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
|
||||
@@ -1639,6 +2203,8 @@ class DeepResearchJobExecutor:
|
||||
"report_markdown": result.report_markdown[:200] if result.report_markdown else "",
|
||||
"source_ids": result.source_ids,
|
||||
"plan": result.plan,
|
||||
"diagnostics": result.diagnostics,
|
||||
"executorRevision": EXECUTOR_REVISION,
|
||||
},
|
||||
)
|
||||
# Emit a terminal event for SSE consumers.
|
||||
@@ -1674,6 +2240,7 @@ class DeepResearchJobExecutor:
|
||||
usage_snapshot={**(result.usage or {}), "lastJobId": job_id},
|
||||
source_count=len(result.source_ids),
|
||||
active_job_id=None,
|
||||
error=_session_diagnostic_error(result.diagnostics),
|
||||
)
|
||||
if updated is not None:
|
||||
return
|
||||
@@ -1717,7 +2284,14 @@ class DeepResearchJobExecutor:
|
||||
await self._session_store.update(
|
||||
session_id,
|
||||
status="failed",
|
||||
error=f"{code}: {message}",
|
||||
# Correlate the user-visible session error with the exact
|
||||
# durable job and gateway worker that produced it. This
|
||||
# distinguishes a newly raised failure from stale session
|
||||
# state and exposes mixed-version workers immediately.
|
||||
error=(
|
||||
f"[job={job_id} worker={WORKER_ID} revision={EXECUTOR_REVISION}] "
|
||||
f"{code}: {message}"
|
||||
),
|
||||
active_job_id=None,
|
||||
)
|
||||
with suppress(Exception): # noqa: BLE001
|
||||
@@ -1805,4 +2379,10 @@ class DeepResearchJobExecutor:
|
||||
return thread_id
|
||||
|
||||
|
||||
__all__ = ["DeepResearchJobExecutor", "WORKER_ID", "_LEASE_TTL_SECONDS"]
|
||||
__all__ = [
|
||||
"DeepResearchJobExecutor",
|
||||
"EXECUTOR_REVISION",
|
||||
"WORKER_ID",
|
||||
"_LEASE_TTL_SECONDS",
|
||||
"_terminal_report_validation_error",
|
||||
]
|
||||
Reference in new issue
Block a user