test_agent_controller.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. import asyncio
  2. from unittest.mock import AsyncMock, MagicMock
  3. import pytest
  4. from openhands.controller.agent import Agent
  5. from openhands.controller.agent_controller import AgentController
  6. from openhands.controller.state.state import TrafficControlState
  7. from openhands.core.exceptions import LLMMalformedActionError
  8. from openhands.core.schema import AgentState
  9. from openhands.events import EventStream
  10. from openhands.events.action import ChangeAgentStateAction, MessageAction
  11. @pytest.fixture
  12. def temp_dir(tmp_path_factory: pytest.TempPathFactory) -> str:
  13. return str(tmp_path_factory.mktemp('test_event_stream'))
  14. @pytest.fixture(scope='function')
  15. def event_loop():
  16. loop = asyncio.get_event_loop_policy().new_event_loop()
  17. yield loop
  18. loop.close()
  19. @pytest.fixture
  20. def mock_agent():
  21. return MagicMock(spec=Agent)
  22. @pytest.fixture
  23. def mock_event_stream():
  24. return MagicMock(spec=EventStream)
  25. @pytest.mark.asyncio
  26. async def test_set_agent_state(mock_agent, mock_event_stream):
  27. controller = AgentController(
  28. agent=mock_agent,
  29. event_stream=mock_event_stream,
  30. max_iterations=10,
  31. sid='test',
  32. confirmation_mode=False,
  33. headless_mode=True,
  34. )
  35. await controller.set_agent_state_to(AgentState.RUNNING)
  36. assert controller.get_agent_state() == AgentState.RUNNING
  37. await controller.set_agent_state_to(AgentState.PAUSED)
  38. assert controller.get_agent_state() == AgentState.PAUSED
  39. await controller.close()
  40. @pytest.mark.asyncio
  41. async def test_on_event_message_action(mock_agent, mock_event_stream):
  42. controller = AgentController(
  43. agent=mock_agent,
  44. event_stream=mock_event_stream,
  45. max_iterations=10,
  46. sid='test',
  47. confirmation_mode=False,
  48. headless_mode=True,
  49. )
  50. controller.state.agent_state = AgentState.RUNNING
  51. message_action = MessageAction(content='Test message')
  52. await controller.on_event(message_action)
  53. assert controller.get_agent_state() == AgentState.RUNNING
  54. await controller.close()
  55. @pytest.mark.asyncio
  56. async def test_on_event_change_agent_state_action(mock_agent, mock_event_stream):
  57. controller = AgentController(
  58. agent=mock_agent,
  59. event_stream=mock_event_stream,
  60. max_iterations=10,
  61. sid='test',
  62. confirmation_mode=False,
  63. headless_mode=True,
  64. )
  65. controller.state.agent_state = AgentState.RUNNING
  66. change_state_action = ChangeAgentStateAction(agent_state=AgentState.PAUSED)
  67. await controller.on_event(change_state_action)
  68. assert controller.get_agent_state() == AgentState.PAUSED
  69. await controller.close()
  70. @pytest.mark.asyncio
  71. async def test_report_error(mock_agent, mock_event_stream):
  72. controller = AgentController(
  73. agent=mock_agent,
  74. event_stream=mock_event_stream,
  75. max_iterations=10,
  76. sid='test',
  77. confirmation_mode=False,
  78. headless_mode=True,
  79. )
  80. error_message = 'Test error'
  81. await controller.report_error(error_message)
  82. assert controller.state.last_error == error_message
  83. controller.event_stream.add_event.assert_called_once()
  84. await controller.close()
  85. @pytest.mark.asyncio
  86. async def test_step_with_exception(mock_agent, mock_event_stream):
  87. controller = AgentController(
  88. agent=mock_agent,
  89. event_stream=mock_event_stream,
  90. max_iterations=10,
  91. sid='test',
  92. confirmation_mode=False,
  93. headless_mode=True,
  94. )
  95. controller.state.agent_state = AgentState.RUNNING
  96. controller.report_error = AsyncMock()
  97. controller.agent.step.side_effect = LLMMalformedActionError('Malformed action')
  98. await controller._step()
  99. # Verify that report_error was called with the correct error message
  100. controller.report_error.assert_called_once_with('Malformed action')
  101. await controller.close()
  102. @pytest.mark.asyncio
  103. async def test_step_max_iterations(mock_agent, mock_event_stream):
  104. controller = AgentController(
  105. agent=mock_agent,
  106. event_stream=mock_event_stream,
  107. max_iterations=10,
  108. sid='test',
  109. confirmation_mode=False,
  110. headless_mode=False,
  111. )
  112. controller.state.agent_state = AgentState.RUNNING
  113. controller.state.iteration = 10
  114. assert controller.state.traffic_control_state == TrafficControlState.NORMAL
  115. await controller._step()
  116. assert controller.state.traffic_control_state == TrafficControlState.THROTTLING
  117. assert controller.state.agent_state == AgentState.PAUSED
  118. await controller.close()
  119. @pytest.mark.asyncio
  120. async def test_step_max_iterations_headless(mock_agent, mock_event_stream):
  121. controller = AgentController(
  122. agent=mock_agent,
  123. event_stream=mock_event_stream,
  124. max_iterations=10,
  125. sid='test',
  126. confirmation_mode=False,
  127. headless_mode=True,
  128. )
  129. controller.state.agent_state = AgentState.RUNNING
  130. controller.state.iteration = 10
  131. assert controller.state.traffic_control_state == TrafficControlState.NORMAL
  132. await controller._step()
  133. assert controller.state.traffic_control_state == TrafficControlState.THROTTLING
  134. # In headless mode, throttling results in an error
  135. assert controller.state.agent_state == AgentState.ERROR
  136. await controller.close()
  137. @pytest.mark.asyncio
  138. async def test_step_max_budget(mock_agent, mock_event_stream):
  139. controller = AgentController(
  140. agent=mock_agent,
  141. event_stream=mock_event_stream,
  142. max_iterations=10,
  143. max_budget_per_task=10,
  144. sid='test',
  145. confirmation_mode=False,
  146. headless_mode=False,
  147. )
  148. controller.state.agent_state = AgentState.RUNNING
  149. controller.state.metrics.accumulated_cost = 10.1
  150. assert controller.state.traffic_control_state == TrafficControlState.NORMAL
  151. await controller._step()
  152. assert controller.state.traffic_control_state == TrafficControlState.THROTTLING
  153. assert controller.state.agent_state == AgentState.PAUSED
  154. await controller.close()
  155. @pytest.mark.asyncio
  156. async def test_step_max_budget_headless(mock_agent, mock_event_stream):
  157. controller = AgentController(
  158. agent=mock_agent,
  159. event_stream=mock_event_stream,
  160. max_iterations=10,
  161. max_budget_per_task=10,
  162. sid='test',
  163. confirmation_mode=False,
  164. headless_mode=True,
  165. )
  166. controller.state.agent_state = AgentState.RUNNING
  167. controller.state.metrics.accumulated_cost = 10.1
  168. assert controller.state.traffic_control_state == TrafficControlState.NORMAL
  169. await controller._step()
  170. assert controller.state.traffic_control_state == TrafficControlState.THROTTLING
  171. # In headless mode, throttling results in an error
  172. assert controller.state.agent_state == AgentState.ERROR
  173. await controller.close()