Skip to content

Commit

Permalink
fix initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
enyst committed Nov 14, 2024
1 parent d85debd commit 2fcb088
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 13 deletions.
4 changes: 2 additions & 2 deletions openhands/controller/agent_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@ def _init_history(self):
events: list[Event] = []

# If we have a truncation point, get first user message and then rest of history
if hasattr(self.state, 'truncation_id') and self.state.truncation_id:
if hasattr(self.state, 'truncation_id') and self.state.truncation_id > 0:
# Find first user message from stream
first_user_msg = next(
(
Expand Down Expand Up @@ -822,7 +822,7 @@ def _apply_conversation_window(self, events: list[Event]) -> list[Event]:

# Handle first event in truncated history
if kept_events:
i = mid_point
i = 0
while i < len(kept_events):
first_event = kept_events[i]
if isinstance(first_event, Observation) and first_event.cause:
Expand Down
45 changes: 34 additions & 11 deletions tests/unit/test_truncation.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,14 @@ def test_apply_conversation_window_basic(self, mock_event_stream, mock_agent):
cmd1 = CmdRunAction(command='ls')
cmd1._id = 2
obs1 = CmdOutputObservation(command='ls', content='file1.txt', command_id=2)
obs1._id = 3
obs1._cause = 2

cmd2 = CmdRunAction(command='pwd')
cmd2._id = 3
obs2 = CmdOutputObservation(command='pwd', content='/home', command_id=3)
obs2._cause = 3
cmd2._id = 4
obs2 = CmdOutputObservation(command='pwd', content='/home', command_id=4)
obs2._id = 5
obs2._cause = 4

events = [first_msg, cmd1, obs1, cmd2, obs2]

Expand Down Expand Up @@ -83,15 +85,36 @@ def test_context_window_exceeded_handling(self, mock_event_stream, mock_agent):
first_msg._source = EventSource.USER
first_msg._id = 1

cmd1 = CmdRunAction(command='ls')
cmd1._id = 2
obs1 = CmdOutputObservation(command='ls', content='file1.txt', command_id=2)
obs1._id = 3
obs1._cause = 2
# Add agent question
agent_msg = MessageAction(
content='What task would you like me to perform?', wait_for_response=True
)
agent_msg._source = EventSource.AGENT
agent_msg._id = 2

# Set up mock event stream to return our events
mock_event_stream.get_events.return_value = [first_msg, cmd1, obs1]
controller.state.history = [first_msg, cmd1, obs1]
# Add user response
user_response = MessageAction(
content='Please list all files and show me current directory',
wait_for_response=False,
)
user_response._source = EventSource.USER
user_response._id = 3

cmd1 = CmdRunAction(command='ls')
cmd1._id = 4
obs1 = CmdOutputObservation(command='ls', content='file1.txt', command_id=4)
obs1._id = 5
obs1._cause = 4

# Update mock event stream to include new messages
mock_event_stream.get_events.return_value = [
first_msg,
agent_msg,
user_response,
cmd1,
obs1,
]
controller.state.history = [first_msg, agent_msg, user_response, cmd1, obs1]
original_history_len = len(controller.state.history)

# Simulate ContextWindowExceededError and truncation
Expand Down

0 comments on commit 2fcb088

Please sign in to comment.