Skip to content

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

fix(agent): avoid duplicate external tool start event (#2167)

March-77698297b4c084
diff --git a/src/agentscope/agent/_agent.py b/src/agentscope/agent/_agent.py
index 69ad6d44d..295faf615 100644
--- a/src/agentscope/agent/_agent.py
+++ b/src/agentscope/agent/_agent.py
@@ -723,9 +723,12 @@ class Agent:
call_block = last_msg.content[index]
assert isinstance(call_block, ToolCallBlock)
- # An ALLOWED call was already running, so its START was already
- # emitted — skip it here (checked before flipping to FINISHED).
- if call_block.state != ToolCallState.ALLOWED:
+ # ALLOWED calls are running and SUBMITTED external calls are
+ # awaiting their result; both already emitted START.
+ if call_block.state not in (
+ ToolCallState.ALLOWED,
+ ToolCallState.SUBMITTED,
+ ):
yield ToolResultStartEvent(
reply_id=self.state.reply_id,
tool_call_id=last_msg.content[index].id,
diff --git a/tests/agent_interrupt_test.py b/tests/agent_interrupt_test.py
index 4eaac32b0..626842552 100644
--- a/tests/agent_interrupt_test.py
+++ b/tests/agent_interrupt_test.py
@@ -20,6 +20,7 @@ from utils import AnyString, MockModel
from agentscope.agent import Agent, InjectionConfig
from agentscope.event import (
ReplyEndEvent,
+ ToolResultStartEvent,
UserInterruptEvent,
)
from agentscope.message import (
@@ -768,7 +769,7 @@ class AgentInterruptEventTest(IsolatedAsyncioTestCase):
agent: Agent,
model: MockModel,
pending_tool_calls: list[ToolCallBlock],
- ) -> None:
+ ) -> list[Any]:
"""Drive one reply that yields the given tool calls so the reply
parks in either ASKING or SUBMITTED state."""
model.set_responses(
@@ -784,10 +785,12 @@ class AgentInterruptEventTest(IsolatedAsyncioTestCase):
],
],
)
- async for _ in agent.reply_stream(
+ events = []
+ async for event in agent.reply_stream(
UserMsg(name="user", content="Hi"),
):
- pass
+ events.append(event)
+ return events
async def test_interrupt_event_on_idle_agent_is_noop(self) -> None:
"""No parked HITL → ``UserInterruptEvent`` yields nothing and
@@ -867,7 +870,7 @@ class AgentInterruptEventTest(IsolatedAsyncioTestCase):
"""Parked in SUBMITTED: ``UserInterruptEvent`` patches the same
way (``str`` output)."""
agent, model = self._make_agent([_ExternalConcurrentTool()])
- await self._park_with(
+ parked_events = await self._park_with(
agent,
model,
[
@@ -887,6 +890,13 @@ class AgentInterruptEventTest(IsolatedAsyncioTestCase):
):
events.append(evt)
+ self.assertEqual(
+ sum(
+ isinstance(evt, ToolResultStartEvent)
+ for evt in [*parked_events, *events]
+ ),
+ 1,
+ )
_assert_interrupted_end(self, events, reply_id, session_id)
context_dicts = [