forked from All-Hands-AI/OpenHands
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_observation_serialization.py
235 lines (212 loc) · 7.87 KB
/
test_observation_serialization.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
from openhands.events.action.files import FileEditSource
from openhands.events.observation import (
CmdOutputMetadata,
CmdOutputObservation,
FileEditObservation,
Observation,
)
from openhands.events.serialization import (
event_from_dict,
event_to_dict,
event_to_memory,
event_to_trajectory,
)
def serialization_deserialization(
original_observation_dict, cls, max_message_chars: int = 10000
):
observation_instance = event_from_dict(original_observation_dict)
assert isinstance(
observation_instance, Observation
), 'The observation instance should be an instance of Action.'
assert isinstance(
observation_instance, cls
), 'The observation instance should be an instance of CmdOutputObservation.'
serialized_observation_dict = event_to_dict(observation_instance)
serialized_observation_trajectory = event_to_trajectory(observation_instance)
serialized_observation_memory = event_to_memory(
observation_instance, max_message_chars
)
assert (
serialized_observation_dict == original_observation_dict
), 'The serialized observation should match the original observation dict.'
assert (
serialized_observation_trajectory == original_observation_dict
), 'The serialized observation trajectory should match the original observation dict.'
original_observation_dict.pop('message', None)
original_observation_dict.pop('id', None)
original_observation_dict.pop('timestamp', None)
assert (
serialized_observation_memory == original_observation_dict
), 'The serialized observation memory should match the original observation dict.'
# Additional tests for various observation subclasses can be included here
def test_observation_event_props_serialization_deserialization():
original_observation_dict = {
'id': 42,
'source': 'agent',
'timestamp': '2021-08-01T12:00:00',
'observation': 'run',
'message': 'Command `ls -l` executed with exit code 0.',
'extras': {
'command': 'ls -l',
'hidden': False,
'metadata': {
'exit_code': 0,
'hostname': None,
'pid': -1,
'prefix': '',
'py_interpreter_path': None,
'suffix': '',
'username': None,
'working_dir': None,
},
},
'content': 'foo.txt',
'success': True,
}
serialization_deserialization(original_observation_dict, CmdOutputObservation)
def test_command_output_observation_serialization_deserialization():
original_observation_dict = {
'observation': 'run',
'extras': {
'command': 'ls -l',
'hidden': False,
'metadata': {
'exit_code': 0,
'hostname': None,
'pid': -1,
'prefix': '',
'py_interpreter_path': None,
'suffix': '',
'username': None,
'working_dir': None,
},
},
'message': 'Command `ls -l` executed with exit code 0.',
'content': 'foo.txt',
'success': True,
}
serialization_deserialization(original_observation_dict, CmdOutputObservation)
def test_success_field_serialization():
# Test success=True
obs = CmdOutputObservation(
content='Command succeeded',
command='ls -l',
metadata=CmdOutputMetadata(
exit_code=0,
),
)
serialized = event_to_dict(obs)
assert serialized['success'] is True
# Test success=False
obs = CmdOutputObservation(
content='No such file or directory',
command='ls -l',
metadata=CmdOutputMetadata(
exit_code=1,
),
)
serialized = event_to_dict(obs)
assert serialized['success'] is False
def test_legacy_serialization():
original_observation_dict = {
'id': 42,
'source': 'agent',
'timestamp': '2021-08-01T12:00:00',
'observation': 'run',
'message': 'Command `ls -l` executed with exit code 0.',
'extras': {
'command': 'ls -l',
'hidden': False,
'exit_code': 0,
'command_id': 3,
},
'content': 'foo.txt',
'success': True,
}
event = event_from_dict(original_observation_dict)
assert isinstance(event, Observation)
assert isinstance(event, CmdOutputObservation)
assert event.metadata.exit_code == 0
assert event.success is True
assert event.command == 'ls -l'
assert event.hidden is False
event_dict = event_to_dict(event)
assert event_dict['success'] is True
assert event_dict['extras']['metadata']['exit_code'] == 0
assert event_dict['extras']['metadata']['pid'] == 3
assert event_dict['extras']['command'] == 'ls -l'
assert event_dict['extras']['hidden'] is False
def test_file_edit_observation_serialization():
original_observation_dict = {
'observation': 'edit',
'extras': {
'_diff_cache': None,
'impl_source': FileEditSource.LLM_BASED_EDIT,
'new_content': None,
'old_content': None,
'path': '',
'prev_exist': False,
},
'message': 'I edited the file .',
'content': '[Existing file /path/to/file.txt is edited with 1 changes.]',
}
serialization_deserialization(original_observation_dict, FileEditObservation)
def test_file_edit_observation_new_file_serialization():
original_observation_dict = {
'observation': 'edit',
'content': '[New file /path/to/newfile.txt is created with the provided content.]',
'extras': {
'_diff_cache': None,
'impl_source': FileEditSource.LLM_BASED_EDIT,
'new_content': None,
'old_content': None,
'path': '',
'prev_exist': False,
},
'message': 'I edited the file .',
}
serialization_deserialization(original_observation_dict, FileEditObservation)
def test_file_edit_observation_oh_aci_serialization():
original_observation_dict = {
'observation': 'edit',
'content': 'The file /path/to/file.txt is edited with the provided content.',
'extras': {
'_diff_cache': None,
'impl_source': FileEditSource.LLM_BASED_EDIT,
'new_content': None,
'old_content': None,
'path': '',
'prev_exist': False,
},
'message': 'I edited the file .',
}
serialization_deserialization(original_observation_dict, FileEditObservation)
def test_file_edit_observation_legacy_serialization():
original_observation_dict = {
'observation': 'edit',
'content': 'content',
'extras': {
'path': '/workspace/game_2048.py',
'prev_exist': False,
'old_content': None,
'new_content': 'new content',
'impl_source': 'oh_aci',
'formatted_output_and_error': 'File created successfully at: /workspace/game_2048.py',
},
}
event = event_from_dict(original_observation_dict)
assert isinstance(event, Observation)
assert isinstance(event, FileEditObservation)
assert event.impl_source == FileEditSource.OH_ACI
assert event.path == '/workspace/game_2048.py'
assert event.prev_exist is False
assert event.old_content is None
assert event.new_content == 'new content'
assert not hasattr(event, 'formatted_output_and_error')
event_dict = event_to_dict(event)
assert event_dict['extras']['impl_source'] == 'oh_aci'
assert event_dict['extras']['path'] == '/workspace/game_2048.py'
assert event_dict['extras']['prev_exist'] is False
assert event_dict['extras']['old_content'] is None
assert event_dict['extras']['new_content'] == 'new content'
assert 'formatted_output_and_error' not in event_dict['extras']