forked from All-Hands-AI/OpenHands
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_bash_session.py
384 lines (319 loc) Β· 15.5 KB
/
test_bash_session.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
import os
import tempfile
from openhands.core.logger import openhands_logger as logger
from openhands.events.action import CmdRunAction
from openhands.runtime.utils.bash import BashCommandStatus, BashSession
def test_session_initialization():
# Test with custom working directory
with tempfile.TemporaryDirectory() as temp_dir:
session = BashSession(work_dir=temp_dir)
session.initialize()
obs = session.execute(CmdRunAction('pwd'))
logger.info(obs, extra={'msg_type': 'OBSERVATION'})
assert temp_dir in obs.content
assert '[The command completed with exit code 0.]' in obs.metadata.suffix
session.close()
# Test with custom username
session = BashSession(work_dir=os.getcwd(), username='nobody')
session.initialize()
assert 'openhands-nobody' in session.session.name
session.close()
def test_cwd_property(tmp_path):
session = BashSession(work_dir=tmp_path)
session.initialize()
# Change directory and verify pwd updates
random_dir = tmp_path / 'random'
random_dir.mkdir()
session.execute(CmdRunAction(f'cd {random_dir}'))
assert session.cwd == str(random_dir)
session.close()
def test_basic_command():
session = BashSession(work_dir=os.getcwd())
session.initialize()
# Test simple command
obs = session.execute(CmdRunAction("echo 'hello world'"))
logger.info(obs, extra={'msg_type': 'OBSERVATION'})
assert 'hello world' in obs.content
assert obs.metadata.suffix == '\n[The command completed with exit code 0.]'
assert obs.metadata.prefix == ''
assert obs.metadata.exit_code == 0
assert session.prev_status == BashCommandStatus.COMPLETED
# Test command with error
obs = session.execute(CmdRunAction('nonexistent_command'))
logger.info(obs, extra={'msg_type': 'OBSERVATION'})
assert obs.metadata.exit_code == 127
assert 'nonexistent_command: command not found' in obs.content
assert obs.metadata.suffix == '\n[The command completed with exit code 127.]'
assert obs.metadata.prefix == ''
assert session.prev_status == BashCommandStatus.COMPLETED
# Test multiple commands in sequence
obs = session.execute(CmdRunAction('echo "first" && echo "second" && echo "third"'))
assert 'first' in obs.content
assert 'second' in obs.content
assert 'third' in obs.content
assert obs.metadata.suffix == '\n[The command completed with exit code 0.]'
assert obs.metadata.prefix == ''
assert obs.metadata.exit_code == 0
assert session.prev_status == BashCommandStatus.COMPLETED
session.close()
def test_long_running_command_follow_by_execute():
session = BashSession(work_dir=os.getcwd(), no_change_timeout_seconds=2)
session.initialize()
# Test command that produces output slowly
obs = session.execute(
CmdRunAction('for i in {1..3}; do echo $i; sleep 3; done', blocking=False)
)
logger.info(obs, extra={'msg_type': 'OBSERVATION'})
assert '1' in obs.content # First number should appear before timeout
assert obs.metadata.exit_code == -1 # -1 indicates command is still running
assert session.prev_status == BashCommandStatus.NO_CHANGE_TIMEOUT
assert obs.metadata.suffix == (
'\n[The command has no new output after 2 seconds. '
"You may wait longer to see additional output by sending empty command '', "
'send other commands to interact with the current process, '
'or send keys to interrupt/kill the command.]'
)
assert obs.metadata.prefix == ''
# Continue watching output
obs = session.execute(CmdRunAction(''))
logger.info(obs, extra={'msg_type': 'OBSERVATION'})
assert '2' in obs.content
assert obs.metadata.prefix == '[Command output continued from previous command]\n'
assert obs.metadata.suffix == (
'\n[The command has no new output after 2 seconds. '
"You may wait longer to see additional output by sending empty command '', "
'send other commands to interact with the current process, '
'or send keys to interrupt/kill the command.]'
)
assert obs.metadata.exit_code == -1 # -1 indicates command is still running
assert session.prev_status == BashCommandStatus.NO_CHANGE_TIMEOUT
# Test command that produces no output
obs = session.execute(CmdRunAction('sleep 15'))
logger.info(obs, extra={'msg_type': 'OBSERVATION'})
assert '3' in obs.content
assert obs.metadata.prefix == '[Command output continued from previous command]\n'
assert obs.metadata.suffix == (
'\n[The command has no new output after 2 seconds. '
"You may wait longer to see additional output by sending empty command '', "
'send other commands to interact with the current process, '
'or send keys to interrupt/kill the command.]'
)
assert obs.metadata.exit_code == -1 # -1 indicates command is still running
assert session.prev_status == BashCommandStatus.NO_CHANGE_TIMEOUT
session.close()
def test_interactive_command():
session = BashSession(work_dir=os.getcwd(), no_change_timeout_seconds=3)
session.initialize()
# Test interactive command with blocking=True
obs = session.execute(
CmdRunAction(
'read -p \'Enter name: \' name && echo "Hello $name"',
)
)
logger.info(obs, extra={'msg_type': 'OBSERVATION'})
assert 'Enter name:' in obs.content
assert obs.metadata.exit_code == -1 # -1 indicates command is still running
assert session.prev_status == BashCommandStatus.NO_CHANGE_TIMEOUT
assert obs.metadata.suffix == (
'\n[The command has no new output after 3 seconds. '
"You may wait longer to see additional output by sending empty command '', "
'send other commands to interact with the current process, '
'or send keys to interrupt/kill the command.]'
)
assert obs.metadata.prefix == ''
# Send input
obs = session.execute(CmdRunAction('John'))
logger.info(obs, extra={'msg_type': 'OBSERVATION'})
assert 'Hello John' in obs.content
assert obs.metadata.exit_code == 0
assert obs.metadata.suffix == '\n[The command completed with exit code 0.]'
assert obs.metadata.prefix == ''
assert session.prev_status == BashCommandStatus.COMPLETED
# Test multiline command input
obs = session.execute(CmdRunAction('cat << EOF'))
logger.info(obs, extra={'msg_type': 'OBSERVATION'})
assert obs.metadata.exit_code == -1
assert session.prev_status == BashCommandStatus.NO_CHANGE_TIMEOUT
assert obs.metadata.suffix == (
'\n[The command has no new output after 3 seconds. '
"You may wait longer to see additional output by sending empty command '', "
'send other commands to interact with the current process, '
'or send keys to interrupt/kill the command.]'
)
assert obs.metadata.prefix == ''
obs = session.execute(CmdRunAction('line 1'))
logger.info(obs, extra={'msg_type': 'OBSERVATION'})
assert obs.metadata.exit_code == -1
assert session.prev_status == BashCommandStatus.NO_CHANGE_TIMEOUT
assert obs.metadata.suffix == (
'\n[The command has no new output after 3 seconds. '
"You may wait longer to see additional output by sending empty command '', "
'send other commands to interact with the current process, '
'or send keys to interrupt/kill the command.]'
)
assert obs.metadata.prefix == '[Command output continued from previous command]\n'
obs = session.execute(CmdRunAction('line 2'))
logger.info(obs, extra={'msg_type': 'OBSERVATION'})
assert obs.metadata.exit_code == -1
assert session.prev_status == BashCommandStatus.NO_CHANGE_TIMEOUT
assert obs.metadata.suffix == (
'\n[The command has no new output after 3 seconds. '
"You may wait longer to see additional output by sending empty command '', "
'send other commands to interact with the current process, '
'or send keys to interrupt/kill the command.]'
)
assert obs.metadata.prefix == '[Command output continued from previous command]\n'
obs = session.execute(CmdRunAction('EOF'))
logger.info(obs, extra={'msg_type': 'OBSERVATION'})
assert 'line 1' in obs.content and 'line 2' in obs.content
assert obs.metadata.exit_code == 0
assert obs.metadata.suffix == '\n[The command completed with exit code 0.]'
assert obs.metadata.prefix == ''
session.close()
def test_ctrl_c():
session = BashSession(work_dir=os.getcwd(), no_change_timeout_seconds=2)
session.initialize()
# Start infinite loop
obs = session.execute(
CmdRunAction("while true; do echo 'looping'; sleep 3; done"),
)
logger.info(obs, extra={'msg_type': 'OBSERVATION'})
assert 'looping' in obs.content
assert obs.metadata.suffix == (
'\n[The command has no new output after 2 seconds. '
"You may wait longer to see additional output by sending empty command '', "
'send other commands to interact with the current process, '
'or send keys to interrupt/kill the command.]'
)
assert obs.metadata.prefix == ''
assert obs.metadata.exit_code == -1 # -1 indicates command is still running
assert session.prev_status == BashCommandStatus.NO_CHANGE_TIMEOUT
# Send Ctrl+C
obs = session.execute(CmdRunAction('C-c'))
logger.info(obs, extra={'msg_type': 'OBSERVATION'})
assert obs.metadata.exit_code == 130 # Standard exit code for Ctrl+C
assert (
obs.metadata.suffix
== '\n[The command completed with exit code 130. CTRL+C was sent.]'
)
assert obs.metadata.prefix == ''
assert session.prev_status == BashCommandStatus.COMPLETED
session.close()
def test_empty_command_errors():
session = BashSession(work_dir=os.getcwd())
session.initialize()
# Test empty command without previous command
obs = session.execute(CmdRunAction(''))
logger.info(obs, extra={'msg_type': 'OBSERVATION'})
assert (
obs.content
== 'ERROR: No previous command to continue from. Previous command has to be timeout to be continued.'
)
assert obs.metadata.exit_code == -1
assert obs.metadata.prefix == ''
assert obs.metadata.suffix == ''
assert session.prev_status is None
session.close()
def test_command_output_continuation():
session = BashSession(work_dir=os.getcwd(), no_change_timeout_seconds=2)
session.initialize()
# Start a command that produces output slowly
obs = session.execute(CmdRunAction('for i in {1..5}; do echo $i; sleep 3; done'))
logger.info(obs, extra={'msg_type': 'OBSERVATION'})
assert obs.content.strip() == '1'
assert obs.metadata.prefix == ''
assert '[The command has no new output after 2 seconds.' in obs.metadata.suffix
assert session.prev_status == BashCommandStatus.NO_CHANGE_TIMEOUT
obs = session.execute(CmdRunAction(''))
logger.info(obs, extra={'msg_type': 'OBSERVATION'})
assert '[Command output continued from previous command]' in obs.metadata.prefix
assert obs.content.strip() == '2'
assert '[The command has no new output after 2 seconds.' in obs.metadata.suffix
assert session.prev_status == BashCommandStatus.NO_CHANGE_TIMEOUT
obs = session.execute(CmdRunAction(''))
logger.info(obs, extra={'msg_type': 'OBSERVATION'})
assert '[Command output continued from previous command]' in obs.metadata.prefix
assert obs.content.strip() == '3'
assert '[The command has no new output after 2 seconds.' in obs.metadata.suffix
assert session.prev_status == BashCommandStatus.NO_CHANGE_TIMEOUT
obs = session.execute(CmdRunAction(''))
logger.info(obs, extra={'msg_type': 'OBSERVATION'})
assert '[Command output continued from previous command]' in obs.metadata.prefix
assert obs.content.strip() == '4'
assert '[The command has no new output after 2 seconds.' in obs.metadata.suffix
assert session.prev_status == BashCommandStatus.NO_CHANGE_TIMEOUT
obs = session.execute(CmdRunAction(''))
logger.info(obs, extra={'msg_type': 'OBSERVATION'})
assert '[Command output continued from previous command]' in obs.metadata.prefix
assert obs.content.strip() == '5'
assert '[The command has no new output after 2 seconds.' in obs.metadata.suffix
assert session.prev_status == BashCommandStatus.NO_CHANGE_TIMEOUT
obs = session.execute(CmdRunAction(''))
logger.info(obs, extra={'msg_type': 'OBSERVATION'})
assert '[The command completed with exit code 0.]' in obs.metadata.suffix
assert session.prev_status == BashCommandStatus.COMPLETED
session.close()
def test_long_output():
session = BashSession(work_dir=os.getcwd())
session.initialize()
# Generate a long output that may exceed buffer size
obs = session.execute(CmdRunAction('for i in {1..5000}; do echo "Line $i"; done'))
logger.info(obs, extra={'msg_type': 'OBSERVATION'})
assert 'Line 1' in obs.content
assert 'Line 5000' in obs.content
assert obs.metadata.exit_code == 0
assert obs.metadata.prefix == ''
assert obs.metadata.suffix == '\n[The command completed with exit code 0.]'
session.close()
def test_long_output_exceed_history_limit():
session = BashSession(work_dir=os.getcwd())
session.initialize()
# Generate a long output that may exceed buffer size
obs = session.execute(CmdRunAction('for i in {1..50000}; do echo "Line $i"; done'))
logger.info(obs, extra={'msg_type': 'OBSERVATION'})
assert 'Previous command outputs are truncated' in obs.metadata.prefix
assert 'Line 40000' in obs.content
assert 'Line 50000' in obs.content
assert obs.metadata.exit_code == 0
assert obs.metadata.suffix == '\n[The command completed with exit code 0.]'
session.close()
def test_multiline_command():
session = BashSession(work_dir=os.getcwd())
session.initialize()
# Test multiline command with PS2 prompt disabled
obs = session.execute(
CmdRunAction("""if true; then
echo "inside if"
fi""")
)
logger.info(obs, extra={'msg_type': 'OBSERVATION'})
assert 'inside if' in obs.content
assert obs.metadata.exit_code == 0
assert obs.metadata.prefix == ''
assert obs.metadata.suffix == '\n[The command completed with exit code 0.]'
session.close()
def test_python_interactive_input():
session = BashSession(work_dir=os.getcwd(), no_change_timeout_seconds=2)
session.initialize()
# Test Python program that asks for input - properly escaped for bash
python_script = """name = input('Enter your name: '); age = input('Enter your age: '); print(f'Hello {name}, you are {age} years old')"""
# Start Python with the interactive script
obs = session.execute(CmdRunAction(f'python3 -c "{python_script}"'))
logger.info(obs, extra={'msg_type': 'OBSERVATION'})
assert 'Enter your name:' in obs.content
assert obs.metadata.exit_code == -1 # -1 indicates command is still running
assert session.prev_status == BashCommandStatus.NO_CHANGE_TIMEOUT
# Send first input (name)
obs = session.execute(CmdRunAction('Alice'))
logger.info(obs, extra={'msg_type': 'OBSERVATION'})
assert 'Enter your age:' in obs.content
assert obs.metadata.exit_code == -1
assert session.prev_status == BashCommandStatus.NO_CHANGE_TIMEOUT
# Send second input (age)
obs = session.execute(CmdRunAction('25'))
logger.info(obs, extra={'msg_type': 'OBSERVATION'})
assert 'Hello Alice, you are 25 years old' in obs.content
assert obs.metadata.exit_code == 0
assert obs.metadata.suffix == '\n[The command completed with exit code 0.]'
assert session.prev_status == BashCommandStatus.COMPLETED
session.close()