sync: 鍚屾灞€鍩熺綉鏈€鏂颁唬鐮?(source master @ b69568e4)

This commit is contained in:
gozeros committed 2026-10-21 14:51:31 +08:00
1 parent 4f7f07504a
commit 8bc8b0e082
1651 files changed
+278599 -11416

No files matched your search

@@ -6,6 +6,10 @@ from types import SimpleNamespace
from langchain_core.messages import AIMessage, HumanMessage, ToolMessage
from deerflow.agents.analysis_project import (
SOLUTION_REQUIREMENTS_CONFIRMATION_TOKEN,
SOLUTION_REQUIREMENTS_FORCE_TOKEN,
)
from deerflow.agents.middlewares.writing_mode_report_middleware import (
TOOL_NAME,
WritingModeReportMiddleware,
@@ -30,6 +34,7 @@ def test_only_report_md_is_writing_mode_follow_up() -> None:
def test_handwritten_report_detects_outputs_markdown() -> None:
assert is_handwritten_report_call({"name": "write_file", "args": {"path": "/mnt/user-data/outputs/report.md"}, "id": "c1"})
assert is_handwritten_report_call({"name": "begin_write_file", "args": {"path": "/mnt/user-data/outputs/report.md"}, "id": "c1"})
assert is_handwritten_report_call({"name": "write_file", "args": {"path": "/mnt/user-data/outputs/analysis.md"}, "id": "c1"})
assert is_handwritten_report_call({"name": "str_replace", "args": {"path": "report.md"}, "id": "c1"})
assert not is_handwritten_report_call({"name": "write_file", "args": {"path": "/mnt/user-data/workspace/notes.md"}, "id": "c1"})
@@ -47,6 +52,47 @@ def test_extract_topic_uses_latest_visible_user_text() -> None:
assert topic.startswith("写一份新能源汽车产业报告")
def test_extract_topic_skips_solution_generation_control_message() -> None:
topic = extract_report_topic(
[
HumanMessage(content="为普通大众写一份 AI 趋势与个人应对方案"),
AIMessage(content="需求已经足够。"),
HumanMessage(
content=(
"直接生成方案"
f"{SOLUTION_REQUIREMENTS_CONFIRMATION_TOKEN}"
f"{SOLUTION_REQUIREMENTS_FORCE_TOKEN}"
)
),
]
)
assert topic == "为普通大众写一份 AI 趋势与个人应对方案"
def test_extract_topic_skips_missing_button_complaint() -> None:
topic = extract_report_topic(
[
HumanMessage(content="为普通大众写一份 AI 趋势与个人应对方案"),
AIMessage(content="我已理解需求。"),
HumanMessage(content="结束问答并生成方案按钮 没有按钮,我走不了下一步"),
HumanMessage(content=f"直接生成方案{SOLUTION_REQUIREMENTS_FORCE_TOKEN}"),
]
)
assert topic == "为普通大众写一份 AI 趋势与个人应对方案"
def test_extract_topic_skips_missing_report_display_complaint() -> None:
topic = extract_report_topic(
[
HumanMessage(content="为普通大众写一份 AI 趋势与个人应对方案"),
AIMessage(content="正在处理。"),
HumanMessage(content="我根本没看到报告,右侧没显示,报告在哪?"),
HumanMessage(content=f"重新生成方案{SOLUTION_REQUIREMENTS_FORCE_TOKEN}"),
]
)
assert topic == "为普通大众写一份 AI 趋势与个人应对方案"
def test_rewrite_write_file_to_pipeline() -> None:
last = AIMessage(
content="",
@@ -132,6 +178,189 @@ def test_middleware_allows_chat_before_writing() -> None:
assert middleware.after_model(state, runtime=SimpleNamespace()) is None
def test_required_pipeline_replaces_plain_fake_completion() -> None:
middleware = WritingModeReportMiddleware(require_pipeline=True)
state = {
"messages": [
HumanMessage(content="写一份新能源汽车报告"),
AIMessage(content="方案已经生成并展示在右侧。"),
],
"artifacts": [],
}
update = middleware.after_model(state, runtime=SimpleNamespace())
assert update is not None
patched = update["messages"][0]
assert patched.content == ""
assert len(patched.tool_calls) == 1
assert patched.tool_calls[0]["name"] == TOOL_NAME
assert patched.tool_calls[0]["args"]["topic"] == "写一份新能源汽车报告"
def test_required_pipeline_prefers_validated_intent_over_button_complaint() -> None:
middleware = WritingModeReportMiddleware(
require_pipeline=True,
solution_intent={
"goal": "帮助普通大众理解 AI 发展趋势及个人影响",
"audience": "不具备技术背景的普通大众",
"success_outcome": "读者形成清晰认知并能采取行动",
"scope": "技术突破、就业、生活与应对策略",
"constraints": "不展开产业竞争和实现细节",
"deliverable": "结构化 Markdown 报告",
"acceptance_criteria": "通俗、可靠、可操作",
"open_questions": ["篇幅尚未确认"],
},
)
state = {
"messages": [
HumanMessage(content="结束问答并生成方案按钮 没有按钮"),
HumanMessage(content=f"直接生成方案{SOLUTION_REQUIREMENTS_FORCE_TOKEN}"),
AIMessage(content="方案已经生成。"),
],
"artifacts": [],
}
update = middleware.after_model(state, runtime=SimpleNamespace())
assert update is not None
args = update["messages"][0].tool_calls[0]["args"]
assert args["topic"] == "帮助普通大众理解 AI 发展趋势及个人影响"
assert "结构化 Markdown 报告" in args["focus"]
assert "暂定假设" in args["focus"]
def test_required_pipeline_replaces_clarification_in_generation_turn() -> None:
middleware = WritingModeReportMiddleware(require_pipeline=True)
state = {
"messages": [
HumanMessage(content="写一份新能源汽车报告"),
AIMessage(
content="",
tool_calls=[
{
"id": "q1",
"name": "ask_clarification",
"args": {
"question": "预算是多少?",
"clarification_type": "missing_info",
"options": ["100 万", "500 万", "其他/我补充"],
},
"type": "tool_call",
}
],
),
],
"artifacts": [],
}
update = middleware.after_model(state, runtime=SimpleNamespace())
assert update is not None
patched = update["messages"][0]
assert [call["name"] for call in patched.tool_calls] == [TOOL_NAME]
def test_required_pipeline_accepts_completion_only_with_report_artifact() -> None:
middleware = WritingModeReportMiddleware(require_pipeline=True)
messages = [
HumanMessage(content="写一份新能源汽车报告"),
AIMessage(
content="",
tool_calls=[
{"id": "c1", "name": TOOL_NAME, "args": {"topic": "课题"}, "type": "tool_call"}
],
),
ToolMessage(
content="研究报告已完成:已写入 report.md。",
tool_call_id="c1",
name=TOOL_NAME,
),
AIMessage(content="方案已经生成并展示在右侧。"),
]
missing = middleware.after_model(
{"messages": messages, "artifacts": []},
runtime=SimpleNamespace(),
)
assert missing is not None
assert missing["jump_to"] == "end"
assert missing["messages"][0].tool_calls == []
assert "未成功" in missing["messages"][0].content
completed = middleware.after_model(
{"messages": messages, "artifacts": ["/mnt/user-data/outputs/report.md"]},
runtime=SimpleNamespace(),
)
assert completed is None
def test_required_pipeline_failure_ends_without_automatic_retry() -> None:
middleware = WritingModeReportMiddleware(require_pipeline=True)
messages = [
HumanMessage(content=f"直接生成方案{SOLUTION_REQUIREMENTS_FORCE_TOKEN}"),
AIMessage(
content="",
tool_calls=[
{"id": "c1", "name": TOOL_NAME, "args": {"topic": "课题"}, "type": "tool_call"}
],
),
ToolMessage(
content="Error: 深度研究报告生成失败:模型暂时不可用。",
tool_call_id="c1",
name=TOOL_NAME,
),
AIMessage(
content="我再试一次。",
tool_calls=[
{"id": "c2", "name": TOOL_NAME, "args": {"topic": "课题"}, "type": "tool_call"}
],
),
]
update = middleware.after_model(
{"messages": messages, "artifacts": []},
runtime=SimpleNamespace(),
)
assert update is not None
assert update["jump_to"] == "end"
patched = update["messages"][0]
assert patched.tool_calls == []
assert "未成功" in patched.content
assert "重新生成方案" in patched.content
def test_new_user_retry_is_not_blocked_by_prior_pipeline_failure() -> None:
middleware = WritingModeReportMiddleware(require_pipeline=True)
messages = [
HumanMessage(content=f"直接生成方案{SOLUTION_REQUIREMENTS_FORCE_TOKEN}"),
AIMessage(
content="",
tool_calls=[
{"id": "c1", "name": TOOL_NAME, "args": {"topic": "课题"}, "type": "tool_call"}
],
),
ToolMessage(
content="Error: 深度研究报告生成失败:模型暂时不可用。",
tool_call_id="c1",
name=TOOL_NAME,
),
AIMessage(content="本轮生成失败,请重试。"),
HumanMessage(content=f"重新生成方案{SOLUTION_REQUIREMENTS_FORCE_TOKEN}"),
AIMessage(content="开始重试。"),
]
update = middleware.after_model(
{"messages": messages, "artifacts": []},
runtime=SimpleNamespace(),
)
assert update is not None
assert update.get("jump_to") is None
assert update["messages"][0].tool_calls[0]["name"] == TOOL_NAME
def test_middleware_rewrites_write_file_on_after_model() -> None:
middleware = WritingModeReportMiddleware()
state = {
@@ -198,6 +427,45 @@ def test_pipeline_success_is_detected() -> None:
ToolMessage(content="Error: 深度研究报告生成失败。", tool_call_id="c1", name=TOOL_NAME),
]
assert pipeline_already_succeeded(failed) is False
refused = [
*messages[:-1],
ToolMessage(
content="当前仍处于正常问答阶段,不能生成首份方案。",
tool_call_id="c1",
name=TOOL_NAME,
),
]
assert pipeline_already_succeeded(refused) is False
def test_policy_refusal_never_injects_completed_report_reminder() -> None:
middleware = WritingModeReportMiddleware()
state = {
"messages": [
HumanMessage(content="开始"),
AIMessage(
content="",
tool_calls=[
{"id": "c1", "name": TOOL_NAME, "args": {"topic": "当前课题"}, "type": "tool_call"}
],
),
ToolMessage(
content="当前仍处于正常问答阶段,不能生成首份方案。请继续自然理解用户意图。",
tool_call_id="c1",
name=TOOL_NAME,
),
HumanMessage(content="结束问答并生成方案按钮 没有按钮"),
AIMessage(
content="",
tool_calls=[
{"id": "c2", "name": TOOL_NAME, "args": {"topic": "当前课题"}, "type": "tool_call"}
],
),
],
"artifacts": [],
}
assert middleware.after_model(state, runtime=SimpleNamespace()) is None
def test_reread_detects_outputs_markdown() -> None:
@@ -273,7 +541,8 @@ def test_middleware_nudges_summary_after_successful_pipeline() -> None:
}
],
),
]
],
"artifacts": ["/mnt/user-data/outputs/report.md"],
}
update = middleware.after_model(state, runtime=SimpleNamespace())
@@ -313,7 +582,8 @@ def test_middleware_ends_turn_after_ignored_summary_nudge() -> None:
}
],
),
]
],
"artifacts": ["/mnt/user-data/outputs/report.md"],
}
update = middleware.after_model(state, runtime=SimpleNamespace())
@@ -335,7 +605,7 @@ def test_wrap_tool_call_rejects_rerun_after_success() -> None:
]
request = SimpleNamespace(
tool_call={"id": "c2", "name": TOOL_NAME, "args": {"topic": "课题"}},
state={"messages": prior},
state={"messages": prior, "artifacts": ["/mnt/user-data/outputs/report.md"]},
)
called = {"handler": False}
@@ -352,6 +622,62 @@ def test_wrap_tool_call_rejects_rerun_after_success() -> None:
assert "请立即调用" not in result.content
def test_wrap_tool_call_does_not_trust_success_text_without_artifact() -> None:
middleware = WritingModeReportMiddleware()
prior = [
HumanMessage(content="写一份报告"),
AIMessage(
content="",
tool_calls=[
{"id": "c1", "name": TOOL_NAME, "args": {"topic": "课题"}, "type": "tool_call"}
],
),
ToolMessage(
content="研究报告已完成:已写入 report.md。",
tool_call_id="c1",
name=TOOL_NAME,
),
]
request = SimpleNamespace(
tool_call={"id": "c2", "name": TOOL_NAME, "args": {"topic": "课题"}},
state={"messages": prior, "artifacts": []},
)
expected = ToolMessage(content="ran", tool_call_id="c2", name=TOOL_NAME)
result = middleware.wrap_tool_call(request, lambda _request: expected)
assert result is expected
def test_wrap_tool_call_allows_new_workbench_write_after_success() -> None:
middleware = WritingModeReportMiddleware()
prior = [
HumanMessage(content="写一份报告"),
AIMessage(
content="",
tool_calls=[{"id": "c1", "name": TOOL_NAME, "args": {"topic": "课题"}, "type": "tool_call"}],
),
ToolMessage(content="研究报告已完成:已写入 report.md。", tool_call_id="c1", name=TOOL_NAME),
HumanMessage(
content="请立即开始撰写研究报告。按新结构撰写研究报告。",
additional_kwargs={"hide_from_ui": True, "workbench_write_now": True, "workbench_topic": "课题"},
),
AIMessage(
content="",
tool_calls=[{"id": "c2", "name": TOOL_NAME, "args": {"topic": "课题"}, "type": "tool_call"}],
),
]
request = SimpleNamespace(
tool_call={"id": "c2", "name": TOOL_NAME, "args": {"topic": "课题"}},
state={"messages": prior, "artifacts": ["/mnt/user-data/outputs/report.md"]},
)
expected = ToolMessage(content="ran", tool_call_id="c2", name=TOOL_NAME)
result = middleware.wrap_tool_call(request, lambda _request: expected)
assert result is expected
def test_wrap_tool_call_rejects_reread_after_success() -> None:
middleware = WritingModeReportMiddleware()
prior = [
@@ -368,7 +694,7 @@ def test_wrap_tool_call_rejects_reread_after_success() -> None:
"name": "read_file",
"args": {"path": "/mnt/user-data/outputs/report.md"},
},
state={"messages": prior},
state={"messages": prior, "artifacts": ["/mnt/user-data/outputs/report.md"]},
)
called = {"handler": False}
@@ -530,7 +856,8 @@ def test_bash_write_after_success_is_dropped() -> None:
}
],
),
]
],
"artifacts": ["/mnt/user-data/outputs/report.md"],
}
update = middleware.after_model(state, runtime=SimpleNamespace())
@@ -588,6 +915,7 @@ def test_wrap_model_call_strips_handwrite_tools_from_binding() -> None:
make_request(
[
SimpleNamespace(name="write_file"),
SimpleNamespace(name="begin_write_file"),
SimpleNamespace(name="web_search"),
SimpleNamespace(name="str_replace"),
SimpleNamespace(name="bash"),
@@ -694,7 +1022,8 @@ def test_second_offense_after_nudge_gets_fallback_summary() -> None:
}
],
),
]
],
"artifacts": ["/mnt/user-data/outputs/report.md"],
}
update = middleware.after_model(state, runtime=SimpleNamespace())