sync update from LAN repo (source dhk-1 @ 3b1ec5b4)

This commit is contained in:
gozeros committed 2026-11-05 10:56:34 +08:00
1 parent da6521b6cb
commit 264e2200d6
995 files changed
+148105 -13399

No files matched your search

@@ -69,6 +69,9 @@ class DeepResearchConfig(BaseModel):
mode: ResearchMode = "basic"
collection_mode: CollectionMode = "chat"
# Frozen collector retrieval scope; access is checked by the shared RAG gateway.
llmwiki_knowledge_base_ids: list[str] = Field(default_factory=list)
knowledge_retrieval_mode: Literal["classic", "smart_reasoning"] = "classic"
# Missing snapshots (older sessions) stay on the writer fallback.
outline_planner: OutlinePlanner = "pipeline"
language: str = "zh-CN"
@@ -824,6 +824,7 @@ class BasicRunner:
message: str,
error_type: str,
error_detail: str,
messages: list[dict[str, str]] | None = None,
) -> str:
"""Spend one real second model call before any deterministic report.
@@ -863,7 +864,7 @@ class BasicRunner:
result = await _await_writer(
complete_prose(
model_role="smart",
messages=completion_messages,
messages=messages or completion_messages,
operation="write_report_retry",
)
)
@@ -876,7 +877,7 @@ class BasicRunner:
result = await _await_writer(
llm.stream_complete(
model_role="smart",
messages=completion_messages,
messages=messages or completion_messages,
operation="write_report_retry",
on_delta=emitter.add,
on_reasoning=emitter.add_thinking,
@@ -927,7 +928,13 @@ class BasicRunner:
payload={"reportVersion": report_version, "reason": reason},
)
fallback = ensure_outline_preamble(
_build_report_fallback(request.query, context_text, source_count or 0),
_build_report_fallback(
request.query,
context_text,
source_count or 0,
custom_outline=outline,
structure_mode=request.config.structure_mode,
),
outline,
request.query,
)
@@ -972,10 +979,76 @@ class BasicRunner:
),
report_version=3,
)
return report if not _is_refusal_report(report) else await refusal_fallback()
if _is_refusal_report(report):
return await refusal_fallback()
outline_error = _outline_adherence_error(
report,
outline,
structure_mode=request.config.structure_mode,
)
if outline_error:
return await complete_with_fallback(
reason="outline_mismatch_after_retry",
message="报告写入重试完成但仍未遵循所选大纲,已按所选大纲生成保底稿",
error_type="InvalidReportStructure",
error_detail=outline_error,
report_version=3,
)
return report
if not _is_refusal_report(report):
return report
outline_error = _outline_adherence_error(
report,
outline,
structure_mode=request.config.structure_mode,
)
if not outline_error:
return report
# A selected outline is a delivery contract, not merely a prompt
# hint. Fixed mode validates the complete hierarchy; adaptive mode
# only rejects zero core-heading overlap. Reuse the existing single
# retry budget for one targeted rewrite; never add a third call.
try:
report = await retry_report(
report_version=2,
reason="outline_mismatch",
code="report_outline_retry",
message="生成结果未遵循所选报告大纲,正在按模板结构重新生成",
error_type="InvalidReportStructure",
error_detail=outline_error,
messages=_outline_repair_messages(
completion_messages,
outline=outline,
structure_mode=request.config.structure_mode,
validation_error=outline_error,
),
)
except ResearchCancelled:
raise
except Exception as retry_exc: # noqa: BLE001 - retain a template-shaped result
return await complete_with_fallback(
reason="outline_mismatch_retry_failed",
message="报告大纲重写未成功,已按所选大纲生成保底稿",
error_type=type(retry_exc).__name__,
error_detail=f"retry={type(retry_exc).__name__}: {str(retry_exc)[:600]}",
report_version=3,
)
retry_outline_error = _outline_adherence_error(
report,
outline,
structure_mode=request.config.structure_mode,
)
if not _is_refusal_report(report) and not retry_outline_error:
return report
return await complete_with_fallback(
reason="outline_mismatch_after_retry",
message="重写结果仍未遵循所选大纲,已按所选大纲生成保底稿",
error_type="InvalidReportStructure",
error_detail=retry_outline_error or report,
report_version=3,
)
# A refusal is a content failure, not a transport failure: one clean
# rewrite recovers it far more often than a material dump does.
@@ -998,7 +1071,22 @@ class BasicRunner:
error_detail=f"retry={type(retry_exc).__name__}: {str(retry_exc)[:600]}",
report_version=3,
)
return report if not _is_refusal_report(report) else await refusal_fallback()
if _is_refusal_report(report):
return await refusal_fallback()
outline_error = _outline_adherence_error(
report,
outline,
structure_mode=request.config.structure_mode,
)
if outline_error:
return await complete_with_fallback(
reason="outline_mismatch_after_retry",
message="报告重试结果未遵循所选大纲,已按所选大纲生成保底稿",
error_type="InvalidReportStructure",
error_detail=outline_error,
report_version=3,
)
return report
# ── helpers ─────────────────────────────────────────────────────────────────
@@ -1064,6 +1152,204 @@ def extract_generated_outline(text: str | None) -> str | None:
return None
_OUTLINE_TITLE_PLACEHOLDERS = frozenset(
{
"报告标题",
"报告题目",
"方案标题",
"方案题目",
"研究报告",
}
)
_ADAPTIVE_HEADING_PREFIX_RE = re.compile(
r"^(?:"
r"第\s*[零〇一二三四五六七八九十百千万\d]+\s*(?:章|节|部分|篇)\s*[、..::\-—]?\s*"
r"|[((]\s*(?:[零〇一二三四五六七八九十百千万]+|\d+)\s*[))]\s*[、..::\-—]?\s*"
r"|\d+(?:\.\d+)*(?:\s*[、..::\-—]\s*|\s+)"
r"|[零〇一二三四五六七八九十百千万]+(?:\s*[、..::\-—]\s*|\s+)"
r"|(?:chapter|part|section)\s+\d+(?:\s*[.::\-—]\s*|\s+)"
r")",
re.IGNORECASE,
)
_ADAPTIVE_GENERIC_HEADINGS = frozenset(
{
"摘要",
"内容摘要",
"执行摘要",
"概述",
"引言",
"导言",
"前言",
"绪论",
"结论",
"核心结论",
"主要结论",
"研究结论",
"结论与建议",
"总结",
"总结与展望",
"结语",
"参考资料",
"参考来源",
"参考文献",
"附录",
"附件",
}
)
def _normalized_heading_title(value: str) -> str:
"""Normalize harmless Markdown decoration without weakening title checks."""
text = re.sub(r"[`*_~]", "", value)
return " ".join(text.split()).strip().casefold()
def _adaptive_heading_key(value: str) -> str:
"""Normalize chapter-number variants for loose adaptive matching only."""
text = _normalized_heading_title(value)
while text:
stripped = _ADAPTIVE_HEADING_PREFIX_RE.sub("", text, count=1).strip()
if stripped == text:
break
text = stripped
return text
def _outline_heading_specs(value: str | None) -> list[tuple[int, str, str]]:
"""Return ``(level, title, markdown_line)`` for a stored outline/report.
Report structures accept both Markdown headings and common Chinese/numbered
chapter lines. Reusing ``_normalize_outline_heading_line`` keeps validation
and the deterministic fallback aligned with the formats accepted by the
writer prompt.
"""
specs: list[tuple[int, str, str]] = []
for raw_line in (value or "").splitlines():
normalized = _normalize_outline_heading_line(raw_line)
if not normalized:
continue
match = re.match(r"^(#{1,6})\s+(.+?)\s*$", normalized)
if not match:
continue
title = match.group(2).strip()
specs.append((len(match.group(1)), title, normalized))
return specs
def _outline_adherence_error(
report: str,
custom_outline: str | None,
*,
structure_mode: str,
) -> str | None:
"""Describe a selected-outline contract breach, or return ``None``.
Fixed outlines require every non-placeholder heading with its original
level and order. Adaptive outlines permit material-driven changes, but a
result with zero overlap against every core template chapter means the
selected template was catastrophically ignored and warrants one rewrite.
Extra generated headings remain allowed because the shared report contract
may append a references/limitations section.
"""
if not (custom_outline or "").strip():
return None
if structure_mode != "fixed":
adaptive_specs = _fallback_outline_specs(
custom_outline,
structure_mode="adaptive",
)
if adaptive_specs:
first, *remaining = adaptive_specs
first_is_title = (
_normalized_heading_title(first[1]) in _OUTLINE_TITLE_PLACEHOLDERS
or (remaining and first[0] < min(level for level, _title, _line in remaining))
)
if first_is_title:
adaptive_specs = remaining
core = [
(title, _adaptive_heading_key(title))
for _level, title, _line in adaptive_specs
if _adaptive_heading_key(title) not in _OUTLINE_TITLE_PLACEHOLDERS
]
core = [(title, key) for title, key in core if key]
if not core:
return None
signal_core = [item for item in core if item[1] not in _ADAPTIVE_GENERIC_HEADINGS]
expected = signal_core or core
actual_titles = {
_adaptive_heading_key(title)
for _level, title, _line in _outline_heading_specs(report)
}
if any(key in actual_titles for _title, key in expected):
return None
shown = "、".join(title for title, _key in expected[:6])
return f"弹性大纲未采用任何核心章节(应至少保留一项):{shown}"
required = [
(level, title)
for level, title, _line in _outline_heading_specs(custom_outline)
if _normalized_heading_title(title) not in _OUTLINE_TITLE_PLACEHOLDERS
]
if not required:
return None
actual_normalized = [
(level, _normalized_heading_title(title))
for level, title, _line in _outline_heading_specs(report)
]
cursor = 0
missing: list[str] = []
for level, title in required:
expected = (level, _normalized_heading_title(title))
try:
cursor = actual_normalized.index(expected, cursor) + 1
except ValueError:
missing.append(f"{'#' * level} {title}")
if not missing:
return None
shown = "、".join(missing[:8])
suffix = f"等 {len(missing)} 项" if len(missing) > 8 else ""
return f"固定大纲缺少或打乱以下标题:{shown}{suffix}"
def _outline_repair_messages(
completion_messages: list[dict[str, str]],
*,
outline: str | None,
structure_mode: str,
validation_error: str,
) -> list[dict[str, str]]:
"""Strengthen the sole retry after a selected-outline validation failure."""
messages = [dict(message) for message in completion_messages]
if structure_mode == "fixed":
repair = (
"\n\n【固定大纲结构校验失败】\n"
f"{validation_error}\n"
"请从头完整重写报告。下面大纲中的每一个非占位标题都必须逐字保留,并严格按原顺序和层级出现;"
"不得改名、合并、省略、降级、升级或调换。材料不足也要保留原标题,并在该节明确写明待补资料。\n"
f"<required-fixed-outline>\n{outline or ''}\n</required-fixed-outline>"
)
else:
repair = (
"\n\n【弹性大纲结构校验失败】\n"
f"{validation_error}\n"
"请从头完整重写报告。可以按材料合并或省略没有依据的小节,但不能完全改用通用报告套式;"
"至少保留下列大纲中的一个核心章节,并围绕所选模板组织其余内容。\n"
f"<selected-adaptive-outline>\n{outline or ''}\n</selected-adaptive-outline>"
)
if messages:
messages[-1]["content"] = str(messages[-1].get("content") or "") + repair
else:
messages.append({"role": "user", "content": repair.lstrip()})
return messages
def _report_outline_instruction(custom_outline: str | None) -> str:
"""Render a user outline as the required skeleton, not as extra commands."""
if not custom_outline:
@@ -1406,7 +1692,98 @@ def _error_diagnostic(stage: str, exc: Exception, *, query: str | None = None) -
return result
def _build_report_fallback(query: str, context_text: str, source_count: int) -> str:
def _fallback_outline_specs(
custom_outline: str | None,
*,
structure_mode: str,
) -> list[tuple[int, str, str]]:
"""Keep every fixed heading, or only the adaptive outline's core levels."""
specs = _outline_heading_specs(custom_outline)
if structure_mode == "fixed" or len(specs) < 2:
return specs
# In adaptive mode, preserve the document title plus the major chapter
# level. Subsections may legitimately be merged when evidence is thin.
first, *remaining = specs
first_is_title = (
_normalized_heading_title(first[1]) in _OUTLINE_TITLE_PLACEHOLDERS
or (remaining and first[0] < min(level for level, _title, _line in remaining))
)
chapters = remaining if first_is_title else specs
if not chapters:
return specs
major_level = min(level for level, _title, _line in chapters)
selected = [spec for spec in chapters if spec[0] == major_level]
return ([first] if first_is_title else []) + selected
def _build_outline_fallback(
query: str,
context_text: str,
source_count: int,
*,
custom_outline: str,
structure_mode: str,
) -> str | None:
"""Create a conservative report that still honors the selected structure."""
specs = _fallback_outline_specs(custom_outline, structure_mode=structure_mode)
if not specs:
return None
bounded_context = (context_text or "").strip()
if len(bounded_context) > 6000:
bounded_context = bounded_context[:6000].rstrip() + "\n\n(其余已确认材料未在保底正文中展开。)"
quoted_context = "\n".join(f"> {line}" if line else ">" for line in bounded_context.splitlines())
context_written = False
output: list[str] = []
for index, (level, title, markdown_line) in enumerate(specs):
normalized_title = _normalized_heading_title(title)
if normalized_title in _OUTLINE_TITLE_PLACEHOLDERS:
output.append(f"{'#' * level} {query}")
continue
output.append(markdown_line)
if any(marker in title for marker in ("参考资料", "参考来源", "参考文献")):
output.append(
"- 本次未收集到可直接列示的外部来源;后续补充资料时应逐项核验并更新本节。"
if source_count <= 0
else "- 已确认材料保留在本次研究记录中;自动撰写未完成,需在后续重写时补齐逐项来源。"
)
continue
if source_count > 0 and bounded_context and not context_written:
output.append(
f"本次研究已收集到 {source_count} 条来源。由于自动撰写服务未能按所选结构完成成稿,"
"现先保留所选模板并附上已确认材料摘录,供继续核验和重写。\n\n"
f"{quoted_context}"
)
context_written = True
continue
if source_count <= 0 and index == 0:
output.append(
f"本方案围绕“{query}”按所选模板保留完整结构。当前没有可引用的外部研究材料,"
"本节仅给出待核验的推进框架,不把缺失事实写成确定结论。"
)
else:
output.append(
"现有材料不足以形成可靠的专项结论。本节按所选模板保留,待补充可核验事实、"
"预算、责任主体和时间要求后完善;不得为填满结构而编造。"
)
return "\n\n".join(output).strip() + "\n"
def _build_report_fallback(
query: str,
context_text: str,
source_count: int,
*,
custom_outline: str | None = None,
structure_mode: str = "adaptive",
) -> str:
"""Build a non-refusal last-resort report from confirmed context.
This is deliberately conservative: it preserves a bounded extract of the
@@ -1414,6 +1791,17 @@ def _build_report_fallback(query: str, context_text: str, source_count: int) ->
preliminary, rather than inventing facts when the configured model cannot
complete either writing attempt.
"""
if custom_outline and (
outlined := _build_outline_fallback(
query,
context_text,
source_count,
custom_outline=custom_outline,
structure_mode=structure_mode,
)
):
return outlined
if source_count <= 0:
return f"""# {query}