|
| 1 | +"""Tests for ToolResultUniquenessProperty. |
| 2 | +
|
| 3 | +This module tests that duplicate tool results for the same tool_call_id |
| 4 | +are properly deduplicated, preferring ObservationEvent over AgentErrorEvent. |
| 5 | +""" |
| 6 | + |
| 7 | +from unittest.mock import create_autospec |
| 8 | + |
| 9 | +from openhands.sdk.context.view.properties.tool_result_uniqueness import ( |
| 10 | + ToolResultUniquenessProperty, |
| 11 | +) |
| 12 | +from openhands.sdk.event.base import LLMConvertibleEvent |
| 13 | +from openhands.sdk.event.llm_convertible import ( |
| 14 | + ActionEvent, |
| 15 | + AgentErrorEvent, |
| 16 | + ObservationEvent, |
| 17 | + UserRejectObservation, |
| 18 | +) |
| 19 | +from tests.sdk.context.view.properties.conftest import message_event |
| 20 | + |
| 21 | + |
| 22 | +class TestToolResultUniquenessPropertyEnforcement: |
| 23 | + """Tests for the enforce method of ToolResultUniquenessProperty.""" |
| 24 | + |
| 25 | + def setup_method(self) -> None: |
| 26 | + """Set up test fixtures.""" |
| 27 | + self.property = ToolResultUniquenessProperty() |
| 28 | + |
| 29 | + def test_empty_list(self) -> None: |
| 30 | + """Test enforce with empty event list.""" |
| 31 | + result = self.property.enforce([], []) |
| 32 | + assert result == set() |
| 33 | + |
| 34 | + def test_no_duplicates(self) -> None: |
| 35 | + """Test enforce when there are no duplicate tool_call_ids.""" |
| 36 | + obs1 = create_autospec(ObservationEvent, instance=True) |
| 37 | + obs1.tool_call_id = "call_1" |
| 38 | + obs1.id = "obs_1" |
| 39 | + |
| 40 | + obs2 = create_autospec(ObservationEvent, instance=True) |
| 41 | + obs2.tool_call_id = "call_2" |
| 42 | + obs2.id = "obs_2" |
| 43 | + |
| 44 | + events: list[LLMConvertibleEvent] = [ |
| 45 | + message_event("Start"), |
| 46 | + obs1, |
| 47 | + obs2, |
| 48 | + message_event("End"), |
| 49 | + ] |
| 50 | + |
| 51 | + result = self.property.enforce(events, events) |
| 52 | + assert result == set() |
| 53 | + |
| 54 | + def test_duplicate_observation_events(self) -> None: |
| 55 | + """Test that duplicate ObservationEvents keep the later one.""" |
| 56 | + obs1 = create_autospec(ObservationEvent, instance=True) |
| 57 | + obs1.tool_call_id = "call_1" |
| 58 | + obs1.id = "obs_1" |
| 59 | + |
| 60 | + obs2 = create_autospec(ObservationEvent, instance=True) |
| 61 | + obs2.tool_call_id = "call_1" # Same tool_call_id! |
| 62 | + obs2.id = "obs_2" |
| 63 | + |
| 64 | + events: list[LLMConvertibleEvent] = [obs1, obs2] |
| 65 | + |
| 66 | + result = self.property.enforce(events, events) |
| 67 | + # obs1 should be removed, obs2 (later) should be kept |
| 68 | + assert result == {"obs_1"} |
| 69 | + |
| 70 | + def test_observation_event_preferred_over_agent_error(self) -> None: |
| 71 | + """Test that ObservationEvent is preferred over AgentErrorEvent.""" |
| 72 | + # This is the main bug scenario: AgentErrorEvent created on restart, |
| 73 | + # then ObservationEvent arrives later with actual result |
| 74 | + agent_error = AgentErrorEvent( |
| 75 | + tool_name="terminal", |
| 76 | + tool_call_id="call_1", |
| 77 | + error="Restart occurred while tool was running", |
| 78 | + ) |
| 79 | + |
| 80 | + obs = create_autospec(ObservationEvent, instance=True) |
| 81 | + obs.tool_call_id = "call_1" # Same tool_call_id! |
| 82 | + obs.id = "obs_1" |
| 83 | + |
| 84 | + events: list[LLMConvertibleEvent] = [agent_error, obs] |
| 85 | + |
| 86 | + result = self.property.enforce(events, events) |
| 87 | + # AgentErrorEvent should be removed, ObservationEvent kept |
| 88 | + assert result == {agent_error.id} |
| 89 | + |
| 90 | + def test_agent_error_before_observation_event(self) -> None: |
| 91 | + """Test AgentErrorEvent followed by ObservationEvent (restart scenario).""" |
| 92 | + # Simulates: restart creates AgentErrorEvent, then actual result arrives |
| 93 | + action = create_autospec(ActionEvent, instance=True) |
| 94 | + action.tool_call_id = "call_1" |
| 95 | + action.id = "action_1" |
| 96 | + action.llm_response_id = "response_1" |
| 97 | + |
| 98 | + agent_error = AgentErrorEvent( |
| 99 | + tool_name="terminal", |
| 100 | + tool_call_id="call_1", |
| 101 | + error="A restart occurred while this tool was in progress.", |
| 102 | + ) |
| 103 | + |
| 104 | + obs = create_autospec(ObservationEvent, instance=True) |
| 105 | + obs.tool_call_id = "call_1" |
| 106 | + obs.id = "obs_1" |
| 107 | + |
| 108 | + events: list[LLMConvertibleEvent] = [ |
| 109 | + message_event("User message"), |
| 110 | + action, |
| 111 | + agent_error, |
| 112 | + obs, # Actual result arrives later |
| 113 | + ] |
| 114 | + |
| 115 | + result = self.property.enforce(events, events) |
| 116 | + # AgentErrorEvent should be removed since we have actual ObservationEvent |
| 117 | + assert result == {agent_error.id} |
| 118 | + |
| 119 | + def test_multiple_agent_errors_keep_last(self) -> None: |
| 120 | + """Test that when only AgentErrorEvents exist, the last one is kept.""" |
| 121 | + error1 = AgentErrorEvent( |
| 122 | + tool_name="terminal", |
| 123 | + tool_call_id="call_1", |
| 124 | + error="First error", |
| 125 | + ) |
| 126 | + |
| 127 | + error2 = AgentErrorEvent( |
| 128 | + tool_name="terminal", |
| 129 | + tool_call_id="call_1", |
| 130 | + error="Second error", |
| 131 | + ) |
| 132 | + |
| 133 | + events: list[LLMConvertibleEvent] = [error1, error2] |
| 134 | + |
| 135 | + result = self.property.enforce(events, events) |
| 136 | + # First error should be removed, second (later) should be kept |
| 137 | + assert result == {error1.id} |
| 138 | + |
| 139 | + def test_user_reject_observation_handling(self) -> None: |
| 140 | + """Test that UserRejectObservation is handled correctly.""" |
| 141 | + reject = UserRejectObservation( |
| 142 | + tool_name="terminal", |
| 143 | + tool_call_id="call_1", |
| 144 | + action_id="action_1", |
| 145 | + rejection_reason="User rejected", |
| 146 | + ) |
| 147 | + |
| 148 | + obs = create_autospec(ObservationEvent, instance=True) |
| 149 | + obs.tool_call_id = "call_1" |
| 150 | + obs.id = "obs_1" |
| 151 | + |
| 152 | + events: list[LLMConvertibleEvent] = [reject, obs] |
| 153 | + |
| 154 | + result = self.property.enforce(events, events) |
| 155 | + # ObservationEvent is preferred over UserRejectObservation |
| 156 | + assert result == {reject.id} |
| 157 | + |
| 158 | + def test_mixed_scenario_multiple_tool_calls(self) -> None: |
| 159 | + """Test with multiple tool calls, some with duplicates.""" |
| 160 | + # Tool call 1: has duplicate (error + observation) |
| 161 | + error1 = AgentErrorEvent( |
| 162 | + tool_name="terminal", |
| 163 | + tool_call_id="call_1", |
| 164 | + error="Restart error", |
| 165 | + ) |
| 166 | + obs1 = create_autospec(ObservationEvent, instance=True) |
| 167 | + obs1.tool_call_id = "call_1" |
| 168 | + obs1.id = "obs_1" |
| 169 | + |
| 170 | + # Tool call 2: single observation (no duplicate) |
| 171 | + obs2 = create_autospec(ObservationEvent, instance=True) |
| 172 | + obs2.tool_call_id = "call_2" |
| 173 | + obs2.id = "obs_2" |
| 174 | + |
| 175 | + # Tool call 3: single error (no duplicate) |
| 176 | + error3 = AgentErrorEvent( |
| 177 | + tool_name="file_editor", |
| 178 | + tool_call_id="call_3", |
| 179 | + error="Tool not found", |
| 180 | + ) |
| 181 | + |
| 182 | + events: list[LLMConvertibleEvent] = [ |
| 183 | + message_event("Start"), |
| 184 | + error1, |
| 185 | + obs1, |
| 186 | + obs2, |
| 187 | + error3, |
| 188 | + ] |
| 189 | + |
| 190 | + result = self.property.enforce(events, events) |
| 191 | + # Only error1 should be removed (duplicate with obs1) |
| 192 | + assert result == {error1.id} |
| 193 | + |
| 194 | + |
| 195 | +class TestToolResultUniquenessPropertyManipulationIndices: |
| 196 | + """Tests for the manipulation_indices method.""" |
| 197 | + |
| 198 | + def setup_method(self) -> None: |
| 199 | + """Set up test fixtures.""" |
| 200 | + self.property = ToolResultUniquenessProperty() |
| 201 | + |
| 202 | + def test_complete_indices_returned(self) -> None: |
| 203 | + """Test that manipulation indices are complete (no restrictions).""" |
| 204 | + obs = create_autospec(ObservationEvent, instance=True) |
| 205 | + obs.tool_call_id = "call_1" |
| 206 | + obs.id = "obs_1" |
| 207 | + |
| 208 | + events: list[LLMConvertibleEvent] = [ |
| 209 | + message_event("Start"), |
| 210 | + obs, |
| 211 | + message_event("End"), |
| 212 | + ] |
| 213 | + |
| 214 | + result = self.property.manipulation_indices(events) |
| 215 | + # Should have indices 0, 1, 2, 3 (all positions) |
| 216 | + assert 0 in result |
| 217 | + assert 1 in result |
| 218 | + assert 2 in result |
| 219 | + assert 3 in result |
0 commit comments