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(swarm): skip stream cancel for team chat-send and emit processing_done for follow-up

min_gitcode25641c2e5ca2
diff --git a/jiuwenswarm/gateway/message_handler/message_handler.py b/jiuwenswarm/gateway/message_handler/message_handler.py
index a3c8b301e9..368b2b17e0 100644
--- a/jiuwenswarm/gateway/message_handler/message_handler.py
+++ b/jiuwenswarm/gateway/message_handler/message_handler.py
@@ -1,4 +1,4 @@
-# Copyright (c) Huawei Technologies Co., Ltd. 2025. All rights reserved.
+# Copyright (c) Huawei Technologies Co., Ltd. 2025-2026. All rights reserved.
"""MessageHandler - 消息处理抽象与双队列实现(入队经 AgentServerClient 发往 AgentServer)."""
@@ -268,6 +268,16 @@ class MessageHandler(ABC):
value = getattr(method, "value", method)
return value == "chat.send" or str(value) == "ReqMethod.CHAT_SEND"
+ @staticmethod
+ def _is_team_chat_send(msg: "Message") -> bool:
+ if not isinstance(msg.params, dict):
+ return False
+ return str(msg.params.get("mode") or "").strip().lower() == "team"
+
+ @classmethod
+ def _should_cancel_existing_stream_before_chat_send(cls, msg: "Message") -> bool:
+ return cls._is_chat_send_message(msg) and not cls._is_team_chat_send(msg)
+
def _get_channel_default_state(self, channel_id: str) -> ChannelControlState:
"""从 config.yaml 读取 Channel 的默认 session_id / mode."""
try:
@@ -2752,7 +2762,7 @@ class MessageHandler(ABC):
if env.is_stream:
# 取消同一 channel 上已有的流式任务,避免会话孤岛
# (例如 TUI 发送新消息时,旧 session 仍在后台空跑)
- if msg.req_method == ReqMethod.CHAT_SEND:
+ if self._should_cancel_existing_stream_before_chat_send(msg):
await self._cancel_stream_tasks_for_channel(msg)
# 流式处理:启动后台任务
# 通知前端新任务开始处理
diff --git a/jiuwenswarm/server/runtime/agent_adapter/team_helpers.py b/jiuwenswarm/server/runtime/agent_adapter/team_helpers.py
index 6c4d2c1bb9..0d9fbac00a 100644
--- a/jiuwenswarm/server/runtime/agent_adapter/team_helpers.py
+++ b/jiuwenswarm/server/runtime/agent_adapter/team_helpers.py
@@ -944,6 +944,7 @@ async def process_team_message_stream(
session_id,
rid,
)
+ yield _team_processing_done_chunk(rid, channel_id, session_id)
yield AgentResponseChunk(
request_id=rid,
channel_id=channel_id,
diff --git a/tests/unit_tests/agentserver/test_team_helpers.py b/tests/unit_tests/agentserver/test_team_helpers.py
index 3ae74a7661..a1476e6c3a 100644
--- a/tests/unit_tests/agentserver/test_team_helpers.py
+++ b/tests/unit_tests/agentserver/test_team_helpers.py
@@ -10,7 +10,6 @@ from types import SimpleNamespace
import pytest
from openjiuwen.agent_teams.schema.team import TeamRole
-from jiuwenswarm.agents.harness.team import TeamMonitorHandler
from jiuwenswarm.server.runtime.agent_adapter import evolution_helpers
from jiuwenswarm.server.runtime.agent_adapter import team_helpers
@@ -1033,6 +1032,59 @@ async def test_process_team_message_stream_handles_team_evolve_list(monkeypatch)
assert chunks[2].is_complete is True
+async def test_process_team_message_stream_emits_processing_done_for_followup(monkeypatch):
+ class _FakeManager:
+ interact_calls: list[tuple[str, str]] = []
+
+ @staticmethod
+ def has_stream_task(session_id: str) -> bool:
+ assert session_id == "sess-team-followup"
+ return True
+
+ @staticmethod
+ async def get_swarm_enriched_team_spec(**kwargs):
+ return SimpleNamespace(team_name="unit-team")
+
+ @classmethod
+ async def interact(cls, session_id: str, query: str):
+ cls.interact_calls.append((session_id, query))
+ return True, None
+
+ monkeypatch.setattr(team_helpers, "get_team_manager", lambda channel_id: _FakeManager())
+
+ request = SimpleNamespace(
+ session_id="sess-team-followup",
+ request_id="req-team-followup",
+ channel_id="web",
+ metadata=None,
+ params={"mode": "team"},
+ )
+ inputs = {"query": "$human-reporter claim task"}
+
+ chunks = []
+ async for chunk in team_helpers.process_team_message_stream(
+ request,
+ inputs,
+ object(),
+ ):
+ chunks.append(chunk)
+
+ assert _FakeManager.interact_calls == [
+ ("sess-team-followup", "$human-reporter claim task"),
+ ]
+ assert len(chunks) == 2
+ assert chunks[0].payload == {
+ "event_type": "chat.processing_status",
+ "session_id": "sess-team-followup",
+ "is_processing": False,
+ "is_complete": True,
+ }
+ assert chunks[0].is_complete is False
+ assert chunks[1].payload is None
+ assert chunks[1].is_complete is True
+
+
@pytest.mark.anyio
async def test_process_team_message_stream_emits_processing_done_for_evolve_approval(monkeypatch):
approval_event = SimpleNamespace(
diff --git a/tests/unit_tests/gateway/test_message_handler_stream_cancel.py b/tests/unit_tests/gateway/test_message_handler_stream_cancel.py
index 3770a37ee4..05808097d0 100644
--- a/tests/unit_tests/gateway/test_message_handler_stream_cancel.py
+++ b/tests/unit_tests/gateway/test_message_handler_stream_cancel.py
@@ -1,3 +1,5 @@
+# Copyright (c) Huawei Technologies Co., Ltd. 2026. All rights reserved.
+
"""Tests for gateway stream task cancellation before chat.send."""
from __future__ import annotations
@@ -48,13 +50,14 @@ def _chat_send_message(
*,
channel_id: str = "tui",
session_id: str = "sess_new",
+ mode: str = "agent.plan",
) -> Message:
return Message(
id="req-new",
type="req",
channel_id=channel_id,
session_id=session_id,
- params={"mode": "agent.plan", "query": "hello"},
+ params={"mode": mode, "query": "hello"},
timestamp=0.0,
ok=True,
req_method=ReqMethod.CHAT_SEND,
@@ -178,6 +181,20 @@ def test_is_single_user_channel_includes_cli_alias() -> None:
assert not _is_single_user_channel("web")
+def test_team_chat_send_keeps_existing_team_stream() -> None:
+ _should_cancel_existing_stream_before_chat_send = getattr(
+ MessageHandler,
+ "_should_cancel_existing_stream_before_chat_send",
+ )
+
+ assert not _should_cancel_existing_stream_before_chat_send(
+ _chat_send_message(channel_id="web", session_id="sess_team", mode="team"),
+ )
+ assert _should_cancel_existing_stream_before_chat_send(
+ _chat_send_message(channel_id="web", session_id="sess_agent", mode="agent.plan"),
+ )
+
+
# ── cancel_agent_sessions_on_disconnect ─────────────────────────
#
# Regression: when the user's WebSocket closes but `_session_to_client`
@@ -242,4 +259,4 @@ async def test_disconnect_backward_compatible_without_request_keys_kwarg() -> No
await handler.cancel_agent_sessions_on_disconnect([("tui", "sess_legacy")])
await asyncio.sleep(0)
- assert len(_FakeAgentClient.sent_requests) == 1
\ No newline at end of file
+ assert len(_FakeAgentClient.sent_requests) == 1