diff --git a/openhands/controller/agent_controller.py b/openhands/controller/agent_controller.py index b011a99ed4db..ad80cdcb385d 100644 --- a/openhands/controller/agent_controller.py +++ b/openhands/controller/agent_controller.py @@ -816,8 +816,8 @@ def _apply_conversation_window(self, events: list[Event]) -> list[Event]: None, ) - # Initial cut in half - mid_point = len(events) // 2 + # cut in half + mid_point = max(1, len(events) // 2) kept_events = events[mid_point:] # Handle first event in truncated history diff --git a/tests/unit/test_truncation.py b/tests/unit/test_truncation.py index fef0e4495193..14cb1ca0245c 100644 --- a/tests/unit/test_truncation.py +++ b/tests/unit/test_truncation.py @@ -86,6 +86,7 @@ def test_context_window_exceeded_handling(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 # Set up mock event stream to return our events