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): 专家团跟进消息避开 reset 未完成时的空流

yuzengjie01e2be9e78fc2a
diff --git a/jiuwenswarm/server/runtime/agent_adapter/team_helpers.py b/jiuwenswarm/server/runtime/agent_adapter/team_helpers.py
index 69799ab715..c58bdf7769 100644
--- a/jiuwenswarm/server/runtime/agent_adapter/team_helpers.py
+++ b/jiuwenswarm/server/runtime/agent_adapter/team_helpers.py
@@ -1262,6 +1262,60 @@ _TEAM_TASK_TERMINAL_STATUSES = frozenset({"completed", "cancelled"})
_TEAM_MEMBER_UNSTARTED_STATUS = "unstarted"
+_POOL_RELEASE_TIMEOUT_S = 90.0
+_POOL_RELEASE_POLL_S = 0.2
+
+
+def _pool_entry_running(info: Any, team_name: str, session_id: str) -> bool:
+ """True when this session's team is still RUNNING in the runner pool."""
+ if getattr(info, "team_name", None) != team_name:
+ return False
+ if getattr(info, "current_session_id", None) != session_id:
+ return False
+ state = getattr(info, "state", None)
+ return getattr(state, "value", state) == "running"
+
+
+async def _await_runner_pool_release(team_name: str, session_id: str) -> None:
+ """Wait out an in-flight team.session.reset before opening a new stream.
+
+ Reset clears the local stream marker immediately, then spends a long time
+ in stop_coordination. A chat.send in that window used to activate against
+ the still-pooled team, get reject_running, and end with no output.
+ A paused entry is left alone so the next stream can resume it.
+ """
+ deadline = time.monotonic() + _POOL_RELEASE_TIMEOUT_S
+ while True:
+ try:
+ infos = await Runner.list_active_teams()
+ except Exception as exc:
+ logger.warning(
+ "[TeamHelpers] list active teams failed before stream: session_id=%s error=%s",
+ session_id,
+ exc,
+ )
+ return
+ if not any(_pool_entry_running(info, team_name, session_id) for info in infos):
+ return
+ if time.monotonic() >= deadline:
+ logger.warning(
+ "[TeamHelpers] runner pool still running before first stream; stopping: "
+ "session_id=%s team_name=%s",
+ session_id,
+ team_name,
+ )
+ try:
+ await Runner.stop_agent_team(team_name=team_name, session_id=session_id)
+ except Exception as exc:
+ logger.warning(
+ "[TeamHelpers] stop stuck runner pool failed: session_id=%s error=%s",
+ session_id,
+ exc,
+ )
+ return
+ await asyncio.sleep(_POOL_RELEASE_POLL_S)
+
+
def _run_agent_team_streaming(**kwargs: Any) -> AsyncIterator[Any]:
# The public Runner facade re-yields the core generator and does not
# propagate aclose(). Keep the core handle so early round termination runs
@@ -2582,6 +2636,7 @@ async def _consume_stream_with_query(
_safe_query_preview(initial_query),
)
runner_entered_at = time.monotonic()
+ await _await_runner_pool_release(str(getattr(team_spec, "team_name", "") or ""), session_id)
team_stream = _run_agent_team_streaming(
agent_team=team_spec,
inputs={"query": initial_query},
diff --git a/tests/unit_tests/agentserver/test_team_helpers.py b/tests/unit_tests/agentserver/test_team_helpers.py
index e1eaa611cc..972785c3d9 100644
--- a/tests/unit_tests/agentserver/test_team_helpers.py
+++ b/tests/unit_tests/agentserver/test_team_helpers.py
@@ -5389,3 +5389,72 @@ def test_workflow_runs_persist_and_restore_honor_sessions_root(
)
assert restored is not None
assert restored["wf-1"].id == "wf-1"
+
+
+def _pool_info(state: str) -> SimpleNamespace:
+ return SimpleNamespace(
+ team_name="oc_team",
+ current_session_id="sess",
+ state=SimpleNamespace(value=state),
+ )
+
+
+async def test_await_runner_pool_release_waits_until_running_entry_leaves(monkeypatch):
+ snapshots = [[_pool_info("running")], []]
+ stopped: list[dict[str, str]] = []
+
+ async def _list_active_teams():
+ return snapshots.pop(0)
+
+ async def _stop_agent_team(**kwargs):
+ stopped.append(kwargs)
+ return True
+
+ monkeypatch.setattr(team_helpers.Runner, "list_active_teams", _list_active_teams)
+ monkeypatch.setattr(team_helpers.Runner, "stop_agent_team", _stop_agent_team)
+ monkeypatch.setattr(team_helpers, "_POOL_RELEASE_POLL_S", 0.01)
+
+ await team_helpers._await_runner_pool_release("oc_team", "sess")
+
+ assert stopped == []
+ assert snapshots == []
+
+
+async def test_await_runner_pool_release_leaves_paused_entry(monkeypatch):
+ stopped: list[dict[str, str]] = []
+
+ async def _list_active_teams():
+ return [_pool_info("paused")]
+
+ async def _stop_agent_team(**kwargs):
+ stopped.append(kwargs)
+ return True
+
+ monkeypatch.setattr(team_helpers.Runner, "list_active_teams", _list_active_teams)
+ monkeypatch.setattr(team_helpers.Runner, "stop_agent_team", _stop_agent_team)
+
+ await team_helpers._await_runner_pool_release("oc_team", "sess")
+
+ assert stopped == []
+
+
+async def test_await_runner_pool_release_stops_entry_that_stays_running(monkeypatch):
+ stopped: list[dict[str, str]] = []
+
+ async def _list_active_teams():
+ return [_pool_info("running")]
+
+ async def _stop_agent_team(**kwargs):
+ stopped.append(kwargs)
+ return True
+
+ monkeypatch.setattr(team_helpers.Runner, "list_active_teams", _list_active_teams)
+ monkeypatch.setattr(team_helpers.Runner, "stop_agent_team", _stop_agent_team)
+ monkeypatch.setattr(team_helpers, "_POOL_RELEASE_TIMEOUT_S", 0)
+
+ await team_helpers._await_runner_pool_release("oc_team", "sess")
+
+ assert stopped == [{"team_name": "oc_team", "session_id": "sess"}]