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

@@ -795,15 +795,14 @@ def apply_agent_only_prompt_template(
# 对话行为照旧;只有开始写 md 时才走 deep_research_report。资料不足也直接开写。
writing_mode_section = """<writing_mode>
Writing mode is enabled for this run (first report turn — no existing artifact).
- HARD RULE: the report file is produced ONLY by the `deep_research_report` tool — never write it yourself with `write_file`, never `present_files` it.
- HARD RULE: the report file is produced ONLY by the `deep_research_report` tool — call it directly at delivery time and do NOT draft the full report body first (not in chat, not inside tool arguments). File-writing tools (`write_file` / `str_replace`) and the `bash` tool are NOT available in this run; any hand-write attempt is intercepted and redirected to `deep_research_report`. Never `present_files` the report.
- Work normally (chat, search, clarify) until the report is due; then produce it by calling the `deep_research_report` tool:
`topic` = the user's subject copied faithfully, `focus` = the user's extra requirements (if any).
`topic` = the user's subject (the refined thesis after clarifications, not the raw first message), `focus` = the extra requirements from the user's answers (type, length, tone, must-cover points).
The tool reuses the conversation's collected materials and writes the full report (general-analysis structure with references)
to `/mnt/user-data/outputs/report.md`, presented automatically.
- If harvested materials are thin, still call `deep_research_report` once and let it write immediately.
Do NOT ask the user whether to search for more, and do NOT call `ask_clarification` for insufficient materials.
- Search with your normal conversation tools. `deep_research_report` only writes the report file from materials already in this conversation; it does not search.
- Do NOT write the report body yourself with `write_file`, and do NOT call `present_files` for it.
- After the tool returns successfully: do NOT call `read_file`, `write_file`, `str_replace`, or `present_files` on the report. Reply briefly (2-3 sentences) summarizing the core conclusions. Do not rewrite even if you think materials were thin.
- Only if the tool fails, tell the user and offer to retry.
</writing_mode>
@@ -1479,20 +1478,18 @@ def apply_prompt_template(
Writing mode is enabled for this run (first report turn — no existing artifact).
**Mandatory Output Contract**
- HARD RULE: the report file is produced ONLY by the `deep_research_report` tool. You are FORBIDDEN from writing the report yourself — do not call `write_file`
(or any file-writing tool) to create the report, and do not call `present_files` for it.
- HARD RULE: the report file is produced ONLY by the `deep_research_report` tool. Call the tool directly when the report is due — do NOT draft the full report body first (not in chat, not inside tool arguments). File-writing tools (`write_file` / `str_replace`) and the `bash` tool are NOT available in this run; any hand-write attempt is intercepted and redirected to `deep_research_report`. Do not call `present_files` for the report.
- Treat the user's request as a research-report writing task. Work normally until delivery time: discuss, search, and clarify exactly as you always would.
- When the report file is due — the user asks to start writing, or you have finished gathering what the conversation needs — call the `deep_research_report` tool:
- `topic`: the user's subject/thesis, copied faithfully.
- `focus`: the user's extra requirements (length, tone, must-cover points), if any.
- When the report file is due — the user asks to start writing, or you have finished gathering what the conversation needs — call the `deep_research_report` tool directly:
- `topic`: the user's subject/thesis — the refined one settled in the clarifications, not the raw first message.
- `focus`: the extra requirements from the user's answers (type, length, tone, must-cover points), if any.
- The tool reuses the materials already collected in this conversation, then writes the full
report (general-analysis structure with a numbered reference section) to `/mnt/user-data/outputs/report.md`, presented automatically.
- If harvested materials are thin, still call `deep_research_report` once and let it start the writing pipeline immediately.
Do NOT ask the user whether to search for more materials, and do NOT call `ask_clarification` for that reason.
- Search with your normal conversation tools. `deep_research_report` only writes the report file from materials already in this conversation; it does not search.
- Do NOT write the report body yourself with `write_file`, and do NOT call `present_files` for the report — the tool already handles both.
- While the tool runs, stay silent. After it returns successfully: do NOT call `read_file`, `write_file`, `str_replace`, or `present_files` on the report. Reply briefly (2-3 sentences) summarizing the report's core conclusions and point the user to the Markdown artifact. Do not duplicate the report inline, and do not rewrite even if you think materials were thin.
- Only if the tool fails, tell the user what went wrong and offer to retry; do not silently fall back to writing the report by hand unless the user explicitly asks.
- Only if the tool fails, tell the user what went wrong and offer to retry; do not silently fall back to writing the report by hand.
</writing_mode>
"""
else: