stuck.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. from openhands.controller.state.state import State
  2. from openhands.core.logger import openhands_logger as logger
  3. from openhands.events.action.action import Action
  4. from openhands.events.action.commands import IPythonRunCellAction
  5. from openhands.events.action.empty import NullAction
  6. from openhands.events.action.message import MessageAction
  7. from openhands.events.event import Event, EventSource
  8. from openhands.events.observation.commands import (
  9. CmdOutputObservation,
  10. IPythonRunCellObservation,
  11. )
  12. from openhands.events.observation.empty import NullObservation
  13. from openhands.events.observation.error import ErrorObservation
  14. from openhands.events.observation.observation import Observation
  15. class StuckDetector:
  16. SYNTAX_ERROR_MESSAGES = [
  17. 'SyntaxError: unterminated string literal (detected at line',
  18. 'SyntaxError: invalid syntax. Perhaps you forgot a comma?',
  19. 'SyntaxError: incomplete input',
  20. ]
  21. def __init__(self, state: State):
  22. self.state = state
  23. def is_stuck(self):
  24. # filter out MessageAction with source='user' from history
  25. filtered_history = [
  26. event
  27. for event in self.state.history
  28. if not (
  29. (isinstance(event, MessageAction) and event.source == EventSource.USER)
  30. or
  31. # there might be some NullAction or NullObservation in the history at least for now
  32. isinstance(event, (NullAction, NullObservation))
  33. )
  34. ]
  35. # it takes 3 actions minimum to detect a loop, otherwise nothing to do here
  36. if len(filtered_history) < 3:
  37. return False
  38. # the first few scenarios detect 3 or 4 repeated steps
  39. # prepare the last 4 actions and observations, to check them out
  40. last_actions: list[Event] = []
  41. last_observations: list[Event] = []
  42. # retrieve the last four actions and observations starting from the end of history, wherever they are
  43. for event in reversed(filtered_history):
  44. if isinstance(event, Action) and len(last_actions) < 4:
  45. last_actions.append(event)
  46. elif isinstance(event, Observation) and len(last_observations) < 4:
  47. last_observations.append(event)
  48. if len(last_actions) == 4 and len(last_observations) == 4:
  49. break
  50. # scenario 1: same action, same observation
  51. if self._is_stuck_repeating_action_observation(last_actions, last_observations):
  52. return True
  53. # scenario 2: same action, errors
  54. if self._is_stuck_repeating_action_error(last_actions, last_observations):
  55. return True
  56. # scenario 3: monologue
  57. if self._is_stuck_monologue(filtered_history):
  58. return True
  59. # scenario 4: action, observation pattern on the last six steps
  60. if len(filtered_history) < 6:
  61. return False
  62. if self._is_stuck_action_observation_pattern(filtered_history):
  63. return True
  64. return False
  65. def _is_stuck_repeating_action_observation(self, last_actions, last_observations):
  66. # scenario 1: same action, same observation
  67. # it takes 4 actions and 4 observations to detect a loop
  68. # assert len(last_actions) == 4 and len(last_observations) == 4
  69. # reset almost_stuck reminder
  70. self.state.almost_stuck = 0
  71. # almost stuck? if two actions, obs are the same, we're almost stuck
  72. if len(last_actions) >= 2 and len(last_observations) >= 2:
  73. actions_equal = all(
  74. self._eq_no_pid(last_actions[0], action) for action in last_actions[:2]
  75. )
  76. observations_equal = all(
  77. self._eq_no_pid(last_observations[0], observation)
  78. for observation in last_observations[:2]
  79. )
  80. # the last two actions and obs are the same?
  81. if actions_equal and observations_equal:
  82. self.state.almost_stuck = 2
  83. # the last three actions and observations are the same?
  84. if len(last_actions) >= 3 and len(last_observations) >= 3:
  85. if (
  86. actions_equal
  87. and observations_equal
  88. and self._eq_no_pid(last_actions[0], last_actions[2])
  89. and self._eq_no_pid(last_observations[0], last_observations[2])
  90. ):
  91. self.state.almost_stuck = 1
  92. if len(last_actions) == 4 and len(last_observations) == 4:
  93. if (
  94. actions_equal
  95. and observations_equal
  96. and self._eq_no_pid(last_actions[0], last_actions[3])
  97. and self._eq_no_pid(last_observations[0], last_observations[3])
  98. ):
  99. logger.warning('Action, Observation loop detected')
  100. self.state.almost_stuck = 0
  101. return True
  102. return False
  103. def _is_stuck_repeating_action_error(self, last_actions, last_observations):
  104. # scenario 2: same action, errors
  105. # it takes 3 actions and 3 observations to detect a loop
  106. # check if the last three actions are the same and result in errors
  107. if len(last_actions) < 4 or len(last_observations) < 4:
  108. return False
  109. # are the last three actions the "same"?
  110. if all(self._eq_no_pid(last_actions[0], action) for action in last_actions[:3]):
  111. # and the last three observations are all errors?
  112. if all(isinstance(obs, ErrorObservation) for obs in last_observations[:3]):
  113. logger.warning('Action, ErrorObservation loop detected')
  114. return True
  115. # or, are the last three observations all IPythonRunCellObservation with SyntaxError?
  116. elif all(
  117. isinstance(obs, IPythonRunCellObservation)
  118. for obs in last_observations[:3]
  119. ):
  120. warning = 'Action, IPythonRunCellObservation loop detected'
  121. for error_message in self.SYNTAX_ERROR_MESSAGES:
  122. if error_message.startswith(
  123. 'SyntaxError: unterminated string literal (detected at line'
  124. ):
  125. if self._check_for_consistent_line_error(
  126. last_observations[:3], error_message
  127. ):
  128. logger.warning(warning)
  129. return True
  130. elif error_message in (
  131. 'SyntaxError: invalid syntax. Perhaps you forgot a comma?',
  132. 'SyntaxError: incomplete input',
  133. ) and self._check_for_consistent_invalid_syntax(
  134. last_observations[:3], error_message
  135. ):
  136. logger.warning(warning)
  137. return True
  138. return False
  139. def _check_for_consistent_invalid_syntax(self, observations, error_message):
  140. first_lines = []
  141. valid_observations = []
  142. for obs in observations:
  143. content = obs.content
  144. lines = content.strip().split('\n')
  145. if len(lines) < 6: # 6 because a real syntax error has at least 6 lines
  146. return False
  147. line1 = lines[0].strip()
  148. if not line1.startswith('Cell In[1], line'):
  149. return False
  150. first_lines.append(line1) # Store the first line of each observation
  151. # Check last three lines
  152. if (
  153. lines[-1].startswith('[Jupyter Python interpreter:')
  154. and lines[-2].startswith('[Jupyter current working directory:')
  155. and error_message in lines[-3]
  156. ):
  157. valid_observations.append(obs)
  158. # Check if:
  159. # 1. All first lines are identical
  160. # 2. We have exactly 3 valid observations
  161. # 3. The error message line is identical in all valid observations
  162. return (
  163. len(set(first_lines)) == 1
  164. and len(valid_observations) == 3
  165. and len(
  166. set(
  167. obs.content.strip().split('\n')[:-2][-1]
  168. for obs in valid_observations
  169. )
  170. )
  171. == 1
  172. )
  173. def _check_for_consistent_line_error(self, observations, error_message):
  174. error_lines = []
  175. for obs in observations:
  176. content = obs.content
  177. lines = content.strip().split('\n')
  178. if len(lines) < 3:
  179. return False
  180. last_lines = lines[-3:]
  181. # Check if the last two lines are our own
  182. if not (
  183. last_lines[-2].startswith('[Jupyter current working directory:')
  184. and last_lines[-1].startswith('[Jupyter Python interpreter:')
  185. ):
  186. return False
  187. # Check for the error message in the 3rd-to-last line
  188. if error_message in last_lines[-3]:
  189. error_lines.append(last_lines[-3])
  190. # Check if we found the error message in all 3 observations
  191. # and the 3rd-to-last line is identical across all occurrences
  192. return len(error_lines) == 3 and len(set(error_lines)) == 1
  193. def _is_stuck_monologue(self, filtered_history):
  194. # scenario 3: monologue
  195. # check for repeated MessageActions with source=AGENT
  196. # see if the agent is engaged in a good old monologue, telling itself the same thing over and over
  197. agent_message_actions = [
  198. (i, event)
  199. for i, event in enumerate(filtered_history)
  200. if isinstance(event, MessageAction) and event.source == EventSource.AGENT
  201. ]
  202. # last three message actions will do for this check
  203. if len(agent_message_actions) >= 3:
  204. last_agent_message_actions = agent_message_actions[-3:]
  205. if all(
  206. (last_agent_message_actions[0][1] == action[1])
  207. for action in last_agent_message_actions
  208. ):
  209. # check if there are any observations between the repeated MessageActions
  210. # then it's not yet a loop, maybe it can recover
  211. start_index = last_agent_message_actions[0][0]
  212. end_index = last_agent_message_actions[-1][0]
  213. has_observation_between = False
  214. for event in filtered_history[start_index + 1 : end_index]:
  215. if isinstance(event, Observation):
  216. has_observation_between = True
  217. break
  218. if not has_observation_between:
  219. logger.warning('Repeated MessageAction with source=AGENT detected')
  220. return True
  221. return False
  222. def _is_stuck_action_observation_pattern(self, filtered_history):
  223. # scenario 4: action, observation pattern on the last six steps
  224. # check if the agent repeats the same (Action, Observation)
  225. # every other step in the last six steps
  226. last_six_actions: list[Event] = []
  227. last_six_observations: list[Event] = []
  228. # the end of history is most interesting
  229. for event in reversed(filtered_history):
  230. if isinstance(event, Action) and len(last_six_actions) < 6:
  231. last_six_actions.append(event)
  232. elif isinstance(event, Observation) and len(last_six_observations) < 6:
  233. last_six_observations.append(event)
  234. if len(last_six_actions) == 6 and len(last_six_observations) == 6:
  235. break
  236. # this pattern is every other step, like:
  237. # (action_1, obs_1), (action_2, obs_2), (action_1, obs_1), (action_2, obs_2),...
  238. if len(last_six_actions) == 6 and len(last_six_observations) == 6:
  239. actions_equal = (
  240. # action_0 == action_2 == action_4
  241. self._eq_no_pid(last_six_actions[0], last_six_actions[2])
  242. and self._eq_no_pid(last_six_actions[0], last_six_actions[4])
  243. # action_1 == action_3 == action_5
  244. and self._eq_no_pid(last_six_actions[1], last_six_actions[3])
  245. and self._eq_no_pid(last_six_actions[1], last_six_actions[5])
  246. )
  247. observations_equal = (
  248. # obs_0 == obs_2 == obs_4
  249. self._eq_no_pid(last_six_observations[0], last_six_observations[2])
  250. and self._eq_no_pid(last_six_observations[0], last_six_observations[4])
  251. # obs_1 == obs_3 == obs_5
  252. and self._eq_no_pid(last_six_observations[1], last_six_observations[3])
  253. and self._eq_no_pid(last_six_observations[1], last_six_observations[5])
  254. )
  255. if actions_equal and observations_equal:
  256. logger.warning('Action, Observation pattern detected')
  257. return True
  258. return False
  259. def _eq_no_pid(self, obj1, obj2):
  260. if isinstance(obj1, IPythonRunCellAction) and isinstance(
  261. obj2, IPythonRunCellAction
  262. ):
  263. # for loop detection on edit actions, ignore the thought, compare some code
  264. # the code should have at least 3 lines, to avoid simple one-liners
  265. if (
  266. 'edit_file_by_replace(' in obj1.code
  267. and 'edit_file_by_replace(' in obj2.code
  268. ):
  269. return (
  270. len(obj1.code.split('\n')) > 2
  271. and obj1.code.split('\n')[:3] == obj2.code.split('\n')[:3]
  272. )
  273. else:
  274. # default comparison
  275. return obj1 == obj2
  276. elif isinstance(obj1, CmdOutputObservation) and isinstance(
  277. obj2, CmdOutputObservation
  278. ):
  279. # for loop detection, ignore command_id, which is the pid
  280. return obj1.command == obj2.command and obj1.exit_code == obj2.exit_code
  281. else:
  282. # this is the default comparison
  283. return obj1 == obj2