Skip to content

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

fix(agent): count rounds completed during HITL resumption toward max_iters (#3645)

golemon337562e22316
diff --git a/src/agentscope/agent/_agent.py b/src/agentscope/agent/_agent.py
index e2f946f63..7ab79bc2a 100644
--- a/src/agentscope/agent/_agent.py
+++ b/src/agentscope/agent/_agent.py
@@ -1120,6 +1120,9 @@ class Agent:
if is_awaiting:
async for evt in self._handle_incoming_event(event):
yield evt
+ # Count the round if this event completed its last tool call.
+ if not self.state.get_unfinished_tool_calls(self.name):
+ self.state.cur_iter += 1
else:
await self._handle_incoming_messages(msgs)
# Update the context with the incoming message and state
diff --git a/tests/hitl_external_execution_test.py b/tests/hitl_external_execution_test.py
index 3fd98d8fe..8ed24b4ad 100644
--- a/tests/hitl_external_execution_test.py
+++ b/tests/hitl_external_execution_test.py
@@ -3,6 +3,7 @@
"""Test the external execution events in the agent class."""
from typing import Any
from unittest.async_case import IsolatedAsyncioTestCase
+from unittest.mock import patch
from utils import AnyString, MockModel
@@ -1483,5 +1484,131 @@ class AgentExternalExecutionTest(IsolatedAsyncioTestCase):
]
self.assertListEqual(context_dicts, expected_context_final)
+ async def test_external_resumptions_enforce_iteration_budget(self) -> None:
+ """Each resumed round counts toward the iteration budget."""
+ self.agent.toolkit = Toolkit(tools=[MockExternalConcurrentTool()])
+ for max_iters in (1, 2):
+ with self.subTest(max_iters=max_iters):
+ self.agent.react_config.max_iters = max_iters
+ tool_calls = [
+ ToolCallBlock(
+ id=call_id,
+ name=self.concurrent_tool_name,
+ input=self.tool_input_1,
+ )
+ for call_id in (
+ self.tool_call_id_1,
+ self.tool_call_id_2,
+ )[:max_iters]
+ ]
+ self.model.set_responses(
+ [
+ *[
+ ChatResponse(content=[call], is_last=True)
+ for call in tool_calls
+ ],
+ self.final_mock_responses,
+ ],
+ )
+ with patch.object(
+ MockModel,
+ "__call__",
+ wraps=self.model.__call__,
+ ) as model_call:
+ parked = await self.agent.reply(
+ UserMsg(name="user", content=self.user_input_text),
+ )
+ self.assertEqual(
+ (parked.finished_reason, self.agent.state.cur_iter),
+ (None, 0),
+ )
+ for index, tool_call in enumerate(tool_calls, start=1):
+ result = await self.agent.reply(
+ ExternalExecutionResultEvent(
+ reply_id=self.agent.state.reply_id,
+ execution_results=[
+ ToolResultBlock(
+ id=tool_call.id,
+ name=tool_call.name,
+ output=[
+ TextBlock(
+ text=self.concurrent_result_1,
+ ),
+ ],
+ state=ToolResultState.SUCCESS,
+ ),
+ ],
+ ),
+ )
+ is_final = index == max_iters
+ self.assertEqual(
+ (
+ result.finished_reason,
+ self.agent.state.cur_iter,
+ self.model.cnt,
+ ),
+ (
+ "exceed_max_iters" if is_final else None,
+ index + 1 if is_final else index,
+ index + 1,
+ ),
+ )
+ self.assertEqual(
+ model_call.call_args.kwargs["tool_choice"].mode,
+ "none",
+ )
+
+ async def test_partial_external_resumption_counts_round_once(self) -> None:
+ """Partial results count only when the last tool call completes."""
+ self.agent.toolkit = Toolkit(tools=[MockExternalConcurrentTool()])
+ self.agent.react_config.max_iters = 2
+ tool_calls = [
+ ToolCallBlock(
+ id=call_id,
+ name=self.concurrent_tool_name,
+ input=self.tool_input_1,
+ )
+ for call_id in (self.tool_call_id_1, self.tool_call_id_2)
+ ]
+ self.model.set_responses(
+ [
+ ChatResponse(content=tool_calls, is_last=True),
+ self.final_mock_responses,
+ ],
+ )
+ parked = await self.agent.reply(
+ UserMsg(name="user", content=self.user_input_text),
+ )
+ self.assertEqual(
+ (
+ parked.finished_reason,
+ self.agent.state.cur_iter,
+ self.model.cnt,
+ ),
+ (None, 0, 1),
+ )
+ for index, tool_call in enumerate(tool_calls):
+ result = await self.agent.reply(
+ ExternalExecutionResultEvent(
+ reply_id=self.agent.state.reply_id,
+ execution_results=[
+ ToolResultBlock(
+ id=tool_call.id,
+ name=tool_call.name,
+ output=[TextBlock(text=self.concurrent_result_1)],
+ state=ToolResultState.SUCCESS,
+ ),
+ ],
+ ),
+ )
+ self.assertEqual(
+ (
+ result.finished_reason,
+ self.agent.state.cur_iter,
+ self.model.cnt,
+ ),
+ (None, 0, 1) if index == 0 else ("completed", 2, 2),
+ )
+
async def asyncTearDown(self) -> None:
"""The async teardown method."""
diff --git a/tests/hitl_user_confirmation_test.py b/tests/hitl_user_confirmation_test.py
index 060d388c0..9d0860b75 100644
--- a/tests/hitl_user_confirmation_test.py
+++ b/tests/hitl_user_confirmation_test.py
@@ -3,6 +3,7 @@
"""Test the user confirmation events in the agent class."""
from typing import Any
from unittest.async_case import IsolatedAsyncioTestCase
+from unittest.mock import patch
from utils import AnyString, MockModel
from agentscope.agent import Agent, InjectionConfig
@@ -1838,5 +1839,62 @@ class AgentUserConfirmationTest(IsolatedAsyncioTestCase):
# One tool round plus the final reasoning
self.assertEqual(self.agent.state.cur_iter, 2)
+ async def test_confirmation_resumption_counts_round_once(self) -> None:
+ """Denied and approved rounds each count once toward the budget."""
+ self.agent.toolkit = Toolkit(tools=[MockUserConfirmConcurrentTool()])
+ for confirmed, max_iters, finished_reason in (
+ (False, 1, "exceed_max_iters"),
+ (True, 2, "completed"),
+ ):
+ with self.subTest(confirmed=confirmed):
+ self.agent.react_config.max_iters = max_iters
+ tool_call = ToolCallBlock(
+ id=self.tool_call_id_1,
+ name=self.concurrent_tool_name,
+ input=self.tool_input_1,
+ )
+ self.model.set_responses(
+ [
+ ChatResponse(content=[tool_call], is_last=True),
+ self.final_mock_responses,
+ ],
+ )
+ with patch.object(
+ MockModel,
+ "__call__",
+ wraps=self.model.__call__,
+ ) as model_call:
+ parked = await self.agent.reply(
+ UserMsg(name="user", content=self.user_input_text),
+ )
+ self.assertEqual(
+ (parked.finished_reason, self.agent.state.cur_iter),
+ (None, 0),
+ )
+ final = await self.agent.reply(
+ UserConfirmResultEvent(
+ reply_id=self.agent.state.reply_id,
+ confirm_results=[
+ ConfirmResult(
+ confirmed=confirmed,
+ tool_call=tool_call,
+ ),
+ ],
+ ),
+ )
+ self.assertEqual(
+ (
+ final.finished_reason,
+ self.agent.state.cur_iter,
+ self.model.cnt,
+ ),
+ (finished_reason, 2, 2),
+ )
+ if not confirmed:
+ self.assertEqual(
+ model_call.call_args.kwargs["tool_choice"].mode,
+ "none",
+ )
+
async def asyncTearDown(self) -> None:
"""The async teardown method."""