Skip to content

JiuwenSwarm is an intelligent AI Agent built on openJiuwen. It extends the powerful capabilities of large language models directly to your fingertips through various communication apps you use daily.

Python

fix(deepresearch): keep stage updates out of chat

DL123204c5634509f0
diff --git a/jiuwenswarm/agents/harness/common/tools/deepresearch/stream_router.py b/jiuwenswarm/agents/harness/common/tools/deepresearch/stream_router.py
index 866dc21143..4d164b52df 100644
--- a/jiuwenswarm/agents/harness/common/tools/deepresearch/stream_router.py
+++ b/jiuwenswarm/agents/harness/common/tools/deepresearch/stream_router.py
@@ -393,7 +393,7 @@ def _stage_child_reasoning(stage: int, agent: str, display: tuple[str, str], con
}
-def _stage_snapshot_frames(state: RouterState, *, complete: bool = False) -> list[dict]:
+def _stage_snapshot_frames(state: RouterState) -> list[dict]:
tasks = []
for index, title in enumerate(DEEPRESEARCH_STAGES, start=1):
if state.stages_completed or index < state.current_stage:
@@ -410,41 +410,18 @@ def _stage_snapshot_frames(state: RouterState, *, complete: bool = False) -> lis
completed = len(DEEPRESEARCH_STAGES) if state.stages_completed else state.current_stage - 1
in_progress = 0 if state.stages_completed else 1
- task_update = {
- "event_type": "task.update",
- "tasks": tasks,
- "total_tasks": len(DEEPRESEARCH_STAGES),
- "completed_tasks": completed,
- "in_progress_tasks": in_progress,
- "pending_tasks": len(DEEPRESEARCH_STAGES) - completed - in_progress,
- }
- title = DEEPRESEARCH_STAGES[state.current_stage - 1]
- content = (
- f"[DeepResearch 阶段完成] Stage {state.current_stage}:{title}\n"
- if complete
- else f"[DeepResearch 阶段切换] 开始 Stage {state.current_stage}:{title}\n"
- )
- message = {
- "task_id": f"deepresearch_stage_{state.current_stage}",
- "task_content": title,
- "content": content,
- }
return [
- task_update,
- {"event_type": "chat.delta", **message},
+ {
+ "event_type": "task.update",
+ "tasks": tasks,
+ "total_tasks": len(DEEPRESEARCH_STAGES),
+ "completed_tasks": completed,
+ "in_progress_tasks": in_progress,
+ "pending_tasks": len(DEEPRESEARCH_STAGES) - completed - in_progress,
+ }
]
-def _stage_completion_frame(stage: int) -> dict:
- title = DEEPRESEARCH_STAGES[stage - 1]
- return {
- "event_type": "chat.delta",
- "task_id": f"deepresearch_stage_{stage}",
- "task_content": title,
- "content": f"[DeepResearch 阶段完成] Stage {stage}:{title}\n",
- }
-
-
def advance_stage(state: RouterState, stage: int, *, complete: bool = False) -> list[dict]:
"""Advance monotonically and emit every missing Stage-facing snapshot."""
if complete:
@@ -452,7 +429,7 @@ def advance_stage(state: RouterState, stage: int, *, complete: bool = False) ->
return []
state.current_stage = len(DEEPRESEARCH_STAGES)
state.stages_completed = True
- return _stage_snapshot_frames(state, complete=True)
+ return _stage_snapshot_frames(state)
if state.stages_completed or stage <= state.current_stage:
return []
@@ -461,8 +438,6 @@ def advance_stage(state: RouterState, stage: int, *, complete: bool = False) ->
frames: list[dict] = []
for next_stage in range(state.current_stage + 1, stage + 1):
- if state.current_stage > 0:
- frames.append(_stage_completion_frame(state.current_stage))
state.current_stage = next_stage
frames.extend(_stage_snapshot_frames(state))
return frames
diff --git a/tests/unit_tests/agentserver/test_deepresearch_stream_router.py b/tests/unit_tests/agentserver/test_deepresearch_stream_router.py
index 86e66e43be..5a915439aa 100644
--- a/tests/unit_tests/agentserver/test_deepresearch_stream_router.py
+++ b/tests/unit_tests/agentserver/test_deepresearch_stream_router.py
@@ -85,19 +85,11 @@ class _CountingList(list):
self.items_visited = 0
-def test_advance_stage_emits_status_only_to_task_list_and_foreground():
+def test_advance_stage_emits_status_only_to_task_list():
frames = advance_stage(RouterState(), 1)
- assert [frame["event_type"] for frame in frames] == [
- "task.update",
- "chat.delta",
- ]
- assert frames[1] == {
- "event_type": "chat.delta",
- "task_id": "deepresearch_stage_1",
- "task_content": "研究主题澄清",
- "content": "[DeepResearch 阶段切换] 开始 Stage 1:研究主题澄清\n",
- }
+ assert [frame["event_type"] for frame in frames] == ["task.update"]
+ _assert_stage(frames[0], 1)
def test_advance_stage_backfills_every_missing_stage_in_event_order():
@@ -106,12 +98,8 @@ def test_advance_stage_backfills_every_missing_stage_in_event_order():
frames = advance_stage(state, 4)
assert [frame["event_type"] for frame in frames] == [
- "chat.delta",
"task.update",
- "chat.delta",
- "chat.delta",
"task.update",
- "chat.delta",
]
updates = [
frame for frame in frames if frame["event_type"] == "task.update"
@@ -124,42 +112,26 @@ def test_advance_stage_backfills_every_missing_stage_in_event_order():
)
for update in updates
] == [3, 4]
- assert [
- frame["content"]
- for frame in frames
- if frame["event_type"] == "chat.delta"
- ] == [
- "[DeepResearch 阶段完成] Stage 2:大纲生成\n",
- "[DeepResearch 阶段切换] 开始 Stage 3:并行调研与章节撰写\n",
- "[DeepResearch 阶段完成] Stage 3:并行调研与章节撰写\n",
- "[DeepResearch 阶段切换] 开始 Stage 4:报告交付\n",
- ]
assert state.current_stage == 4
-def test_advance_stage_marks_previous_stage_complete_before_next_stage_starts():
+def test_advance_stage_marks_previous_stage_complete_in_next_snapshot():
state = RouterState(current_stage=1)
frames = advance_stage(state, 2)
- visible_stage_events = [
- frame["content"]
- for frame in frames
- if frame["event_type"] == "chat.delta"
- ]
- assert visible_stage_events == [
- "[DeepResearch 阶段完成] Stage 1:研究主题澄清\n",
- "[DeepResearch 阶段切换] 开始 Stage 2:大纲生成\n",
- ]
+ assert [frame["event_type"] for frame in frames] == ["task.update"]
_assert_stage(_stage_update(frames), 2)
-def test_stage_2_uses_outline_generation_title_on_all_surfaces():
- frames = advance_stage(RouterState(), 2)[-2:]
+def test_stage_2_uses_outline_generation_title_in_task_list():
+ frames = advance_stage(RouterState(), 2)
- assert frames[0]["tasks"][1]["task_content"] == "大纲生成"
- assert frames[1]["task_content"] == "大纲生成"
- assert frames[1]["content"] == "[DeepResearch 阶段切换] 开始 Stage 2:大纲生成\n"
+ assert [frame["event_type"] for frame in frames] == [
+ "task.update",
+ "task.update",
+ ]
+ assert frames[-1]["tasks"][1]["task_content"] == "大纲生成"
def test_advance_stage_completion_keeps_all_four_completed_tasks_visible():
@@ -168,20 +140,16 @@ def test_advance_stage_completion_keeps_all_four_completed_tasks_visible():
frames = advance_stage(state, 4, complete=True)
- assert [frame["event_type"] for frame in frames] == [
- "task.update",
- "chat.delta",
- ]
+ assert [frame["event_type"] for frame in frames] == ["task.update"]
update = frames[0]
assert len(update["tasks"]) == 4
assert [task["task_id"] for task in update["tasks"]] == [
f"deepresearch_stage_{index}" for index in range(1, 5)
]
assert all(task["status"] == "completed" for task in update["tasks"])
- assert frames[1]["content"] == "[DeepResearch 阶段完成] Stage 4:报告交付\n"
-def test_advance_stage_does_not_repeat_or_regress_transition_messages():
+def test_advance_stage_does_not_repeat_or_regress_snapshots():
state = RouterState()
assert advance_stage(state, 4)
@@ -1179,10 +1147,7 @@ def test_interrupt_chunk_not_forwarded():
_assert_stage(_stage_update(frames), 2)
assert [frame["event_type"] for frame in frames] == [
"task.update",
- "chat.delta",
- "chat.delta",
"task.update",
- "chat.delta",
]
@@ -1852,19 +1817,14 @@ def test_brief_outline_uses_stage_two_and_outline_preview_contract():
"brief_source_tracer",
],
)
-def test_brief_research_nodes_advance_stage_three_in_foreground(agent):
+def test_brief_research_nodes_advance_stage_three_in_task_list(agent):
frames = route_chunk(
{"agent": agent, "event": "start", "content": "处理中"},
RouterState(current_stage=2),
)
_assert_stage(_stage_update(frames), 3)
- assert any(
- frame["event_type"] == "chat.delta"
- and frame["content"]
- == "[DeepResearch 阶段切换] 开始 Stage 3:并行调研与章节撰写\n"
- for frame in frames
- )
+ assert all(frame["event_type"] != "chat.delta" for frame in frames)
@pytest.mark.parametrize(
diff --git a/tests/unit_tests/agentserver/test_deepresearch_stream_tool.py b/tests/unit_tests/agentserver/test_deepresearch_stream_tool.py
index 12dc78808f..d92db6b125 100644
--- a/tests/unit_tests/agentserver/test_deepresearch_stream_tool.py
+++ b/tests/unit_tests/agentserver/test_deepresearch_stream_tool.py
@@ -2709,15 +2709,24 @@ async def test_outline_resume_starts_from_stage_three_without_replaying_prior_st
),
)
- transition_payloads = [
+ payloads = [
call.args[0]["payload"]
for call in push.send_push.await_args_list
- if call.args[0]["payload"].get("event_type") == "chat.delta"
- and "[DeepResearch 阶段切换]" in call.args[0]["payload"].get("content", "")
- ]
- assert [payload["task_id"] for payload in transition_payloads] == [
- "deepresearch_stage_3"
]
+ assert not any(
+ payload.get("event_type") == "chat.delta"
+ and "[DeepResearch 阶段" in payload.get("content", "")
+ for payload in payloads
+ )
+ assert any(
+ any(
+ task.get("task_id") == "deepresearch_stage_3"
+ and task.get("status") == "in_progress"
+ for task in payload.get("tasks", [])
+ )
+ for payload in payloads
+ if payload.get("event_type") == "task.update"
+ )
@pytest.mark.asyncio
@@ -2766,14 +2775,24 @@ async def test_user_feedback_resume_can_complete_final_report_without_section_re
)
)
assert outcome["status"] == "completed"
- transition_task_ids = [
- call.args[0]["payload"]["task_id"]
+ payloads = [
+ call.args[0]["payload"]
for call in push.send_push.await_args_list
- if call.args[0]["payload"].get("event_type") == "chat.delta"
- and "[DeepResearch 阶段切换]"
- in call.args[0]["payload"].get("content", "")
]
- assert transition_task_ids == ["deepresearch_stage_4"]
+ assert not any(
+ payload.get("event_type") == "chat.delta"
+ and "[DeepResearch 阶段" in payload.get("content", "")
+ for payload in payloads
+ )
+ assert any(
+ any(
+ task.get("task_id") == "deepresearch_stage_4"
+ and task.get("status") == "in_progress"
+ for task in payload.get("tasks", [])
+ )
+ for payload in payloads
+ if payload.get("event_type") == "task.update"
+ )
def test_artifact_bundle_ignores_blank_and_unapproved_companions():