Skip to content

Build and run agents you can see, understand and trust.

fix(pipeline): join sibling members after a failed round (#2982)

Sicheng Han8bd293b3bf1d
diff --git a/src/agentscope/pipeline/_team_pipeline.py b/src/agentscope/pipeline/_team_pipeline.py
index 3f293a8f8..c4c6bada1 100644
--- a/src/agentscope/pipeline/_team_pipeline.py
+++ b/src/agentscope/pipeline/_team_pipeline.py
@@ -338,9 +338,19 @@ class TeamPipeline:
async def run_all() -> None:
"""Run every group and mark the end of the stream."""
+ tasks = [
+ asyncio.create_task(run_group(*group))
+ for group in groups.items()
+ ]
try:
- await asyncio.gather(*[run_group(*_) for _ in groups.items()])
+ await asyncio.gather(*tasks)
finally:
+ # gather propagates a member failure without stopping siblings.
+ # Join them before ending the round, preserving the exception.
+ for task in tasks:
+ if not task.done():
+ task.cancel()
+ await asyncio.gather(*tasks, return_exceptions=True)
await queue.put(sentinel)
gather_task = asyncio.create_task(run_all())
diff --git a/tests/pipeline_team_cleanup_test.py b/tests/pipeline_team_cleanup_test.py
new file mode 100644
index 000000000..ce5bd81c4
--- /dev/null
+++ b/tests/pipeline_team_cleanup_test.py
@@ -0,0 +1,123 @@
+# -*- coding: utf-8 -*-
+"""Regression coverage for failed concurrent team assignment rounds."""
+import asyncio
+from typing import Any
+from unittest.async_case import IsolatedAsyncioTestCase
+
+from pipeline_team_test import SlowTool, _agent, _text, _tool_call
+from utils import MockModel
+
+from agentscope.message import TextBlock, ToolCallBlock, UserMsg
+from agentscope.model import ChatResponse
+from agentscope.pipeline import TeamMember, TeamPipeline
+from agentscope.tool import ToolChunk
+
+
+class CleanupTool(SlowTool):
+ """Track cancellation and asynchronous cleanup without external effects."""
+
+ def __init__(self) -> None:
+ super().__init__()
+ self.release = asyncio.Event()
+ self.stopped = asyncio.Event()
+
+ async def call(self) -> ToolChunk:
+ """Block until released, and record when cleanup has completed."""
+ try:
+ self.started.set()
+ await self.release.wait()
+ return ToolChunk(content=[TextBlock(text="finished")])
+ finally:
+ await asyncio.sleep(0)
+ self.stopped.set()
+
+
+class FailingMemberModel(MockModel):
+ """Fail only after all sibling tools have started."""
+
+ def __init__(self, tools: list[CleanupTool], error: Exception) -> None:
+ super().__init__()
+ self.tools = tools
+ self.error = error
+
+ async def _call_api(self, *args: Any, **kwargs: Any) -> ChatResponse:
+ await asyncio.gather(*(tool.started.wait() for tool in self.tools))
+ raise self.error
+
+
+class TeamCleanupTest(IsolatedAsyncioTestCase):
+ """A failing member must not leave sibling tool executions alive."""
+
+ async def test_model_failure_joins_all_siblings(self) -> None:
+ """Preserve the original failure after every sibling has cleaned up.
+
+ Based on the deterministic reproduction in issue #2972.
+ """
+ for failing_first in (True, False):
+ with self.subTest(failing_first=failing_first):
+ tools = [CleanupTool(), CleanupTool()]
+ workers = [
+ _agent(f"worker{i}", [t]) for i, t in enumerate(tools)
+ ]
+ for worker in workers:
+ worker.model.set_responses(
+ [_tool_call("slow", "{}", "slow_tool"), _text("done")],
+ )
+ failure = RuntimeError("member model failed")
+ failing = _agent("failing")
+ failing.model = FailingMemberModel(tools, failure)
+ members = (
+ [failing, *workers]
+ if failing_first
+ else [*workers, failing]
+ )
+ leader = _agent("leader")
+ leader.model.set_responses(
+ [
+ [
+ ChatResponse(
+ content=[
+ ToolCallBlock(
+ id=member.name,
+ name="TeamAssign",
+ input='{"member":"'
+ + member.name
+ + '","prompt":"work"}',
+ )
+ for member in members
+ ],
+ is_last=True,
+ usage=None,
+ ),
+ ],
+ ],
+ )
+ pipeline = TeamPipeline(
+ leader,
+ [TeamMember(agent=m, description=m.name) for m in members],
+ )
+
+ async def consume(team: TeamPipeline) -> None:
+ """Consume the full reply so member failures propagate."""
+ async for _ in team.reply_stream(
+ UserMsg(name="user", content="go"),
+ ):
+ pass
+
+ try:
+ with self.assertRaises(RuntimeError) as caught:
+ await asyncio.wait_for(consume(pipeline), timeout=5)
+ self.assertIs(caught.exception, failure)
+ self.assertTrue(
+ all(tool.stopped.is_set() for tool in tools),
+ )
+ finally:
+ # Also let the unfixed baseline exit without orphan tasks.
+ for tool in tools:
+ tool.release.set()
+ await asyncio.wait_for(
+ asyncio.gather(
+ *(tool.stopped.wait() for tool in tools),
+ ),
+ timeout=5,
+ )