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(team): restore post-round request routing

openJiuwen-bot6f7c855bc1fb
diff --git a/jiuwenswarm/server/runtime/agent_adapter/team_helpers.py b/jiuwenswarm/server/runtime/agent_adapter/team_helpers.py
index cffebb7b3f..f5774a379b 100644
--- a/jiuwenswarm/server/runtime/agent_adapter/team_helpers.py
+++ b/jiuwenswarm/server/runtime/agent_adapter/team_helpers.py
@@ -208,9 +208,10 @@ def _normalize_team_query(query: Any, *, channel_id: str | None, language: str)
async def _team_session_has_runtime(team_manager: Any, session_id: str) -> bool:
- session_has_runtime = getattr(team_manager, "session_has_runtime", None)
- if callable(session_has_runtime):
- return bool(await session_has_runtime(session_id))
+ # Keep ordinary team first-request detection scoped to claw-local
+ # live markers only. Resumable Runner-pool entries are reserved for
+ # InteractiveInput recovery and must not make a fresh text request
+ # look like a follow-up after the previous round has ended.
return (
getattr(team_manager, "active_session_id", None) == session_id
or getattr(team_manager, "pending_session_id", None) == session_id
@@ -1132,6 +1133,9 @@ async def _consume_stream_with_query(
finally:
team_manager = get_team_manager(channel_id)
team_manager.clear_pending_runtime(session_id)
+ clear_active_runtime = getattr(team_manager, "clear_active_runtime", None)
+ if callable(clear_active_runtime):
+ clear_active_runtime(session_id)
team_manager.pop_stream_task(session_id)
diff --git a/tests/unit_tests/agentserver/test_agentserver_modes.py b/tests/unit_tests/agentserver/test_agentserver_modes.py
index b9e8043e9e..f26b75ba18 100644
--- a/tests/unit_tests/agentserver/test_agentserver_modes.py
+++ b/tests/unit_tests/agentserver/test_agentserver_modes.py
@@ -737,6 +737,86 @@ def test_process_message_stream_treats_team_plan_confirm_resume_as_team_follow_u
assert chunks[-1].is_complete is True
+def test_process_message_stream_treats_plain_team_query_as_first_request_after_round_end(monkeypatch):
+ from jiuwenswarm.server.runtime.agent_adapter import interface as interface_module
+
+ class FakeSessionManager:
+ submit_task_calls = []
+
+ @staticmethod
+ def get_session_id(session_id=None):
+ return session_id or "default"
+
+ @classmethod
+ async def submit_task(cls, session_id, task_factory):
+ cls.submit_task_calls.append(session_id)
+ await task_factory()
+
+ class FakeAdapter:
+ seen_inputs = None
+
+ @staticmethod
+ async def process_message_stream_impl(request, inputs):
+ _ = request
+ FakeAdapter.seen_inputs = inputs
+ yield AgentResponseChunk(
+ request_id="req-team-fresh-round",
+ channel_id="web",
+ payload={"event_type": "chat.done"},
+ is_complete=True,
+ )
+
+ class FakeTeamManager:
+ active_session_id = None
+ pending_session_id = None
+
+ @staticmethod
+ def has_stream_task(session_id: str) -> bool:
+ assert session_id == "team-session"
+ return False
+
+ @staticmethod
+ async def session_has_runtime(session_id: str) -> bool:
+ assert session_id == "team-session"
+ return True
+
+ fake_adapter = FakeAdapter()
+
+ monkeypatch.setattr(interface_module, "SessionManager", FakeSessionManager)
+ monkeypatch.setattr(interface_module, "get_config", lambda: {"preferred_language": "zh"})
+ monkeypatch.setattr(interface_module, "get_memory_mode", lambda _config: "disabled")
+ monkeypatch.setattr(
+ interface_module.JiuWenSwarm,
+ "_ensure_adapter",
+ lambda self, mode="agent": fake_adapter,
+ )
+ monkeypatch.setattr(
+ "jiuwenswarm.agents.harness.team.get_team_manager",
+ lambda _channel_id: FakeTeamManager(),
+ )
+
+ request = AgentRequest(
+ request_id="req-team-fresh-round",
+ channel_id="web",
+ session_id="team-session",
+ params={
+ "query": "你好",
+ "mode": "team",
+ },
+ is_stream=True,
+ )
+
+ async def collect_chunks():
+ return [chunk async for chunk in interface_module.JiuWenSwarm().process_message_stream(request)]
+
+ chunks = asyncio.run(collect_chunks())
+
+ assert FakeSessionManager.submit_task_calls == ["team-session"]
+ assert fake_adapter.seen_inputs["query"] == "你好"
+ assert chunks[0].payload == {"event_type": "chat.done"}
+ assert chunks[-1].is_complete is True
+
+
@pytest.mark.parametrize(
"params",
[
diff --git a/tests/unit_tests/agentserver/test_team_helpers.py b/tests/unit_tests/agentserver/test_team_helpers.py
index 74f75123ff..60f7b6ccd2 100644
--- a/tests/unit_tests/agentserver/test_team_helpers.py
+++ b/tests/unit_tests/agentserver/test_team_helpers.py
@@ -888,6 +888,10 @@ async def test_consume_stream_with_query_launches_watcher_after_runtime_ready(mo
def clear_pending_runtime(session_id: str) -> None:
calls.append(f"clear:{session_id}")
+ @staticmethod
+ def clear_active_runtime(session_id: str) -> None:
+ calls.append(f"clear_active:{session_id}")
+
@staticmethod
def pop_stream_task(session_id: str):
calls.append(f"pop:{session_id}")
@@ -961,6 +965,11 @@ async def test_consume_stream_with_query_launches_watcher_after_runtime_ready(mo
"monitor:sess-runtime:ready-team",
"watcher:sess-runtime:runtime_ready",
]
+ assert calls[-3:] == [
+ "clear:sess-runtime",
+ "clear_active:sess-runtime",
+ "pop:sess-runtime",
+ ]
def test_sync_team_identity_metadata_persists_ready_team_for_any_activation(monkeypatch):
@@ -1502,6 +1511,84 @@ async def test_process_team_message_stream_recovers_paused_runtime_for_interacti
assert chunks[-1].is_complete is True
+async def test_process_team_message_stream_treats_plain_query_as_first_request_after_round_end(monkeypatch):
+ captured: dict[str, Any] = {}
+
+ class _FakeManager:
+ active_session_id = None
+ pending_session_id = None
+
+ @staticmethod
+ async def session_has_runtime(session_id: str) -> bool:
+ assert session_id == "sess-team-new-round"
+ return True
+
+ @staticmethod
+ def has_stream_task(session_id: str) -> bool:
+ assert session_id == "sess-team-new-round"
+ return False
+
+ @staticmethod
+ async def get_swarm_enriched_team_spec(**_kwargs):
+ return SimpleNamespace(team_name="unit-team", enable_swarmflow=False)
+
+ @staticmethod
+ def ensure_team_shared_skills_ready_for_session(session_id: str, spec: Any):
+ captured["skills_ready"] = (session_id, spec.team_name)
+
+ @staticmethod
+ async def prepare_runtime_activation(session_id: str, team_name: str):
+ captured["prepared"] = (session_id, team_name)
+
+ @staticmethod
+ def register_stream_task(session_id: str, task: object) -> None:
+ captured["registered"] = session_id
+
+ @staticmethod
+ async def interact(*_args, **_kwargs):
+ raise AssertionError("plain text query after round end should start a new team round")
+
+ async def _fake_consume_stream_with_query(
+ channel_id: str | None,
+ session_id: str,
+ spec: object,
+ query: str,
+ *,
+ round_id: int,
+ envs: dict | None = None,
+ ) -> None:
+ _ = channel_id, spec, envs
+ captured["consumed"] = (session_id, query, round_id)
+
+ monkeypatch.setattr(team_helpers, "get_team_manager", lambda channel_id: _FakeManager())
+ monkeypatch.setattr(team_helpers, "increment_session_round_count", lambda session_id: 1)
+ monkeypatch.setattr(team_helpers, "_consume_stream_with_query", _fake_consume_stream_with_query)
+
+ request = SimpleNamespace(
+ session_id="sess-team-new-round",
+ request_id="req-team-new-round",
+ channel_id="web",
+ metadata=None,
+ params={"mode": "team"},
+ )
+
+ chunks = []
+ async for chunk in team_helpers.process_team_message_stream(
+ request,
+ {"query": "你好"},
+ object(),
+ ):
+ chunks.append(chunk)
+ await asyncio.sleep(0)
+
+ assert captured["prepared"] == ("sess-team-new-round", "unit-team")
+ assert captured["registered"] == "sess-team-new-round"
+ assert captured["consumed"] == ("sess-team-new-round", "你好", 1)
+ assert captured["skills_ready"] == ("sess-team-new-round", "unit-team")
+ assert chunks[-1].is_complete is True
+
+
@pytest.mark.anyio
async def test_process_team_message_stream_converts_a2ui_followup_event(monkeypatch):
monkeypatch.setenv("JIUWENSWARM_A2UI_ENABLED", "true")