Build and run agents you can see, understand and trust.
Python agentchatbotlarge-language-modelsllmllm-agentmcpmulti-agentmulti-modalreact-agentrealtime-agentvoice-agentvoice-assistant
fix(agent): count reasoning-acting rounds once (#2217)
RICARDO51167662ebaab3d9a
diff --git a/src/agentscope/agent/_agent.py b/src/agentscope/agent/_agent.pyindex 75b172bdf..dd8331b88 100644--- a/src/agentscope/agent/_agent.py+++ b/src/agentscope/agent/_agent.py@@ -970,15 +970,12 @@ class Agent:if break_execution_for_hitl:break- if break_execution_for_hitl:- # Stop executing the next batches, and go back to- # ``_next_action``, which parks the reply on the- # awaiting tool calls. The unfinished round isn't- # counted into ``cur_iter``- continue-- # Update iteration count after each round of reasoning-acting- self.state.cur_iter += 1+ # One reasoning-acting round is over once every tool call it+ # produced has a result. Reasoning that generated tool calls,+ # or Acting that parked some of them on a user confirmation+ # or an external execution, leaves the round unfinished+ if not self.state.get_unfinished_tool_calls(self.name):+ self.state.cur_iter += 1except asyncio.CancelledError:# Handle the CancelledError within the _reply_impl for thediff --git a/src/agentscope/state/_state.py b/src/agentscope/state/_state.pyindex 667afd7d6..009666c19 100644--- a/src/agentscope/state/_state.py+++ b/src/agentscope/state/_state.py@@ -337,3 +337,34 @@ class AgentState(BaseModel):tc.state == ToolCallState.SUBMITTED and tc.id not in result_ids)]++ def get_unfinished_tool_calls(self, name: str) -> list[ToolCallBlock]:+ """Get the current reply's tool calls with no matching tool result+ yet, whatever their state. A reply accumulates into a single message,+ so the calls of the finished reasoning-acting rounds are excluded+ while the ones of the ongoing round are returned.++ Args:+ name (`str`):+ Only messages authored by this agent name are inspected;+ observed messages from other agents are ignored.++ Returns:+ `list[ToolCallBlock]`:+ The unfinished tool call blocks, empty if none.+ """+ if not self.context:+ return []+ last_msg = self.context[-1]+ if (+ last_msg.role != "assistant"+ or last_msg.name != name+ or last_msg.id != self.reply_id+ ):+ return []+ result_ids = {b.id for b in last_msg.get_content_blocks("tool_result")}+ return [+ tc+ for tc in last_msg.get_content_blocks("tool_call")+ if tc.id not in result_ids+ ]diff --git a/tests/agent_basic_test.py b/tests/agent_basic_test.pyindex 94ee77393..b667eec9d 100644--- a/tests/agent_basic_test.py+++ b/tests/agent_basic_test.py@@ -5,7 +5,7 @@ from unittest.async_case import IsolatedAsyncioTestCasefrom utils import AnyString, MockModel-from agentscope.agent import Agent, ContextConfig, InjectionConfig+from agentscope.agent import Agent, ContextConfig, InjectionConfig, ReActConfigfrom agentscope.model import ChatResponsefrom agentscope.tool import (ToolBase,@@ -333,6 +333,7 @@ class AgentBasicTest(IsolatedAsyncioTestCase):]context_dicts = [msg.model_dump() for msg in self.agent.state.context]self.assertListEqual(context_dicts, expected_context)+ self.assertEqual(self.agent.state.cur_iter, 1)# Test reply interfaceself.model.cnt = 0 # Reset mock model response index@@ -658,6 +659,48 @@ class AgentBasicTest(IsolatedAsyncioTestCase):context_dicts = [msg.model_dump() for msg in self.agent.state.context]self.assertListEqual(context_dicts, expected_context_after_reply)+ async def test_max_iters_counts_reasoning_acting_round_once(self) -> None:+ """A tool round consumes one iteration before final reasoning."""+ self.agent.toolkit = Toolkit(tools=[MockSequentialTool()])+ self.agent.react_config = ReActConfig(max_iters=2)+ self.model.set_responses(+ [+ ChatResponse(+ content=[+ ToolCallBlock(+ id="tool_call_1",+ name="mock_sequential_tool",+ input='{"input": "x"}',+ ),+ ],+ is_last=True,+ ),+ ChatResponse(+ content=[TextBlock(text="done")],+ is_last=True,+ ),+ ],+ )++ msg = await self.agent.reply(UserMsg(name="user", content="Go"))++ self.assertEqual(msg.finished_reason, ReplyFinishedReason.COMPLETED)+ self.assertEqual(+ [block.text for block in msg.get_content_blocks("text")],+ ["done"],+ )+ self.assertEqual(self.model.cnt, 2)+ self.assertEqual(self.agent.state.cur_iter, 2)++ tool_results = self.agent.state.context[-1].get_content_blocks(+ "tool_result",+ )+ self.assertEqual(len(tool_results), 1)+ self.assertEqual(+ tool_results[0].output[0].text,+ "Sequential result: x",+ )+async def test_thinking_only_response_continues_reasoning(self) -> None:"""A thinking-only response should not end with an empty reply."""self.model.set_responses(@@ -677,6 +720,7 @@ class AgentBasicTest(IsolatedAsyncioTestCase):# The thinking-only turn must trigger a second reasoning roundself.assertEqual(self.model.cnt, 2)+ self.assertEqual(self.agent.state.cur_iter, 2)# The final reply message only carries the visible text answerself.assertDictEqual(@@ -1038,6 +1082,7 @@ class AgentBasicTest(IsolatedAsyncioTestCase):context_dicts = [msg.model_dump() for msg in self.agent.state.context]expected_context = [{**msg_base, **_} for _ in expected_context]self.assertListEqual(context_dicts, expected_context)+ self.assertEqual(self.agent.state.cur_iter, 2)async def test_streaming_concurrent_tool_calls(self) -> None:"""Test the streaming model inference with tool calls generated.@@ -1637,6 +1682,7 @@ class AgentBasicTest(IsolatedAsyncioTestCase):context_dicts = [msg.model_dump() for msg in self.agent.state.context]expected_context = [{**msg_base, **_} for _ in expected_context]self.assertListEqual(context_dicts, expected_context)+ self.assertEqual(self.agent.state.cur_iter, 2)async def asyncTearDown(self) -> None:"""The async teardown method."""diff --git a/tests/agent_structured_output_test.py b/tests/agent_structured_output_test.pyindex 2d95f5a49..2b8446abc 100644--- a/tests/agent_structured_output_test.py+++ b/tests/agent_structured_output_test.py@@ -138,7 +138,7 @@ class AgentStructuredOutputTest(IsolatedAsyncioTestCase):self.agent.state.reply_context.model_dump(),{"reply_id": self.agent.state.reply_id,- "cur_iter": 2,+ "cur_iter": 1,"structured_schema": WeatherReport.model_json_schema(),"structured_output": {"city": "Hangzhou", "temperature": 25.0},},diff --git a/tests/hitl_user_confirmation_test.py b/tests/hitl_user_confirmation_test.pyindex 70aa38e13..cf4a38f7e 100644--- a/tests/hitl_user_confirmation_test.py+++ b/tests/hitl_user_confirmation_test.py@@ -1748,5 +1748,84 @@ class AgentUserConfirmationTest(IsolatedAsyncioTestCase):[self.final_response_text],)+ async def test_partial_concurrent_confirmation_defers_iteration(+ self,+ ) -> None:+ """A tool round is not counted while one confirmed call is pending."""+ name_a = self.concurrent_tool_name+ name_b = "mock_user_confirm_concurrent_tool_b"+ self.agent.toolkit = Toolkit(+ tools=[+ MockUserConfirmConcurrentTool(),+ MockUserConfirmConcurrentToolB(),+ ],+ )+ self.model.set_responses(+ [+ ChatResponse(+ content=[+ ToolCallBlock(+ id=self.tool_call_id_1,+ name=name_a,+ input=self.tool_input_1,+ ),+ ToolCallBlock(+ id=self.tool_call_id_2,+ name=name_b,+ input=self.tool_input_2,+ ),+ ],+ is_last=True,+ ),+ self.final_mock_responses,+ ],+ )++ parked = await self.agent.reply(+ UserMsg(name="user", content=self.user_input_text),+ )+ self.assertIsNone(parked.finished_reason)+ self.assertEqual(self.agent.state.cur_iter, 0)++ partial = await self.agent.reply(+ UserConfirmResultEvent(+ reply_id=self.agent.state.reply_id,+ confirm_results=[+ ConfirmResult(+ confirmed=True,+ tool_call=ToolCallBlock(+ id=self.tool_call_id_1,+ name=name_a,+ input=self.tool_input_1,+ ),+ ),+ ],+ ),+ )+ self.assertIsNone(partial.finished_reason)+ # The first call has run, but the round isn't over while the second+ # one is still awaiting confirmation+ self.assertEqual(self.agent.state.cur_iter, 0)++ completed = await self.agent.reply(+ UserConfirmResultEvent(+ reply_id=self.agent.state.reply_id,+ confirm_results=[+ ConfirmResult(+ confirmed=True,+ tool_call=ToolCallBlock(+ id=self.tool_call_id_2,+ name=name_b,+ input=self.tool_input_2,+ ),+ ),+ ],+ ),+ )+ self.assertEqual(completed.finished_reason, "completed")+ self.assertEqual(self.model.cnt, 2)+ # One tool round plus the final reasoning+ self.assertEqual(self.agent.state.cur_iter, 2)+async def asyncTearDown(self) -> None:"""The async teardown method."""