File size: 2,122 Bytes
246d201 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
import asyncio
import pytest
from openhands.controller.agent_controller import AgentController
from openhands.core.schema import AgentState
from openhands.events import EventStream
from openhands.events.action import MessageAction
from openhands.events.event import EventSource
class DummyAgent:
def __init__(self):
self.name = 'dummy'
self.llm = type(
'DummyLLM',
(),
{'metrics': type('DummyMetrics', (), {'merge': lambda x: None})()},
)()
def reset(self):
pass
@pytest.mark.asyncio
async def test_iteration_limit_extends_on_user_message():
# Initialize test components
from openhands.storage.memory import InMemoryFileStore
file_store = InMemoryFileStore()
event_stream = EventStream(sid='test', file_store=file_store)
agent = DummyAgent()
initial_max_iterations = 100
controller = AgentController(
agent=agent,
event_stream=event_stream,
max_iterations=initial_max_iterations,
sid='test',
headless_mode=False,
)
# Set initial state
await controller.set_agent_state_to(AgentState.RUNNING)
controller.state.iteration = 90 # Close to the limit
assert controller.state.max_iterations == initial_max_iterations
# Simulate user message
user_message = MessageAction('test message', EventSource.USER)
event_stream.add_event(user_message, EventSource.USER)
await asyncio.sleep(0.1) # Give time for event to be processed
# Verify max_iterations was extended
assert controller.state.max_iterations == 90 + initial_max_iterations
# Simulate more iterations and another user message
controller.state.iteration = 180 # Close to new limit
user_message2 = MessageAction('another message', EventSource.USER)
event_stream.add_event(user_message2, EventSource.USER)
await asyncio.sleep(0.1) # Give time for event to be processed
# Verify max_iterations was extended again
assert controller.state.max_iterations == 180 + initial_max_iterations
|