Skip to content

Commit

Permalink
fix duplicate state
Browse files Browse the repository at this point in the history
  • Loading branch information
enyst committed Jan 6, 2025
1 parent 50a0b1d commit 1b07220
Showing 1 changed file with 12 additions and 14 deletions.
26 changes: 12 additions & 14 deletions openhands/server/session/agent_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,27 +268,25 @@ def _create_controller(
confirmation_mode=confirmation_mode,
headless_mode=False,
status_callback=self._status_callback,
initial_state=self._maybe_restore_state(),
)

# Note: We now attempt to restore the state from session here,
# but if it fails, we fall back to None and still initialize the controller
# with a fresh state. That way, the controller will always load events from the event stream
# even if the state file was corrupt.
return controller

def _maybe_restore_state(self) -> State | None:
"""Helper method to handle state restore logic."""
restored_state = None

# Attempt to restore the state from session.
# Use a heuristic to figure out if we should have a state:
# if we have events in the stream.
try:
restored_state = State.restore_from_session(self.sid, self.file_store)
logger.debug(f'Restored state from session, sid: {self.sid}')
except Exception as e:
if self.event_stream.get_latest_event_id() > 0:
# if we have events, we should have a state
logger.warning(f'State could not be restored: {e}')

# Set the initial state through the controller.
controller.set_initial_state(restored_state, max_iterations, confirmation_mode)
if restored_state:
logger.debug(f'Restored agent state from session, sid: {self.sid}')
else:
logger.debug('New session state created.')

logger.debug('Agent controller initialized.')
return controller
else:
logger.debug('No events found, no state to restore')
return restored_state

0 comments on commit 1b07220

Please sign in to comment.