File size: 25,957 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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 |
import logging
from unittest.mock import Mock, patch
import pytest
from pytest import TempPathFactory
from openhands.controller.agent_controller import AgentController
from openhands.controller.state.state import State
from openhands.controller.stuck import StuckDetector
from openhands.events.action import CmdRunAction, FileReadAction, MessageAction
from openhands.events.action.commands import IPythonRunCellAction
from openhands.events.observation import (
CmdOutputObservation,
FileReadObservation,
)
from openhands.events.observation.commands import IPythonRunCellObservation
from openhands.events.observation.empty import NullObservation
from openhands.events.observation.error import ErrorObservation
from openhands.events.stream import EventSource, EventStream
from openhands.storage import get_file_store
def collect_events(stream):
return [event for event in stream.get_events()]
logging.basicConfig(level=logging.DEBUG)
jupyter_line_1 = '\n[Jupyter current working directory:'
jupyter_line_2 = '\n[Jupyter Python interpreter:'
code_snippet = """
edit_file_by_replace(
'book_store.py',
to_replace=\"""def total(basket):
if not basket:
return 0
"""
@pytest.fixture
def temp_dir(tmp_path_factory: TempPathFactory) -> str:
return str(tmp_path_factory.mktemp('test_is_stuck'))
@pytest.fixture
def event_stream(temp_dir):
file_store = get_file_store('local', temp_dir)
event_stream = EventStream('asdf', file_store)
yield event_stream
# clear after each test
event_stream.clear()
class TestStuckDetector:
@pytest.fixture
def stuck_detector(self):
state = State(inputs={}, max_iterations=50)
state.history = [] # Initialize history as an empty list
return StuckDetector(state)
def _impl_syntax_error_events(
self,
state: State,
error_message: str,
random_line: bool,
incidents: int = 4,
):
for i in range(incidents):
ipython_action = IPythonRunCellAction(code=code_snippet)
state.history.append(ipython_action)
extra_number = (i + 1) * 10 if random_line else '42'
extra_line = '\n' * (i + 1) if random_line else ''
ipython_observation = IPythonRunCellObservation(
content=f' Cell In[1], line {extra_number}\n'
'to_replace="""def largest(min_factor, max_factor):\n ^\n'
f'{error_message}{extra_line}' + jupyter_line_1 + jupyter_line_2,
code=code_snippet,
)
# ipython_observation._cause = ipython_action._id
state.history.append(ipython_observation)
def _impl_unterminated_string_error_events(
self, state: State, random_line: bool, incidents: int = 4
):
for i in range(incidents):
ipython_action = IPythonRunCellAction(code=code_snippet)
state.history.append(ipython_action)
line_number = (i + 1) * 10 if random_line else '1'
ipython_observation = IPythonRunCellObservation(
content=f'print(" Cell In[1], line {line_number}\nhello\n ^\nSyntaxError: unterminated string literal (detected at line {line_number})'
+ jupyter_line_1
+ jupyter_line_2,
code=code_snippet,
)
# ipython_observation._cause = ipython_action._
state.history.append(ipython_observation)
def test_history_too_short(self, stuck_detector: StuckDetector):
state = stuck_detector.state
message_action = MessageAction(content='Hello', wait_for_response=False)
message_action._source = EventSource.USER
observation = NullObservation(content='')
# observation._cause = message_action.id
state.history.append(message_action)
state.history.append(observation)
cmd_action = CmdRunAction(command='ls')
state.history.append(cmd_action)
cmd_observation = CmdOutputObservation(
command='ls', content='file1.txt\nfile2.txt'
)
# cmd_observation._cause = cmd_action._id
state.history.append(cmd_observation)
assert stuck_detector.is_stuck(headless_mode=True) is False
def test_interactive_mode_resets_after_user_message(
self, stuck_detector: StuckDetector
):
state = stuck_detector.state
# First add some actions that would be stuck in non-UI mode
for i in range(4):
cmd_action = CmdRunAction(command='ls')
cmd_action._id = i
state.history.append(cmd_action)
cmd_observation = CmdOutputObservation(
content='', command='ls', command_id=i
)
cmd_observation._cause = cmd_action._id
state.history.append(cmd_observation)
# In headless mode, this should be stuck
assert stuck_detector.is_stuck(headless_mode=True) is True
# with the UI, it will ALSO be stuck initially
assert stuck_detector.is_stuck(headless_mode=False) is True
# Add a user message
message_action = MessageAction(content='Hello', wait_for_response=False)
message_action._source = EventSource.USER
state.history.append(message_action)
# In not-headless mode, this should not be stuck because we ignore history before user message
assert stuck_detector.is_stuck(headless_mode=False) is False
# But in headless mode, this should be still stuck because user messages do not count
assert stuck_detector.is_stuck(headless_mode=True) is True
# Add two more identical actions - still not stuck because we need at least 3
for i in range(2):
cmd_action = CmdRunAction(command='ls')
cmd_action._id = i + 4
state.history.append(cmd_action)
cmd_observation = CmdOutputObservation(
content='', command='ls', command_id=i + 4
)
cmd_observation._cause = cmd_action._id
state.history.append(cmd_observation)
assert stuck_detector.is_stuck(headless_mode=False) is False
# Add two more identical actions - now it should be stuck
for i in range(2):
cmd_action = CmdRunAction(command='ls')
cmd_action._id = i + 6
state.history.append(cmd_action)
cmd_observation = CmdOutputObservation(
content='', command='ls', command_id=i + 6
)
cmd_observation._cause = cmd_action._id
state.history.append(cmd_observation)
assert stuck_detector.is_stuck(headless_mode=False) is True
def test_is_stuck_repeating_action_observation(self, stuck_detector: StuckDetector):
state = stuck_detector.state
message_action = MessageAction(content='Done', wait_for_response=False)
message_action._source = EventSource.USER
hello_action = MessageAction(content='Hello', wait_for_response=False)
hello_observation = NullObservation('')
# 2 events
state.history.append(hello_action)
state.history.append(hello_observation)
cmd_action_1 = CmdRunAction(command='ls')
cmd_action_1._id = 1
state.history.append(cmd_action_1)
cmd_observation_1 = CmdOutputObservation(content='', command='ls')
cmd_observation_1._cause = cmd_action_1._id
state.history.append(cmd_observation_1)
# 4 events
cmd_action_2 = CmdRunAction(command='ls')
cmd_action_2._id = 2
state.history.append(cmd_action_2)
cmd_observation_2 = CmdOutputObservation(content='', command='ls')
cmd_observation_2._cause = cmd_action_2._id
state.history.append(cmd_observation_2)
# 6 events
# random user message just because we can
message_null_observation = NullObservation(content='')
state.history.append(message_action)
state.history.append(message_null_observation)
# 8 events
assert stuck_detector.is_stuck(headless_mode=True) is False
cmd_action_3 = CmdRunAction(command='ls')
cmd_action_3._id = 3
state.history.append(cmd_action_3)
cmd_observation_3 = CmdOutputObservation(content='', command='ls')
cmd_observation_3._cause = cmd_action_3._id
state.history.append(cmd_observation_3)
# 10 events
assert len(state.history) == 10
assert stuck_detector.is_stuck(headless_mode=True) is False
cmd_action_4 = CmdRunAction(command='ls')
cmd_action_4._id = 4
state.history.append(cmd_action_4)
cmd_observation_4 = CmdOutputObservation(content='', command='ls')
cmd_observation_4._cause = cmd_action_4._id
state.history.append(cmd_observation_4)
# 12 events
assert len(state.history) == 12
with patch('logging.Logger.warning') as mock_warning:
assert stuck_detector.is_stuck(headless_mode=True) is True
mock_warning.assert_called_once_with('Action, Observation loop detected')
def test_is_stuck_repeating_action_error(self, stuck_detector: StuckDetector):
state = stuck_detector.state
# (action, error_observation), not necessarily the same error
message_action = MessageAction(content='Done', wait_for_response=False)
message_action._source = EventSource.USER
hello_action = MessageAction(content='Hello', wait_for_response=False)
hello_observation = NullObservation(content='')
state.history.append(hello_action)
# hello_observation._cause = hello_action._id
state.history.append(hello_observation)
# 2 events
cmd_action_1 = CmdRunAction(command='invalid_command')
state.history.append(cmd_action_1)
error_observation_1 = ErrorObservation(content='Command not found')
# error_observation_1._cause = cmd_action_1._id
state.history.append(error_observation_1)
# 4 events
cmd_action_2 = CmdRunAction(command='invalid_command')
state.history.append(cmd_action_2)
error_observation_2 = ErrorObservation(
content='Command still not found or another error'
)
# error_observation_2._cause = cmd_action_2._id
state.history.append(error_observation_2)
# 6 events
message_null_observation = NullObservation(content='')
state.history.append(message_action)
state.history.append(message_null_observation)
# 8 events
cmd_action_3 = CmdRunAction(command='invalid_command')
state.history.append(cmd_action_3)
error_observation_3 = ErrorObservation(content='Different error')
# error_observation_3._cause = cmd_action_3._id
state.history.append(error_observation_3)
# 10 events
cmd_action_4 = CmdRunAction(command='invalid_command')
state.history.append(cmd_action_4)
error_observation_4 = ErrorObservation(content='Command not found')
# error_observation_4._cause = cmd_action_4._id
state.history.append(error_observation_4)
# 12 events
with patch('logging.Logger.warning') as mock_warning:
assert stuck_detector.is_stuck(headless_mode=True) is True
mock_warning.assert_called_once_with(
'Action, ErrorObservation loop detected'
)
def test_is_stuck_invalid_syntax_error(self, stuck_detector: StuckDetector):
state = stuck_detector.state
self._impl_syntax_error_events(
state,
error_message='SyntaxError: invalid syntax. Perhaps you forgot a comma?',
random_line=False,
)
with patch('logging.Logger.warning'):
assert stuck_detector.is_stuck(headless_mode=True) is True
def test_is_not_stuck_invalid_syntax_error_random_lines(
self, stuck_detector: StuckDetector
):
state = stuck_detector.state
self._impl_syntax_error_events(
state,
error_message='SyntaxError: invalid syntax. Perhaps you forgot a comma?',
random_line=True,
)
with patch('logging.Logger.warning'):
assert stuck_detector.is_stuck(headless_mode=True) is False
def test_is_not_stuck_invalid_syntax_error_only_three_incidents(
self, stuck_detector: StuckDetector
):
state = stuck_detector.state
self._impl_syntax_error_events(
state,
error_message='SyntaxError: invalid syntax. Perhaps you forgot a comma?',
random_line=True,
incidents=3,
)
with patch('logging.Logger.warning'):
assert stuck_detector.is_stuck(headless_mode=True) is False
def test_is_stuck_incomplete_input_error(self, stuck_detector: StuckDetector):
state = stuck_detector.state
self._impl_syntax_error_events(
state,
error_message='SyntaxError: incomplete input',
random_line=False,
)
with patch('logging.Logger.warning'):
assert stuck_detector.is_stuck(headless_mode=True) is True
def test_is_not_stuck_incomplete_input_error(self, stuck_detector: StuckDetector):
state = stuck_detector.state
self._impl_syntax_error_events(
state,
error_message='SyntaxError: incomplete input',
random_line=True,
)
with patch('logging.Logger.warning'):
assert stuck_detector.is_stuck(headless_mode=True) is False
def test_is_not_stuck_ipython_unterminated_string_error_random_lines(
self, stuck_detector: StuckDetector
):
state = stuck_detector.state
self._impl_unterminated_string_error_events(state, random_line=True)
with patch('logging.Logger.warning'):
assert stuck_detector.is_stuck(headless_mode=True) is False
def test_is_not_stuck_ipython_unterminated_string_error_only_three_incidents(
self, stuck_detector: StuckDetector
):
state = stuck_detector.state
self._impl_unterminated_string_error_events(
state, random_line=False, incidents=3
)
with patch('logging.Logger.warning'):
assert stuck_detector.is_stuck(headless_mode=True) is False
def test_is_stuck_ipython_unterminated_string_error(
self, stuck_detector: StuckDetector
):
state = stuck_detector.state
self._impl_unterminated_string_error_events(state, random_line=False)
with patch('logging.Logger.warning'):
assert stuck_detector.is_stuck(headless_mode=True) is True
def test_is_not_stuck_ipython_syntax_error_not_at_end(
self, stuck_detector: StuckDetector
):
state = stuck_detector.state
# this test is to make sure we don't get false positives
# since the "at line x" is changing in between!
ipython_action_1 = IPythonRunCellAction(code='print("hello')
state.history.append(ipython_action_1)
ipython_observation_1 = IPythonRunCellObservation(
content='print("hello\n ^\nSyntaxError: unterminated string literal (detected at line 1)\nThis is some additional output',
code='print("hello',
)
# ipython_observation_1._cause = ipython_action_1._id
state.history.append(ipython_observation_1)
ipython_action_2 = IPythonRunCellAction(code='print("hello')
state.history.append(ipython_action_2)
ipython_observation_2 = IPythonRunCellObservation(
content='print("hello\n ^\nSyntaxError: unterminated string literal (detected at line 1)\nToo much output here on and on',
code='print("hello',
)
# ipython_observation_2._cause = ipython_action_2._id
state.history.append(ipython_observation_2)
ipython_action_3 = IPythonRunCellAction(code='print("hello')
state.history.append(ipython_action_3)
ipython_observation_3 = IPythonRunCellObservation(
content='print("hello\n ^\nSyntaxError: unterminated string literal (detected at line 3)\nEnough',
code='print("hello',
)
# ipython_observation_3._cause = ipython_action_3._id
state.history.append(ipython_observation_3)
ipython_action_4 = IPythonRunCellAction(code='print("hello')
state.history.append(ipython_action_4)
ipython_observation_4 = IPythonRunCellObservation(
content='print("hello\n ^\nSyntaxError: unterminated string literal (detected at line 2)\nLast line of output',
code='print("hello',
)
# ipython_observation_4._cause = ipython_action_4._id
state.history.append(ipython_observation_4)
with patch('logging.Logger.warning') as mock_warning:
assert stuck_detector.is_stuck(headless_mode=True) is False
mock_warning.assert_not_called()
def test_is_stuck_repeating_action_observation_pattern(
self, stuck_detector: StuckDetector
):
state = stuck_detector.state
message_action = MessageAction(content='Come on', wait_for_response=False)
message_action._source = EventSource.USER
state.history.append(message_action)
message_observation = NullObservation(content='')
state.history.append(message_observation)
cmd_action_1 = CmdRunAction(command='ls')
state.history.append(cmd_action_1)
cmd_observation_1 = CmdOutputObservation(
command='ls', content='file1.txt\nfile2.txt'
)
# cmd_observation_1._cause = cmd_action_1._id
state.history.append(cmd_observation_1)
read_action_1 = FileReadAction(path='file1.txt')
state.history.append(read_action_1)
read_observation_1 = FileReadObservation(
content='File content', path='file1.txt'
)
# read_observation_1._cause = read_action_1._id
state.history.append(read_observation_1)
cmd_action_2 = CmdRunAction(command='ls')
state.history.append(cmd_action_2)
cmd_observation_2 = CmdOutputObservation(
command='ls', content='file1.txt\nfile2.txt'
)
# cmd_observation_2._cause = cmd_action_2._id
state.history.append(cmd_observation_2)
read_action_2 = FileReadAction(path='file1.txt')
state.history.append(read_action_2)
read_observation_2 = FileReadObservation(
content='File content', path='file1.txt'
)
# read_observation_2._cause = read_action_2._id
state.history.append(read_observation_2)
message_action = MessageAction(content='Come on', wait_for_response=False)
message_action._source = EventSource.USER
state.history.append(message_action)
message_null_observation = NullObservation(content='')
state.history.append(message_null_observation)
cmd_action_3 = CmdRunAction(command='ls')
state.history.append(cmd_action_3)
cmd_observation_3 = CmdOutputObservation(
command='ls', content='file1.txt\nfile2.txt'
)
# cmd_observation_3._cause = cmd_action_3._id
state.history.append(cmd_observation_3)
read_action_3 = FileReadAction(path='file1.txt')
state.history.append(read_action_3)
read_observation_3 = FileReadObservation(
content='File content', path='file1.txt'
)
# read_observation_3._cause = read_action_3._id
state.history.append(read_observation_3)
with patch('logging.Logger.warning') as mock_warning:
assert stuck_detector.is_stuck(headless_mode=True) is True
mock_warning.assert_called_once_with('Action, Observation pattern detected')
def test_is_stuck_not_stuck(self, stuck_detector: StuckDetector):
state = stuck_detector.state
message_action = MessageAction(content='Done', wait_for_response=False)
message_action._source = EventSource.USER
hello_action = MessageAction(content='Hello', wait_for_response=False)
state.history.append(hello_action)
hello_observation = NullObservation(content='')
# hello_observation._cause = hello_action._id
state.history.append(hello_observation)
cmd_action_1 = CmdRunAction(command='ls')
state.history.append(cmd_action_1)
cmd_observation_1 = CmdOutputObservation(
command='ls', content='file1.txt\nfile2.txt'
)
# cmd_observation_1._cause = cmd_action_1._id
state.history.append(cmd_observation_1)
read_action_1 = FileReadAction(path='file1.txt')
state.history.append(read_action_1)
read_observation_1 = FileReadObservation(
content='File content', path='file1.txt'
)
# read_observation_1._cause = read_action_1._id
state.history.append(read_observation_1)
cmd_action_2 = CmdRunAction(command='pwd')
state.history.append(cmd_action_2)
cmd_observation_2 = CmdOutputObservation(command='pwd', content='/home/user')
# cmd_observation_2._cause = cmd_action_2._id
state.history.append(cmd_observation_2)
read_action_2 = FileReadAction(path='file2.txt')
state.history.append(read_action_2)
read_observation_2 = FileReadObservation(
content='Another file content', path='file2.txt'
)
# read_observation_2._cause = read_action_2._id
state.history.append(read_observation_2)
message_null_observation = NullObservation(content='')
state.history.append(message_action)
state.history.append(message_null_observation)
cmd_action_3 = CmdRunAction(command='pwd')
state.history.append(cmd_action_3)
cmd_observation_3 = CmdOutputObservation(command='pwd', content='/home/user')
# cmd_observation_3._cause = cmd_action_3._id
state.history.append(cmd_observation_3)
read_action_3 = FileReadAction(path='file2.txt')
state.history.append(read_action_3)
read_observation_3 = FileReadObservation(
content='Another file content', path='file2.txt'
)
# read_observation_3._cause = read_action_3._id
state.history.append(read_observation_3)
assert stuck_detector.is_stuck(headless_mode=True) is False
def test_is_stuck_monologue(self, stuck_detector):
state = stuck_detector.state
# Add events to the history list directly
message_action_1 = MessageAction(content='Hi there!')
message_action_1._source = EventSource.USER
state.history.append(message_action_1)
message_action_2 = MessageAction(content='Hi there!')
message_action_2._source = EventSource.AGENT
state.history.append(message_action_2)
message_action_3 = MessageAction(content='How are you?')
message_action_3._source = EventSource.USER
state.history.append(message_action_3)
cmd_kill_action = CmdRunAction(
command='echo 42', thought="I'm not stuck, he's stuck"
)
state.history.append(cmd_kill_action)
message_action_4 = MessageAction(content="I'm doing well, thanks for asking.")
message_action_4._source = EventSource.AGENT
state.history.append(message_action_4)
message_action_5 = MessageAction(content="I'm doing well, thanks for asking.")
message_action_5._source = EventSource.AGENT
state.history.append(message_action_5)
message_action_6 = MessageAction(content="I'm doing well, thanks for asking.")
message_action_6._source = EventSource.AGENT
state.history.append(message_action_6)
assert stuck_detector.is_stuck(headless_mode=True)
# Add an observation event between the repeated message actions
cmd_output_observation = CmdOutputObservation(
content='OK, I was stuck, but no more.',
command='storybook',
exit_code=0,
)
# cmd_output_observation._cause = cmd_kill_action._id
state.history.append(cmd_output_observation)
message_action_7 = MessageAction(content="I'm doing well, thanks for asking.")
message_action_7._source = EventSource.AGENT
state.history.append(message_action_7)
message_action_8 = MessageAction(content="I'm doing well, thanks for asking.")
message_action_8._source = EventSource.AGENT
state.history.append(message_action_8)
with patch('logging.Logger.warning'):
assert not stuck_detector.is_stuck(headless_mode=True)
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()
return controller
def test_is_stuck_delegate_stuck(self, controller: AgentController):
controller.delegate = Mock()
controller.delegate._is_stuck.return_value = True
assert controller._is_stuck() is True
|