Build and run agents you can see, understand and trust.
Python agentchatbotlarge-language-modelsllmllm-agentmcpmulti-agentmulti-modalreact-agentrealtime-agentvoice-agentvoice-assistant
fix(app): guard parked inbox continuations (#2849)
林桉2e706a6fa668
diff --git a/src/agentscope/app/_service/_chat.py b/src/agentscope/app/_service/_chat.pyindex c0b67860e..a66c2edc6 100644--- a/src/agentscope/app/_service/_chat.py+++ b/src/agentscope/app/_service/_chat.py@@ -81,7 +81,7 @@ from ...event import ()from ._errors import _classify_error, _classify_setup_errorfrom ..._utils._common import _generate_id-from ...message import AssistantMsg, HintBlock, Msg, ToolCallState, UserMsg+from ...message import AssistantMsg, HintBlock, Msg, UserMsgfrom ...permission import AdditionalWorkingDirectoryif TYPE_CHECKING:@@ -751,18 +751,10 @@ class ChatService:`bool`:``True`` when the run should be skipped."""- if input_msg is not None or not agent.state.context:+ if input_msg is not None:return False- last_msg = agent.state.context[-1]- if last_msg.role != "assistant" or last_msg.name != agent.name:- return False-- awaiting = [- tc- for tc in last_msg.get_content_blocks("tool_call")- if tc.state in (ToolCallState.ASKING, ToolCallState.SUBMITTED)- ]+ awaiting = agent.state.get_awaiting_tool_calls(agent.name)if not awaiting:return False@@ -1422,6 +1414,11 @@ class ChatService:):released = Truebreak++ # A parked agent can't take a ``None`` turn; the queued+ # payloads are drained once it resumes.+ if agent.state.has_awaiting_tool_calls(agent.name):+ breakinput_msg = Nonefinally:diff --git a/tests/service_team_failure_report_test.py b/tests/service_team_failure_report_test.pyindex d12cff67d..7c63d5446 100644--- a/tests/service_team_failure_report_test.py+++ b/tests/service_team_failure_report_test.py@@ -16,6 +16,7 @@ from unittest.mock import patchfrom utils import AnyStringfrom agentscope.agent import ContextConfig, ReActConfig+from agentscope.app._bus_ops import deliver_to_inboxfrom agentscope.app._service import ChatServicefrom agentscope.app.message_bus import InMemoryMessageBus, MessageBusKeysfrom agentscope.app.storage import (@@ -28,6 +29,7 @@ from agentscope.app.storage import (TeamRecord,)from agentscope.event import ReplyEndEvent, ReplyStartEvent+from agentscope.message import AssistantMsg, ToolCallBlock, ToolCallStatefrom agentscope.types import ErrorInfo, ErrorType, ReplyFinishedReason_USER = "user-1"@@ -139,8 +141,14 @@ def _agent_cls(events: list) -> type:class _Agent:"""Replay ``events``; the state is only read back for persistence."""- def __init__(self, *, state: object = None, **_: object) -> None:- self.state = state or SimpleNamespace()+ def __init__(+ self,+ *,+ name: str,+ state: object = None,+ **_: object,+ ) -> None:+ self.name, self.state = name, state or SimpleNamespace()async def reply_stream(self,@@ -186,6 +194,7 @@ class TeamFailureReportTest(IsolatedAsyncioTestCase):agent_id: str,events: list,*,+ agent_cls: type | None = None,model_fails: bool = False,workspace_fails: bool = False,persist_fails: bool = False,@@ -226,7 +235,7 @@ class TeamFailureReportTest(IsolatedAsyncioTestCase):background_task_manager=object(),message_bus=self.bus,resource_access_service=_Access(self.storage),- custom_agent_cls=_agent_cls(events),+ custom_agent_cls=agent_cls or _agent_cls(events),)with (patch(@@ -424,3 +433,60 @@ class TeamFailureReportTest(IsolatedAsyncioTestCase):"Error: boom.",entries[0][1]["hint"],)++ async def test_parked_worker_keeps_inbox_for_resume(self) -> None:+ """A worker parked on a confirmation must not take a ``None`` turn+ for a payload that arrived mid-turn, nor report a failure."""+ bus, worker_session = self.bus, self.worker_session++ class _ParkingAgent:+ """Park on a confirmation while a payload lands in the inbox."""++ def __init__(self, *, name: str, state: object, **_: object):+ self.name, self.state = name, state++ async def reply_stream(+ self,+ inputs: object,+ ) -> AsyncGenerator[object, None]:+ """Park on the first turn, fail like ``Agent`` on a rerun."""+ del inputs+ if self.state.has_awaiting_tool_calls(self.name):+ raise ValueError("awaiting confirmation, got no event")+ self.state.context.append(+ AssistantMsg(+ name=self.name,+ content=[+ ToolCallBlock(+ id="call-1",+ name="Bash",+ input="{}",+ state=ToolCallState.ASKING,+ ),+ ],+ ),+ )+ await deliver_to_inbox(+ bus,+ user_id=_USER,+ session_id=worker_session.id,+ agent_id="agent-l",+ payload={"type": "hint", "hint": "from leader"},+ )+ if False:+ yield++ delivered = await self._run(+ worker_session,+ self.worker_agent.id,+ [],+ agent_cls=_ParkingAgent,+ )+ self.assertListEqual(delivered, [])+ entries = await self.bus.queue_drain(+ MessageBusKeys.inbox(worker_session.id),+ )+ self.assertListEqual(+ [payload for _entry_id, payload in entries],+ [{"type": "hint", "hint": "from leader"}],+ )