| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298 |
- 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.action.commands import CmdKillAction
- 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._eq_no_pid = AgentController._eq_no_pid.__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):
- # (action, error_observation), not necessarily the same error
- 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 still not found or another error'),
- ),
- # 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'),
- # command_id is ignored for the eq check, it's a pid
- CmdOutputObservation(
- command_id=2, 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=3, 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'),
- # command_id is ignored for the eq check, it's the pid
- CmdOutputObservation(command_id=2, 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=3, command='pwd', content='/home/user'),
- ),
- (
- FileReadAction(path='file2.txt'),
- Observation(content='Another file content'),
- ),
- ]
- assert controller._is_stuck() is False
- def test_is_stuck_three_identical_tuples(self, controller):
- # the same (action, observation), three times
- # prepare messages to interrupt things
- message_action = MessageAction(content='Done', wait_for_response=False)
- message_action._source = EventSource.USER
- message_action2 = MessageAction(content='Or not done?', wait_for_response=False)
- message_action2._source = EventSource.USER
- controller.state.history = [
- (
- MessageAction(content='Hello', wait_for_response=False),
- Observation(content='Response 1'),
- ),
- # message from the user shouldn't interfere with the detection
- (message_action, NullObservation(content='')),
- (
- CmdRunAction(command='ls'),
- CmdOutputObservation(
- command_id=1, command='ls', content='file1.txt\nfile2.txt'
- ),
- ),
- (
- CmdRunAction(command='ls'),
- # command_id is ignored for the eq check, it's just the pid
- CmdOutputObservation(
- command_id=2, command='ls', content='file1.txt\nfile2.txt'
- ),
- ),
- # message from the user shouldn't interfere with the detection
- (message_action2, NullObservation(content='')),
- (
- CmdRunAction(command='ls'),
- CmdOutputObservation(
- command_id=3, 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_four_tuples_cmd_kill_and_output(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'),
- ),
- (
- CmdKillAction(
- command_id=1,
- thought='It looks like storybook is stuck, lets kill it',
- ),
- CmdOutputObservation(
- content='Background command storybook has been killed.',
- command_id=1,
- command='storybook',
- exit_code=0,
- ),
- ),
- (
- # command_id is ignored for the eq check, it's the pid
- CmdKillAction(
- command_id=2,
- thought='It looks like storybook is stuck, lets kill it',
- ),
- # command_id here too
- CmdOutputObservation(
- content='Background command storybook has been killed.',
- command_id=2,
- command='storybook',
- exit_code=0,
- ),
- ),
- # message from the user, shouldn't be counted
- (message_action, NullObservation(content='')),
- (
- CmdKillAction(
- command_id=3,
- thought='It looks like storybook is stuck, lets kill it',
- ),
- CmdOutputObservation(
- content='Background command storybook has been killed.',
- command_id=3,
- command='storybook',
- exit_code=0,
- ),
- ),
- (
- CmdKillAction(
- command_id=4,
- thought='It looks like storybook is stuck, lets kill it',
- ),
- CmdOutputObservation(
- content='Background command storybook has been killed.',
- command_id=4,
- command='storybook',
- exit_code=0,
- ),
- ),
- ]
- 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
|