forked from OpenHands/OpenHands
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_config.py
More file actions
579 lines (469 loc) · 20.4 KB
/
test_config.py
File metadata and controls
579 lines (469 loc) · 20.4 KB
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
import os
import pytest
from opendevin.core.config import (
AgentConfig,
AppConfig,
LLMConfig,
UndefinedString,
finalize_config,
get_llm_config_arg,
load_from_env,
load_from_toml,
)
@pytest.fixture
def setup_env():
# Create old-style and new-style TOML files
with open('old_style_config.toml', 'w') as f:
f.write('[default]\nLLM_MODEL="GPT-4"\n')
with open('new_style_config.toml', 'w') as f:
f.write('[app]\nLLM_MODEL="GPT-3"\n')
yield
# Cleanup TOML files after the test
os.remove('old_style_config.toml')
os.remove('new_style_config.toml')
@pytest.fixture
def temp_toml_file(tmp_path):
# Fixture to create a temporary directory and TOML file for testing
tmp_toml_file = os.path.join(tmp_path, 'config.toml')
yield tmp_toml_file
@pytest.fixture
def default_config(monkeypatch):
# Fixture to provide a default AppConfig instance
AppConfig.reset()
yield AppConfig()
def test_compat_env_to_config(monkeypatch, setup_env):
# Use `monkeypatch` to set environment variables for this specific test
monkeypatch.setenv('WORKSPACE_BASE', '/repos/opendevin/workspace')
monkeypatch.setenv('LLM_API_KEY', 'sk-proj-rgMV0...')
monkeypatch.setenv('LLM_MODEL', 'gpt-4o')
monkeypatch.setenv('AGENT_MEMORY_MAX_THREADS', '4')
monkeypatch.setenv('AGENT_MEMORY_ENABLED', 'True')
monkeypatch.setenv('DEFAULT_AGENT', 'CodeActAgent')
monkeypatch.setenv('SANDBOX_TYPE', 'local')
monkeypatch.setenv('SANDBOX_TIMEOUT', '10')
config = AppConfig()
load_from_env(config, os.environ)
assert config.workspace_base == '/repos/opendevin/workspace'
assert isinstance(config.get_llm_config(), LLMConfig)
assert config.get_llm_config().api_key == 'sk-proj-rgMV0...'
assert config.get_llm_config().model == 'gpt-4o'
assert isinstance(config.get_agent_config(), AgentConfig)
assert isinstance(config.get_agent_config().memory_max_threads, int)
assert config.get_agent_config().memory_max_threads == 4
assert config.get_agent_config().memory_enabled is True
assert config.default_agent == 'CodeActAgent'
assert config.sandbox.box_type == 'local'
assert config.sandbox.timeout == 10
def test_load_from_old_style_env(monkeypatch, default_config):
# Test loading configuration from old-style environment variables using monkeypatch
monkeypatch.setenv('LLM_API_KEY', 'test-api-key')
monkeypatch.setenv('AGENT_MEMORY_ENABLED', 'True')
monkeypatch.setenv('DEFAULT_AGENT', 'PlannerAgent')
monkeypatch.setenv('WORKSPACE_BASE', '/opt/files/workspace')
monkeypatch.setenv('SANDBOX_CONTAINER_IMAGE', 'custom_image')
load_from_env(default_config, os.environ)
assert default_config.get_llm_config().api_key == 'test-api-key'
assert default_config.get_agent_config().memory_enabled is True
assert default_config.default_agent == 'PlannerAgent'
assert default_config.workspace_base == '/opt/files/workspace'
assert (
default_config.workspace_mount_path is UndefinedString.UNDEFINED
) # before finalize_config
assert (
default_config.workspace_mount_path_in_sandbox is not UndefinedString.UNDEFINED
)
assert default_config.sandbox.container_image == 'custom_image'
def test_load_from_new_style_toml(default_config, temp_toml_file):
# Test loading configuration from a new-style TOML file
with open(temp_toml_file, 'w', encoding='utf-8') as toml_file:
toml_file.write(
"""
[llm]
model = "test-model"
api_key = "toml-api-key"
[llm.cheap]
model = "some-cheap-model"
api_key = "cheap-model-api-key"
[agent]
memory_enabled = true
[agent.BrowsingAgent]
llm_config = "cheap"
memory_enabled = false
[sandbox]
timeout = 1
[core]
workspace_base = "/opt/files2/workspace"
default_agent = "TestAgent"
sandbox_type = "local"
"""
)
load_from_toml(default_config, temp_toml_file)
# default llm & agent configs
assert default_config.default_agent == 'TestAgent'
assert default_config.get_llm_config().model == 'test-model'
assert default_config.get_llm_config().api_key == 'toml-api-key'
assert default_config.get_agent_config().memory_enabled is True
# undefined agent config inherits default ones
assert (
default_config.get_llm_config_from_agent('CodeActAgent')
== default_config.get_llm_config()
)
assert default_config.get_agent_config('CodeActAgent').memory_enabled is True
# defined agent config overrides default ones
assert default_config.get_llm_config_from_agent(
'BrowsingAgent'
) == default_config.get_llm_config('cheap')
assert (
default_config.get_llm_config_from_agent('BrowsingAgent').model
== 'some-cheap-model'
)
assert default_config.get_agent_config('BrowsingAgent').memory_enabled is False
assert default_config.workspace_base == '/opt/files2/workspace'
assert default_config.sandbox.box_type == 'local'
assert default_config.sandbox.timeout == 1
# default config doesn't have a field sandbox_type
assert not hasattr(default_config, 'sandbox_type')
# before finalize_config, workspace_mount_path is UndefinedString.UNDEFINED if it was not set
assert default_config.workspace_mount_path is UndefinedString.UNDEFINED
assert (
default_config.workspace_mount_path_in_sandbox is not UndefinedString.UNDEFINED
)
assert default_config.workspace_mount_path_in_sandbox == '/workspace'
finalize_config(default_config)
# after finalize_config, workspace_mount_path is set to the absolute path of workspace_base
# if it was undefined
assert default_config.workspace_mount_path == '/opt/files2/workspace'
def test_compat_load_sandbox_from_toml(default_config, temp_toml_file):
# test loading configuration from a new-style TOML file
# uses a toml file with sandbox_vars instead of a sandbox section
with open(temp_toml_file, 'w', encoding='utf-8') as toml_file:
toml_file.write(
"""
[llm]
model = "test-model"
[agent]
memory_enabled = true
[core]
workspace_base = "/opt/files2/workspace"
sandbox_type = "local"
sandbox_timeout = 500
sandbox_container_image = "node:14"
sandbox_user_id = 1001
default_agent = "TestAgent"
"""
)
load_from_toml(default_config, temp_toml_file)
assert default_config.get_llm_config().model == 'test-model'
assert default_config.get_llm_config_from_agent().model == 'test-model'
assert default_config.default_agent == 'TestAgent'
assert default_config.get_agent_config().memory_enabled is True
assert default_config.workspace_base == '/opt/files2/workspace'
assert default_config.sandbox.box_type == 'local'
assert default_config.sandbox.timeout == 500
assert default_config.sandbox.container_image == 'node:14'
assert default_config.sandbox.user_id == 1001
assert default_config.workspace_mount_path_in_sandbox == '/workspace'
finalize_config(default_config)
# app config doesn't have fields sandbox_*
assert not hasattr(default_config, 'sandbox_type')
assert not hasattr(default_config, 'sandbox_timeout')
assert not hasattr(default_config, 'sandbox_container_image')
assert not hasattr(default_config, 'sandbox_user_id')
# after finalize_config, workspace_mount_path is set to the absolute path of workspace_base
# if it was undefined
assert default_config.workspace_mount_path == '/opt/files2/workspace'
def test_env_overrides_compat_toml(monkeypatch, default_config, temp_toml_file):
# test that environment variables override TOML values using monkeypatch
# uses a toml file with sandbox_vars instead of a sandbox section
with open(temp_toml_file, 'w', encoding='utf-8') as toml_file:
toml_file.write("""
[llm]
model = "test-model"
api_key = "toml-api-key"
[core]
workspace_base = "/opt/files3/workspace"
sandbox_type = "local"
disable_color = true
sandbox_timeout = 500
sandbox_user_id = 1001
""")
monkeypatch.setenv('LLM_API_KEY', 'env-api-key')
monkeypatch.setenv('WORKSPACE_BASE', 'UNDEFINED')
monkeypatch.setenv('SANDBOX_TYPE', 'e2b')
monkeypatch.setenv('SANDBOX_TIMEOUT', '1000')
monkeypatch.setenv('SANDBOX_USER_ID', '1002')
load_from_toml(default_config, temp_toml_file)
# before finalize_config, workspace_mount_path is UndefinedString.UNDEFINED if it was not set
assert default_config.workspace_mount_path is UndefinedString.UNDEFINED
load_from_env(default_config, os.environ)
assert os.environ.get('LLM_MODEL') is None
assert default_config.get_llm_config().model == 'test-model'
assert default_config.get_llm_config('llm').model == 'test-model'
assert default_config.get_llm_config_from_agent().model == 'test-model'
assert default_config.get_llm_config().api_key == 'env-api-key'
# after we set workspace_base to 'UNDEFINED' in the environment,
# workspace_base should be set to that
# workspace_mount path is still UndefinedString.UNDEFINED
assert default_config.workspace_base is not UndefinedString.UNDEFINED
assert default_config.workspace_base == 'UNDEFINED'
assert default_config.workspace_mount_path is UndefinedString.UNDEFINED
assert default_config.workspace_mount_path == 'UNDEFINED'
assert default_config.sandbox.box_type == 'e2b'
assert default_config.disable_color is True
assert default_config.sandbox.timeout == 1000
assert default_config.sandbox.user_id == 1002
finalize_config(default_config)
# after finalize_config, workspace_mount_path is set to absolute path of workspace_base if it was undefined
assert default_config.workspace_mount_path == os.getcwd() + '/UNDEFINED'
def test_env_overrides_sandbox_toml(monkeypatch, default_config, temp_toml_file):
# test that environment variables override TOML values using monkeypatch
# uses a toml file with a sandbox section
with open(temp_toml_file, 'w', encoding='utf-8') as toml_file:
toml_file.write("""
[llm]
model = "test-model"
api_key = "toml-api-key"
[core]
workspace_base = "/opt/files3/workspace"
[sandbox]
box_type = "e2b"
timeout = 500
user_id = 1001
""")
monkeypatch.setenv('LLM_API_KEY', 'env-api-key')
monkeypatch.setenv('WORKSPACE_BASE', 'UNDEFINED')
monkeypatch.setenv('SANDBOX_TYPE', 'local')
monkeypatch.setenv('SANDBOX_TIMEOUT', '1000')
monkeypatch.setenv('SANDBOX_USER_ID', '1002')
load_from_toml(default_config, temp_toml_file)
# before finalize_config, workspace_mount_path is UndefinedString.UNDEFINED if it was not set
assert default_config.workspace_mount_path is UndefinedString.UNDEFINED
# before load_from_env, values are set to the values from the toml file
assert default_config.get_llm_config().api_key == 'toml-api-key'
assert default_config.sandbox.box_type == 'e2b'
assert default_config.sandbox.timeout == 500
assert default_config.sandbox.user_id == 1001
load_from_env(default_config, os.environ)
# values from env override values from toml
assert os.environ.get('LLM_MODEL') is None
assert default_config.get_llm_config().model == 'test-model'
assert default_config.get_llm_config().api_key == 'env-api-key'
assert default_config.sandbox.box_type == 'local'
assert default_config.sandbox.timeout == 1000
assert default_config.sandbox.user_id == 1002
finalize_config(default_config)
# after finalize_config, workspace_mount_path is set to absolute path of workspace_base if it was undefined
assert default_config.workspace_mount_path == os.getcwd() + '/UNDEFINED'
def test_sandbox_config_from_toml(default_config, temp_toml_file):
# Test loading configuration from a new-style TOML file
with open(temp_toml_file, 'w', encoding='utf-8') as toml_file:
toml_file.write(
"""
[core]
workspace_base = "/opt/files/workspace"
[llm]
model = "test-model"
[sandbox]
box_type = "local"
timeout = 1
container_image = "custom_image"
user_id = 1001
"""
)
load_from_toml(default_config, temp_toml_file)
load_from_env(default_config, os.environ)
finalize_config(default_config)
assert default_config.get_llm_config().model == 'test-model'
assert default_config.sandbox.box_type == 'local'
assert default_config.sandbox.timeout == 1
assert default_config.sandbox.container_image == 'custom_image'
assert default_config.sandbox.user_id == 1001
def test_defaults_dict_after_updates(default_config):
# Test that `defaults_dict` retains initial values after updates.
initial_defaults = default_config.defaults_dict
assert (
initial_defaults['workspace_mount_path']['default'] is UndefinedString.UNDEFINED
)
assert initial_defaults['default_agent']['default'] == 'CodeActAgent'
updated_config = AppConfig()
updated_config.get_llm_config().api_key = 'updated-api-key'
updated_config.get_llm_config('llm').api_key = 'updated-api-key'
updated_config.get_llm_config_from_agent('agent').api_key = 'updated-api-key'
updated_config.get_llm_config_from_agent('PlannerAgent').api_key = 'updated-api-key'
updated_config.default_agent = 'PlannerAgent'
defaults_after_updates = updated_config.defaults_dict
assert defaults_after_updates['default_agent']['default'] == 'CodeActAgent'
assert (
defaults_after_updates['workspace_mount_path']['default']
is UndefinedString.UNDEFINED
)
assert defaults_after_updates['sandbox']['box_type']['default'] == 'ssh'
assert defaults_after_updates['sandbox']['timeout']['default'] == 120
assert (
defaults_after_updates['sandbox']['container_image']['default']
== 'ghcr.io/opendevin/sandbox:main'
)
assert defaults_after_updates == initial_defaults
def test_invalid_toml_format(monkeypatch, temp_toml_file, default_config):
# Invalid TOML format doesn't break the configuration
monkeypatch.setenv('LLM_MODEL', 'gpt-5-turbo-1106')
monkeypatch.setenv('WORKSPACE_MOUNT_PATH', '/home/user/project')
monkeypatch.delenv('LLM_API_KEY', raising=False)
with open(temp_toml_file, 'w', encoding='utf-8') as toml_file:
toml_file.write('INVALID TOML CONTENT')
load_from_toml(default_config)
load_from_env(default_config, os.environ)
default_config.ssh_password = None # prevent leak
default_config.jwt_secret = None # prevent leak
for llm in default_config.llms.values():
llm.api_key = None # prevent leak
assert default_config.get_llm_config().model == 'gpt-5-turbo-1106'
assert default_config.get_llm_config().custom_llm_provider is None
assert default_config.workspace_mount_path == '/home/user/project'
def test_finalize_config(default_config):
# Test finalize config
assert default_config.workspace_mount_path is UndefinedString.UNDEFINED
default_config.sandbox.box_type = 'local'
finalize_config(default_config)
assert (
default_config.workspace_mount_path_in_sandbox
== default_config.workspace_mount_path
)
assert default_config.workspace_mount_path == os.path.abspath(
default_config.workspace_base
)
# tests for workspace, mount path, path in sandbox, cache dir
def test_workspace_mount_path_default(default_config):
assert default_config.workspace_mount_path is UndefinedString.UNDEFINED
finalize_config(default_config)
assert default_config.workspace_mount_path == os.path.abspath(
default_config.workspace_base
)
def test_workspace_mount_path_in_sandbox_local(default_config):
assert default_config.workspace_mount_path_in_sandbox == '/workspace'
default_config.sandbox.box_type = 'local'
finalize_config(default_config)
assert (
default_config.workspace_mount_path_in_sandbox
== default_config.workspace_mount_path
)
def test_workspace_mount_rewrite(default_config, monkeypatch):
default_config.workspace_base = '/home/user/project'
default_config.workspace_mount_rewrite = '/home/user:/sandbox'
monkeypatch.setattr('os.getcwd', lambda: '/current/working/directory')
finalize_config(default_config)
assert default_config.workspace_mount_path == '/sandbox/project'
def test_embedding_base_url_default(default_config):
default_config.get_llm_config().base_url = 'https://api.exampleapi.com'
finalize_config(default_config)
assert (
default_config.get_llm_config().embedding_base_url
== 'https://api.exampleapi.com'
)
def test_cache_dir_creation(default_config, tmpdir):
default_config.cache_dir = str(tmpdir.join('test_cache'))
finalize_config(default_config)
assert os.path.exists(default_config.cache_dir)
def test_api_keys_repr_str():
# Test LLMConfig
llm_config = LLMConfig(
api_key='my_api_key',
aws_access_key_id='my_access_key',
aws_secret_access_key='my_secret_key',
)
assert "api_key='******'" in repr(llm_config)
assert "aws_access_key_id='******'" in repr(llm_config)
assert "aws_secret_access_key='******'" in repr(llm_config)
assert "api_key='******'" in str(llm_config)
assert "aws_access_key_id='******'" in str(llm_config)
assert "aws_secret_access_key='******'" in str(llm_config)
# Check that no other attrs in LLMConfig have 'key' or 'token' in their name
# This will fail when new attrs are added, and attract attention
known_key_token_attrs_llm = [
'api_key',
'aws_access_key_id',
'aws_secret_access_key',
'input_cost_per_token',
'output_cost_per_token',
]
for attr_name in dir(LLMConfig):
if (
not attr_name.startswith('__')
and attr_name not in known_key_token_attrs_llm
):
assert (
'key' not in attr_name.lower()
), f"Unexpected attribute '{attr_name}' contains 'key' in LLMConfig"
assert (
'token' not in attr_name.lower() or 'tokens' in attr_name.lower()
), f"Unexpected attribute '{attr_name}' contains 'token' in LLMConfig"
# Test AgentConfig
# No attrs in AgentConfig have 'key' or 'token' in their name
agent_config = AgentConfig(memory_enabled=True, memory_max_threads=4)
for attr_name in dir(AgentConfig):
if not attr_name.startswith('__'):
assert (
'key' not in attr_name.lower()
), f"Unexpected attribute '{attr_name}' contains 'key' in AgentConfig"
assert (
'token' not in attr_name.lower() or 'tokens' in attr_name.lower()
), f"Unexpected attribute '{attr_name}' contains 'token' in AgentConfig"
# Test AppConfig
app_config = AppConfig(
llms={'llm': llm_config},
agents={'agent': agent_config},
e2b_api_key='my_e2b_api_key',
jwt_secret='my_jwt_secret',
ssh_password='my_ssh_password',
)
assert "e2b_api_key='******'" in repr(app_config)
assert "e2b_api_key='******'" in str(app_config)
assert "jwt_secret='******'" in repr(app_config)
assert "jwt_secret='******'" in str(app_config)
assert "ssh_password='******'" in repr(app_config)
assert "ssh_password='******'" in str(app_config)
# Check that no other attrs in AppConfig have 'key' or 'token' in their name
# This will fail when new attrs are added, and attract attention
known_key_token_attrs_app = ['e2b_api_key']
for attr_name in dir(AppConfig):
if (
not attr_name.startswith('__')
and attr_name not in known_key_token_attrs_app
):
assert (
'key' not in attr_name.lower()
), f"Unexpected attribute '{attr_name}' contains 'key' in AppConfig"
assert (
'token' not in attr_name.lower() or 'tokens' in attr_name.lower()
), f"Unexpected attribute '{attr_name}' contains 'token' in AppConfig"
def test_max_iterations_and_max_budget_per_task_from_toml(temp_toml_file):
temp_toml = """
[core]
max_iterations = 100
max_budget_per_task = 4.0
"""
config = AppConfig()
with open(temp_toml_file, 'w') as f:
f.write(temp_toml)
load_from_toml(config, temp_toml_file)
assert config.max_iterations == 100
assert config.max_budget_per_task == 4.0
def test_get_llm_config_arg(temp_toml_file):
temp_toml = """
[core]
max_iterations = 100
max_budget_per_task = 4.0
[llm.gpt3]
model="gpt-3.5-turbo"
api_key="redacted"
embedding_model="openai"
[llm.gpt4o]
model="gpt-4o"
api_key="redacted"
embedding_model="openai"
"""
with open(temp_toml_file, 'w') as f:
f.write(temp_toml)
llm_config = get_llm_config_arg('gpt3', temp_toml_file)
assert llm_config.model == 'gpt-3.5-turbo'
assert llm_config.embedding_model == 'openai'