fix: writing mode first-report allows only the pipeline to write the report

- Strip write_file/str_replace/bash from the model binding in the
  first-report run: after_model rewrites only fire after a full model
  response, so any bound write tool let weak models draft the whole
  essay into tool args (write_file content or bash cat>/python
  heredocs) for tens of invisible seconds before interception
- Hallucinated hand-writes are still handled: after_model rewrites
  bash cat >/python open('w')/tee and leftover write calls to
  deep_research_report; wrap_tool_call rejects whatever reaches the
  tool layer; post-success writes are dropped, nudged to a 2-3
  sentence summary, and patched with a fallback closing message so
  the turn never ends silently
- The rewritten pipeline call carries topic = original request
  (clarification answers skipped) plus focus = clarification answers,
  so the pipeline writes the clarified subject instead of the raw
  prompt text; prompts updated to match (tools unavailable, call the
  tool directly, do not draft first)
- Frontend write-step interception and streaming preview scoping now
  applies only in writing mode; normal mode shows/previews writes
  exactly as before
This commit is contained in:
董宏凯 committed 2026-08-30 15:53:43 +08:00
1 parent 49c01cb72b
commit 4964967295
12 files changed
+1271 -133

No files matched your search

@@ -1,19 +1,35 @@
"""Redirect writing-mode Markdown deliverables onto ``deep_research_report``.
Writing mode still allows normal chat, search, and clarification. The pipeline
starts only when the model begins writing the report file (``write_file`` /
``str_replace`` of ``report.md`` or any Markdown under
``/mnt/user-data/outputs/``): those calls are rewritten to
``deep_research_report``. Sibling ``present_files`` of the same report are
dropped because the pipeline presents the file itself.
Writing mode still allows normal chat, search, and clarification. The report
only comes from the pipeline, and no hand-write path can even start:
1. ``wrap_model_call`` removes ``write_file`` / ``str_replace`` / ``bash``
from the model binding for the whole first-report run. ``after_model``
rewrites can only fire AFTER a model response completes, so any write tool
left bound lets weak models stream the full essay into its arguments first
(``write_file`` content or a bash ``cat > report.md << EOF`` heredoc) for
tens of seconds, invisible to the user. bash is the only execution tool
that writes files, so excluding it too leaves NO tool the model could use
to start a file write — at delivery time the only path is the
``deep_research_report`` call itself.
2. Hallucinated calls (emitted despite not being bound) are still handled:
bash heredoc / ``python open(..., 'w')`` / ``tee`` writes and leftover
``write_file`` / ``str_replace`` calls are rewritten to the pipeline by
``after_model``, and ``wrap_tool_call`` rejects whatever reaches the tool
layer.
The rewritten pipeline call carries ``topic`` = the user's original request
(clarification answers skipped) plus ``focus`` = the clarification answers
(direction/type/length), so the pipeline writes the refined subject instead of
the raw prompt text.
After the pipeline has succeeded once in this thread, further
``deep_research_report`` / hand-written report writes / ``read_file`` of the
report are dropped. The first time that happens, a hidden summary nudge is
injected and the graph jumps back to the model so it can write the 2–3
sentence wrap-up. If it still tries to loop after that nudge, the turn ends.
Ending immediately on the first rewrite attempt left the model's
"I will rewrite this" text as the visible summary.
sentence wrap-up. If it still tries to loop after that nudge, the dropped
message is patched to a fallback summary and the turn ends — the user always
sees a closing message.
This middleware never sets ``tool_choice``. Thinking-mode providers (Aliyun
compatible-mode) reject ``required`` / object ``tool_choice`` with HTTP 400.
@@ -23,12 +39,14 @@ from __future__ import annotations
import json
import logging
import re
from collections.abc import Awaitable, Callable
from copy import deepcopy
from typing import Any, override
from langchain.agents import AgentState
from langchain.agents.middleware import AgentMiddleware, hook_config
from langchain.agents.middleware.types import ModelCallResult, ModelRequest, ModelResponse
from langchain_core.messages import AIMessage, HumanMessage, ToolMessage
from langgraph.prebuilt.tool_node import ToolCallRequest
from langgraph.runtime import Runtime
@@ -42,24 +60,20 @@ TOOL_NAME = "deep_research_report"
_WRITE_TOOLS = frozenset({"write_file", "str_replace"})
_READ_TOOLS = frozenset({"read_file", "read_multiple_files"})
_PRESENT_TOOL = "present_files"
_BASH_TOOL = "bash"
_OUTPUTS_PREFIX = "/mnt/user-data/outputs"
_MAX_TOPIC_CHARS = 500
_MAX_FOCUS_CHARS = 600
_NUDGE_KWARG = "writing_mode_summary_nudge"
_REJECT_HANDWRITTEN = (
"写作模式的报告文件必须由 `deep_research_report` 生成,禁止用 write_file / "
"str_replace 手写报告。请立即调用 `deep_research_report`(topic=用户课题原文)。"
)
_FALLBACK_SUMMARY = "写作管线已完成,报告已写入 /mnt/user-data/outputs/report.md 并展示在右侧沙箱,可直接查看。如需调整内容,请在下一条消息中告诉我具体要求。"
_REJECT_HANDWRITTEN = "写作模式的报告文件必须由 `deep_research_report` 生成,禁止用 write_file / str_replace / bash 命令(cat > 重定向、python open(..., 'w')、tee 等)手写报告。请立即调用 `deep_research_report`(topic=用户课题原文)。"
_REJECT_AFTER_SUCCESS = (
"写作管线已经完成,报告已写入 /mnt/user-data/outputs/report.md 并展示给用户。"
"禁止再次调用 deep_research_report,也禁止用 write_file / str_replace / "
"禁止再次调用 deep_research_report,也禁止用 write_file / str_replace / bash / "
"read_file 读写该报告。请只用 2-3 句话总结核心结论后结束本轮,不要再调用任何文件或写作工具。"
)
_SUMMARY_NUDGE = (
"<system_reminder>\n"
"写作管线已经完成,报告已写入 /mnt/user-data/outputs/report.md 并展示给用户。\n"
"请只用 2-3 句话总结核心结论,不要再调用任何工具,不要阅读或重写该文件。\n"
"不要评价材料是否不足,不要计划重写。\n"
"</system_reminder>"
"<system_reminder>\n写作管线已经完成,报告已写入 /mnt/user-data/outputs/report.md 并展示给用户。\n请只用 2-3 句话总结核心结论,不要再调用任何工具,不要阅读或重写该文件。\n不要评价材料是否不足,不要计划重写。\n</system_reminder>"
)
@@ -119,11 +133,42 @@ def _is_outputs_markdown_path(path: str) -> bool:
return relative == "outputs" or relative.startswith("outputs/")
# bash 手写报告的识别:写入动作(重定向 / tee / python 只写打开 / write_text)
# 的目标本身是交付物 Markdown。纯读取(mkdir、wc、cat 输出)不拦。
_BASH_MD_PATH = re.compile(r"""['"]([^'"]{0,200}\.md)['"]|(^|[\s;|&<>()=])([^\s;'"&|<>(){},]{0,200}\.md)\b""")
_BASH_MD_REDIRECT = re.compile(r">{1,2}\s*([^\s;|&>]{0,200}\.md)\b")
_BASH_MD_TEE = re.compile(r"\btee\s+(?:-[A-Za-z]+\s+)*([^\s;|&<>]{0,200}\.md)\b")
_BASH_MD_PY_WRITE = re.compile(r"""open\s*\(\s*['"][^'"]{0,200}\.md['"]\s*,\s*['"]w""")
def _bash_command_writes_report(command: str) -> bool:
"""True 当 bash 命令在向写作模式的 Markdown 交付物写入内容。
实测弱模型被禁用手写工具后会绕道 bash:``cat > report.md << 'EOF'``、
``python3 << PYEOF`` + ``open(..., 'w')``、``tee`` 等。按「写入动作的
目标(或命令提到的路径)是 outputs Markdown / report.md」判定;
mkdir、wc、cat 读取这类无写入动作的命令不受影响。
"""
if not command or ".md" not in command:
return False
for pattern in (_BASH_MD_REDIRECT, _BASH_MD_TEE):
if any(_is_outputs_markdown_path(match.group(1)) for match in pattern.finditer(command)):
return True
paths = [quoted or bare or "" for quoted, _, bare in _BASH_MD_PATH.findall(command)]
if not any(_is_outputs_markdown_path(path) for path in paths):
return False
return bool(_BASH_MD_PY_WRITE.search(command)) or "write_text" in command or "write_bytes" in command
def is_handwritten_report_call(tool_call: object) -> bool:
"""True when the model is about to hand-write the writing-mode Markdown file."""
if _tool_name(tool_call) not in _WRITE_TOOLS:
return False
return _is_outputs_markdown_path(_tool_path(tool_call))
name = _tool_name(tool_call)
if name in _WRITE_TOOLS:
return _is_outputs_markdown_path(_tool_path(tool_call))
if name == _BASH_TOOL:
args = _normalize_args(_tool_field(tool_call, "args", {}))
return _bash_command_writes_report(str(args.get("command") or ""))
return False
def is_report_present_call(tool_call: object) -> bool:
@@ -184,28 +229,6 @@ def _summary_nudge_message() -> HumanMessage:
)
def is_post_success_loop_call(tool_call: object) -> bool:
"""After a successful pipeline, re-reading the report is also a loop."""
return is_deliverable_loop_call(tool_call) or is_report_reread_call(tool_call)
def _already_nudged_summary(messages: list[Any]) -> bool:
for message in messages:
if not isinstance(message, HumanMessage):
continue
kwargs = getattr(message, "additional_kwargs", None) or {}
if kwargs.get(_NUDGE_KWARG):
return True
return False
def _summary_nudge_message() -> HumanMessage:
return HumanMessage(
content=_SUMMARY_NUDGE,
additional_kwargs={"hide_from_ui": True, _NUDGE_KWARG: True},
)
def _report_tool_succeeded(message: ToolMessage) -> bool:
if str(getattr(message, "name", "") or "") != TOOL_NAME:
return False
@@ -238,21 +261,58 @@ def _message_text(message: Any) -> str:
return str(content or "").strip()
def _is_clarification_result(message: Any) -> bool:
return isinstance(message, ToolMessage) and str(getattr(message, "name", "") or "") == "ask_clarification"
def extract_report_topic(messages: list[Any]) -> str:
"""Use the latest visible user text as ``deep_research_report.topic``."""
for message in reversed(messages):
if _is_visible_human(message):
text = " ".join(_message_text(message).split())
if text:
return text[:_MAX_TOPIC_CHARS]
"""Use the latest visible user text that is not a clarification answer.
澄清轮里用户对 ask_clarification 的回答(紧跟着澄清 ToolMessage 的人类
消息,如「方向:AI Agent;用途:学术期刊论文」)不是课题本身;拿它当
topic 会让管线给「回答本身」写报告。向上跳过所有回答,取真正的课题原文
(最新一条非回答的用户消息)。
"""
for index in range(len(messages) - 1, -1, -1):
message = messages[index]
if not _is_visible_human(message):
continue
if index > 0 and _is_clarification_result(messages[index - 1]):
continue
text = " ".join(_message_text(message).split())
if text:
return text[:_MAX_TOPIC_CHARS]
return "当前课题"
def _as_report_call(tool_call: object, topic: str) -> dict[str, Any]:
def extract_report_focus(messages: list[Any], topic: str = "") -> str:
"""Join the user's clarification answers as the pipeline's ``focus``.
澄清回答(方向/类型/篇幅/语气等)不是课题本身,但正是写作要求;
topic 取课题原文的同时把它们汇总进 focus,管线才能按澄清后的
精确主题(如「LLM 的能力边界…」+ 观点评论型 + 3000-5000 字)成稿,
而不是只拿原始请求一句泛写。
"""
parts: list[str] = []
for index, message in enumerate(messages):
if not _is_visible_human(message):
continue
if index == 0 or not _is_clarification_result(messages[index - 1]):
continue
text = " ".join(_message_text(message).split())
if text and text != topic:
parts.append(text)
return ";".join(parts).strip()[:_MAX_FOCUS_CHARS]
def _as_report_call(tool_call: object, topic: str, focus: str = "") -> dict[str, Any]:
call_id = str(_tool_field(tool_call, "id") or "")
args: dict[str, Any] = {"topic": topic}
if focus:
args["focus"] = focus
return {
"name": TOOL_NAME,
"args": {"topic": topic},
"args": args,
"id": call_id,
"type": "tool_call",
}
@@ -267,11 +327,7 @@ def _sync_raw_tool_calls(last: AIMessage, kept: list[Any]) -> dict[str, Any]:
additional_kwargs = dict(getattr(last, "additional_kwargs", {}) or {})
raw_calls = additional_kwargs.get("tool_calls")
kept_ids = _kept_call_ids(kept)
rewritten_by_id = {
str(_tool_field(call, "id") or ""): call
for call in kept
if _tool_field(call, "id")
}
rewritten_by_id = {str(_tool_field(call, "id") or ""): call for call in kept if _tool_field(call, "id")}
if isinstance(raw_calls, list) and raw_calls:
synced: list[Any] = []
for item in raw_calls:
@@ -312,12 +368,14 @@ def rewrite_handwritten_report_calls(
last: AIMessage,
*,
topic: str,
focus: str = "",
pipeline_already_done: bool = False,
) -> AIMessage | None:
"""Rewrite or drop report deliverable tool calls.
Before the pipeline has succeeded: rewrite the first hand-written report
write to ``deep_research_report`` and drop sibling presents.
write to ``deep_research_report`` (carrying ``topic`` + ``focus``) and drop
sibling presents.
After it has succeeded: drop every pipeline / hand-written / present /
report-reread call so the model cannot start another writing loop.
@@ -347,12 +405,10 @@ def rewrite_handwritten_report_calls(
changed = True
if redirected:
continue
rewritten.append(_as_report_call(call, topic))
rewritten.append(_as_report_call(call, topic, focus))
redirected = True
continue
if is_report_present_call(call) and (
has_pipeline or redirected or any(is_handwritten_report_call(item) for item in tool_calls)
):
if is_report_present_call(call) and (has_pipeline or redirected or any(is_handwritten_report_call(item) for item in tool_calls)):
changed = True
continue
rewritten.append(call)
@@ -371,10 +427,45 @@ def _messages_from_tool_request(request: ToolCallRequest) -> list[Any]:
class WritingModeReportMiddleware(AgentMiddleware[AgentState]):
"""Start the writing pipeline only when the model begins writing the Markdown file."""
"""The first-report run's report only comes from ``deep_research_report``.
``wrap_model_call`` 把 write_file / str_replace / bash 一并从模型绑定中
剥离(本中间件只在首报轮挂载)——after_model 的改写只能在整个模型响应
结束后触发,任何留在绑定里的写入口都会被弱模型用来先把全文起草进参数
(write_file 内容或 bash heredoc 命令),流式几十秒前端不可见后才被拦
截。bash 是唯一能写文件的执行类工具,剥离后模型没有任何可发起文件写
入的工具,交付只能直接调管线;幻觉出的调用仍由 after_model 改写/丢弃、
wrap_tool_call 拒绝兜底,管线成功后再写会被丢弃并保证一段收尾总结。
"""
state_schema = AgentState
@staticmethod
def _binding_tool_name(tool: Any) -> str:
if isinstance(tool, str):
return tool
return str(getattr(tool, "name", "") or "")
def _strip_handwrite_tools(self, request: ModelRequest) -> ModelRequest:
"""Remove ``write_file`` / ``str_replace`` / ``bash`` from the model binding.
``after_model`` 的改写只能在模型整个响应结束后触发,所以任何留在绑定里
的写入口都会被弱模型用来先把全文起草进参数(write_file 参数、bash
heredoc 命令),流式输出几十秒且前端不可见后才被拦截。bash 是唯一能
写文件的执行类工具,一并剥离后首报轮**不存在可发起文件写入的工具**,
交付只能直接调 ``deep_research_report``。ToolNode 仍持有全部工具;
模型幻觉出的调用由 after_model 改写/丢弃、wrap_tool_call 拒绝兜底。
"""
tools = list(getattr(request, "tools", None) or [])
if not tools:
return request
excluded = _WRITE_TOOLS | {_BASH_TOOL}
removed = sorted({self._binding_tool_name(tool) for tool in tools if self._binding_tool_name(tool) in excluded})
if not removed:
return request
logger.info("Writing-mode first-report run: removed hand-write tools %s from model binding", removed)
return request.override(tools=[tool for tool in tools if self._binding_tool_name(tool) not in excluded])
def _redirect_or_stop(self, state: AgentState) -> dict[str, Any] | None:
messages = list(state.get("messages") or [])
if not messages:
@@ -383,26 +474,38 @@ class WritingModeReportMiddleware(AgentMiddleware[AgentState]):
if not isinstance(last, AIMessage):
return None
already_done = pipeline_already_succeeded(messages)
topic = extract_report_topic(messages)
patched = rewrite_handwritten_report_calls(
last,
topic=extract_report_topic(messages),
topic=topic,
focus=extract_report_focus(messages, topic),
pipeline_already_done=already_done,
)
if patched is None:
return None
result: dict[str, Any] = {"messages": [patched]}
if already_done and not (patched.tool_calls or []):
if _already_nudged_summary(messages):
if already_done:
if not (patched.tool_calls or []):
if _already_nudged_summary(messages):
logger.info("Writing-mode pipeline already succeeded; summary nudge ignored, ending turn")
# 模型无视总结提示仍要继续读写:丢弃其调用后补一段兜底
# 总结再结束,保证用户总能看到收尾消息而不是静默结束。
if not _message_text(patched):
patched = patched.model_copy(update={"content": _FALLBACK_SUMMARY})
result["messages"] = [patched]
result["jump_to"] = "end"
return result
logger.info("Writing-mode pipeline already succeeded; dropping deliverable tools and nudging summary")
result["messages"] = [patched, _summary_nudge_message()]
result["jump_to"] = "model"
else:
redirected_names = sorted({_tool_name(call) for call in getattr(last, "tool_calls", None) or [] if is_handwritten_report_call(call)})
if redirected_names:
logger.info(
"Writing-mode pipeline already succeeded; summary nudge ignored, ending turn"
"Writing-mode: redirected hand-written report call(s) %s to %s",
redirected_names,
TOOL_NAME,
)
result["jump_to"] = "end"
return result
logger.info(
"Writing-mode pipeline already succeeded; dropping deliverable tools and nudging summary"
)
result["messages"] = [patched, _summary_nudge_message()]
result["jump_to"] = "model"
return result
def _reject_loop_tool(self, request: ToolCallRequest) -> ToolMessage | None:
@@ -427,6 +530,22 @@ class WritingModeReportMiddleware(AgentMiddleware[AgentState]):
def after_model(self, state: AgentState, runtime: Runtime) -> dict[str, Any] | None:
return self._redirect_or_stop(state)
@override
def wrap_model_call(
self,
request: ModelRequest,
handler: Callable[[ModelRequest], ModelResponse],
) -> ModelCallResult:
return handler(self._strip_handwrite_tools(request))
@override
async def awrap_model_call(
self,
request: ModelRequest,
handler: Callable[[ModelRequest], Awaitable[ModelResponse]],
) -> ModelCallResult:
return await handler(self._strip_handwrite_tools(request))
@hook_config(can_jump_to=["end", "model"])
@override
async def aafter_model(self, state: AgentState, runtime: Runtime) -> dict[str, Any] | None:
@@ -458,6 +577,7 @@ class WritingModeReportMiddleware(AgentMiddleware[AgentState]):
__all__ = [
"TOOL_NAME",
"WritingModeReportMiddleware",
"extract_report_focus",
"extract_report_topic",
"is_handwritten_report_call",
"is_post_success_loop_call",