| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- from unittest.mock import Mock, patch
- import pytest
- from opendevin.controller.agent_controller import AgentController
- from opendevin.events.action import CmdRunAction, FileReadAction, MessageAction
- from opendevin.events.observation import (
- CmdOutputObservation,
- FileReadObservation,
- Observation,
- )
- from opendevin.events.observation.empty import NullObservation
- from opendevin.events.observation.error import ErrorObservation
- from opendevin.events.stream import EventSource
- class TestAgentController:
- @pytest.fixture
- def controller(self):
- controller = Mock(spec=AgentController)
- controller._is_stuck = AgentController._is_stuck.__get__(
- controller, AgentController
- )
- controller.delegate = None
- controller.state = Mock()
- controller.state.history = []
- return controller
- def test_history_too_short(self, controller):
- controller.state.history = [
- (
- MessageAction(content='Hello', wait_for_response=False),
- Observation(content='Response 1'),
- ),
- (
- CmdRunAction(command='ls'),
- CmdOutputObservation(
- command_id=1, command='ls', content='file1.txt\nfile2.txt'
- ),
- ),
- ]
- assert controller._is_stuck() is False
- def test_is_stuck_repeating_action_null_observation(self, controller):
- # message actions with source USER are not considered in the stuck check
- message_action = MessageAction(content='Done', wait_for_response=False)
- message_action._source = EventSource.USER
- controller.state.history = [
- (
- MessageAction(content='Hello', wait_for_response=False),
- Observation(content='Response 1'),
- ),
- (CmdRunAction(command='ls'), NullObservation(content='')),
- (CmdRunAction(command='ls'), NullObservation(content='')),
- # user message shouldn't break detection
- (message_action, NullObservation(content='')),
- (CmdRunAction(command='ls'), NullObservation(content='')),
- (CmdRunAction(command='ls'), NullObservation(content='')),
- ]
- with patch('logging.Logger.warning') as mock_warning:
- assert controller._is_stuck() is True
- mock_warning.assert_called_once_with('Action, Observation loop detected')
- def test_is_stuck_repeating_action_error_observation(self, controller):
- message_action = MessageAction(content='Done', wait_for_response=False)
- message_action._source = EventSource.USER
- controller.state.history = [
- (
- MessageAction(content='Hello', wait_for_response=False),
- Observation(content='Response 1'),
- ),
- (
- CmdRunAction(command='invalid_command'),
- ErrorObservation(content='Command not found'),
- ),
- (
- CmdRunAction(command='invalid_command'),
- ErrorObservation(content='Command not found'),
- ),
- # user message shouldn't break detection
- (message_action, NullObservation(content='')),
- (
- CmdRunAction(command='invalid_command'),
- ErrorObservation(content='Different error'),
- ),
- (
- CmdRunAction(command='invalid_command'),
- ErrorObservation(content='Command not found'),
- ),
- ]
- with patch('logging.Logger.warning') as mock_warning:
- assert controller._is_stuck() is True
- mock_warning.assert_called_once_with(
- 'Action, ErrorObservation loop detected'
- )
- def test_is_stuck_repeating_action_observation_pattern(self, controller):
- # six tuples of action, observation
- message_action = MessageAction(content='Come on', wait_for_response=False)
- message_action._source = EventSource.USER
- controller.state.history = [
- (
- message_action,
- Observation(content=''),
- ),
- (
- CmdRunAction(command='ls'),
- CmdOutputObservation(
- command_id=1, command='ls', content='file1.txt\nfile2.txt'
- ),
- ),
- (
- FileReadAction(path='file1.txt'),
- FileReadObservation(content='File content', path='file1.txt'),
- ),
- (
- CmdRunAction(command='ls'),
- CmdOutputObservation(
- command_id=1, command='ls', content='file1.txt\nfile2.txt'
- ),
- ),
- (
- FileReadAction(path='file1.txt'),
- FileReadObservation(content='File content', path='file1.txt'),
- ),
- # insert a message just because they can, it shouldn't break detection
- (message_action, NullObservation(content='')),
- (
- CmdRunAction(command='ls'),
- CmdOutputObservation(
- command_id=1, command='ls', content='file1.txt\nfile2.txt'
- ),
- ),
- (
- FileReadAction(path='file1.txt'),
- FileReadObservation(content='File content', path='file1.txt'),
- ),
- ]
- with patch('logging.Logger.warning') as mock_warning:
- assert controller._is_stuck() is True
- mock_warning.assert_called_once_with('Action, Observation pattern detected')
- def test_is_stuck_not_stuck(self, controller):
- message_action = MessageAction(content='Done', wait_for_response=False)
- message_action._source = EventSource.USER
- controller.state.history = [
- (
- MessageAction(content='Hello', wait_for_response=False),
- Observation(content='Response 1'),
- ),
- (
- CmdRunAction(command='ls'),
- CmdOutputObservation(
- command_id=1, command='ls', content='file1.txt\nfile2.txt'
- ),
- ),
- (
- FileReadAction(path='file1.txt'),
- FileReadObservation(content='File content', path='file1.txt'),
- ),
- (
- CmdRunAction(command='pwd'),
- CmdOutputObservation(command_id=1, command='pwd', content='/home/user'),
- ),
- (
- FileReadAction(path='file2.txt'),
- Observation(content='Another file content'),
- ),
- # insert a message from the user
- (message_action, NullObservation(content='')),
- (
- CmdRunAction(command='pwd'),
- CmdOutputObservation(command_id=1, command='pwd', content='/home/user'),
- ),
- (
- FileReadAction(path='file2.txt'),
- Observation(content='Another file content'),
- ),
- ]
- assert controller._is_stuck() is False
- def test_is_stuck_four_identical_tuples(self, controller):
- message_action = MessageAction(content='Done', wait_for_response=False)
- message_action._source = EventSource.USER
- controller.state.history = [
- (
- MessageAction(content='Hello', wait_for_response=False),
- Observation(content='Response 1'),
- ),
- (
- CmdRunAction(command='ls'),
- CmdOutputObservation(
- command_id=1, command='ls', content='file1.txt\nfile2.txt'
- ),
- ),
- (
- CmdRunAction(command='ls'),
- CmdOutputObservation(
- command_id=1, command='ls', content='file1.txt\nfile2.txt'
- ),
- ),
- # message from the user
- (message_action, NullObservation(content='')),
- (
- CmdRunAction(command='ls'),
- CmdOutputObservation(
- command_id=1, command='ls', content='file1.txt\nfile2.txt'
- ),
- ),
- (
- CmdRunAction(command='ls'),
- CmdOutputObservation(
- command_id=1, command='ls', content='file1.txt\nfile2.txt'
- ),
- ),
- ]
- with patch('logging.Logger.warning') as mock_warning:
- assert controller._is_stuck() is True
- mock_warning.assert_called_once_with('Action, Observation loop detected')
- def test_is_stuck_delegate_stuck(self, controller):
- controller.delegate = Mock()
- controller.delegate._is_stuck.return_value = True
- assert controller._is_stuck() is True
|