Build and run agents you can see, understand and trust.
Python agentchatbotlarge-language-modelsllmllm-agentmcpmulti-agentmulti-modalreact-agentrealtime-agentvoice-agentvoice-assistant
fix(a2a): keep observed messages when the reply is rejected before sending (#2875)
ydflow9425fe25ab3f
diff --git a/src/agentscope/agent/_a2a_agent.py b/src/agentscope/agent/_a2a_agent.pyindex 487124571..1de3b4ba1 100644--- a/src/agentscope/agent/_a2a_agent.py+++ b/src/agentscope/agent/_a2a_agent.py@@ -362,8 +362,9 @@ class A2AAgent:input_msgs = []# Observed messages are earlier context, so they lead this input.+ # They stay cached until the reply is committed to sending below,+ # so a rejected pre-flight keeps them for the retry.input_msgs = [*self.state.observed_context, *input_msgs]- self.state.observed_context.clear()if not input_msgs:raise ValueError(@@ -412,6 +413,9 @@ class A2AAgent:finished_reason = ReplyFinishedReason.COMPLETEDrun = _TextRun()+ # The message is going out now: consume the observed messages.+ self.state.observed_context.clear()+async for response in self._client.send_message(self._build_request(input_msgs),):diff --git a/tests/a2a_agent_test.py b/tests/a2a_agent_test.pyindex 2198e6c41..d2524c880 100644--- a/tests/a2a_agent_test.py+++ b/tests/a2a_agent_test.py@@ -735,6 +735,83 @@ class A2AAgentTaskContinuationTest(IsolatedAsyncioTestCase):self.assertListEqual(client.requests, [])await agent.aclose()+ async def test_rejected_reply_keeps_observed_messages(self) -> None:+ """A reply rejected by the running-Task check must not consume+ the observed messages — the retry still sends them."""+ client = _FakeClient(+ [+ [+ types.StreamResponse(+ message=types.Message(+ message_id="msg-1",+ context_id="context-1",+ role=types.Role.ROLE_AGENT,+ parts=[types.Part(text="ok")],+ ),+ ),+ ],+ ],+ get_tasks=[+ types.Task(+ id="task-1",+ context_id="context-1",+ status=types.TaskStatus(+ state=types.TaskState.TASK_STATE_WORKING,+ ),+ ),+ # The retry finds the Task forgotten and starts a new one.+ TaskNotFoundError(),+ ],+ )+ agent = A2AAgent(+ self.card,+ client=client,+ state=A2AAgentState(context_id="context-1", task_id="task-1"),+ )++ await agent.observe(UserMsg(name="user", content="earlier"))++ with self.assertRaises(RuntimeError):+ await agent.reply(UserMsg(name="user", content="hi"))++ # Nothing was sent and the observation survived the rejection.+ self.assertListEqual(client.requests, [])+ self.assertEqual(+ [_.get_text_content() for _ in agent.state.observed_context],+ ["earlier"],+ )++ await agent.reply(UserMsg(name="user", content="later"))++ self.assertEqual(+ [_.text for _ in client.requests[0].message.parts],+ ["earlier", "later"],+ )+ self.assertListEqual(agent.state.observed_context, [])+ await agent.aclose()++ async def test_pre_flight_failure_keeps_observed_messages(self) -> None:+ """A client error before the send (the task lookup here) must+ not consume the observed messages."""+ client = _FakeClient(get_tasks=[RuntimeError("server down")])+ agent = A2AAgent(+ self.card,+ client=client,+ state=A2AAgentState(context_id="context-1", task_id="task-1"),+ )++ await agent.observe(UserMsg(name="user", content="earlier"))++ with self.assertRaises(RuntimeError):+ await agent.reply(UserMsg(name="user", content="hi"))++ self.assertListEqual(client.requests, [])+ self.assertEqual(+ [_.get_text_content() for _ in agent.state.observed_context],+ ["earlier"],+ )+ await agent.aclose()+async def test_forgotten_task_starts_a_new_one(self) -> None:"""A Task the server dropped degrades into a new one."""client = _FakeClient(