Build and run agents you can see, understand and trust.
Python agentchatbotlarge-language-modelsllmllm-agentmcpmulti-agentmulti-modalreact-agentrealtime-agentvoice-agentvoice-assistant
fix(agent): preserve cancellation before concurrent tool execution (#2768)
dafyy321-pixelcbb185048270
diff --git a/src/agentscope/agent/_agent.py b/src/agentscope/agent/_agent.pyindex 5e9ab43bc..59eade694 100644--- a/src/agentscope/agent/_agent.py+++ b/src/agentscope/agent/_agent.py@@ -2212,11 +2212,7 @@ class Agent:tasks are cancelled explicitly (to avoid orphan tasks), any eventsalready queued by the workers (including interruption chunks emittedby ``toolkit.call_tool`` when it catches ``CancelledError``) are- flushed to the caller, and the generator returns normally. The- caller is expected to detect the interruption via the flushed- ``ToolResultEndEvent(state=INTERRUPTED)`` events, mirroring the- event-based propagation used by- :meth:`_execute_sequential_tool_calls`.+ flushed to the caller before re-raising ``CancelledError``.Args:tool_calls (`list[ToolCallBlock]`):@@ -2232,6 +2228,9 @@ class Agent:The events generated during the execution of the tool calls.Raises:+ `asyncio.CancelledError`:+ Re-raised after cancelling workers and draining queued events+ when the caller task is cancelled.`ExceptionGroup`:Raised after all tool calls finish when one or more of themraised an exception. Each individual exception is included in@@ -2298,13 +2297,9 @@ class Agent:if event is sentinel:continueyield event- # Consume the cancel so this generator returns normally. The- # caller relies on the flushed ``ToolResultEndEvent(state=- # INTERRUPTED)`` events, not on the exception, to detect the- # interruption — mirroring the event-based propagation used by- # :meth:`_execute_sequential_tool_calls`.- asyncio.current_task().uncancel()- return+ # Calls cancelled before reaching the toolkit emit no interrupted+ # result, so propagate to let the reply loop close them+ raise# All tasks are done at this point; collect and re-raise exceptions.results = await gather_taskdiff --git a/tests/agent_interrupt_test.py b/tests/agent_interrupt_test.pyindex 8da9b3de2..5d9410b11 100644--- a/tests/agent_interrupt_test.py+++ b/tests/agent_interrupt_test.py@@ -12,7 +12,7 @@loop."""import asyncio-from typing import Any, AsyncGenerator+from typing import Any, AsyncGenerator, Callablefrom unittest.async_case import IsolatedAsyncioTestCasefrom utils import AnyString, MockModel@@ -29,12 +29,14 @@ from agentscope.message import (UserMsg,)from agentscope.model import ChatResponse+from agentscope.middleware import MiddlewareBasefrom agentscope.permission import (PermissionBehavior,PermissionContext,PermissionDecision,)from agentscope.tool import (+ FunctionTool,ToolBase,ToolChunk,Toolkit,@@ -317,6 +319,90 @@ class AgentInterruptCancelTest(IsolatedAsyncioTestCase):)return agent, model+ async def test_cancel_before_toolkit_entry(self) -> None:+ """A concurrent call cancelled in acting middleware is not re-run."""+ waiting = asyncio.Event()+ tool_entries: list[str] = []++ class WaitingMiddleware(MiddlewareBase):+ """Block the call before it reaches the toolkit."""++ async def on_acting(+ self,+ agent: Agent,+ input_kwargs: dict,+ next_handler: Callable[..., AsyncGenerator],+ ) -> AsyncGenerator:+ """Wait on the first entry only."""+ if not waiting.is_set():+ waiting.set()+ await asyncio.Event().wait()+ async for chunk in next_handler(**input_kwargs):+ yield chunk++ async def record() -> ToolChunk:+ """Record the invocation."""+ tool_entries.append("record")+ return ToolChunk(content=[TextBlock(text="done")])++ tool = FunctionTool(+ record,+ is_concurrency_safe=True,+ is_read_only=True,+ )+ model = MockModel(model="mock-model", stream=True)+ model.set_responses(+ [+ [+ ChatResponse(+ content=[+ ToolCallBlock(id="c1", name="record", input="{}"),+ ],+ is_last=True,+ ),+ ],+ ],+ )+ agent = Agent(+ name="Friday",+ system_prompt="You are a test agent.",+ model=model,+ toolkit=Toolkit(tools=[tool]),+ middlewares=[WaitingMiddleware()],+ injection_config=InjectionConfig(inject_runtime_state=False),+ )++ events: list[Any] = []++ async def drive() -> None:+ """Collect the reply events."""+ async for event in agent.reply_stream(+ UserMsg(name="user", content="Hi"),+ ):+ events.append(event)++ task = asyncio.create_task(drive())+ await asyncio.wait_for(waiting.wait(), timeout=5)+ task.cancel()+ await asyncio.wait_for(task, timeout=5)++ self.assertDictEqual(+ {+ "tool_entries": tool_entries,+ "model_calls": model.cnt,+ "finished_reason": [+ _.finished_reason+ for _ in events+ if isinstance(_, ReplyEndEvent)+ ],+ },+ {+ "tool_entries": [],+ "model_calls": 1,+ "finished_reason": ["interrupted"],+ },+ )+async def test_cancelled_error_propagates_after_tool_cleanup(self) -> None:"""With ``interruption_raise_cancelled_error`` the cancellationreaches the caller once the interrupted tool call is closed."""