1
1
import os
2
2
from pathlib import Path
3
3
4
- from opendevin .core .config import config
5
4
from opendevin .events .observation import (
6
5
ErrorObservation ,
7
6
FileReadObservation ,
10
9
)
11
10
12
11
13
- def resolve_path (file_path , working_directory ):
12
+ def resolve_path (
13
+ file_path : str ,
14
+ working_directory : str ,
15
+ workspace_base : str ,
16
+ workspace_mount_path_in_sandbox : str ,
17
+ ):
18
+ """Resolve a file path to a path on the host filesystem.
19
+
20
+ Args:
21
+ file_path: The path to resolve.
22
+ working_directory: The working directory of the agent.
23
+ workspace_mount_path_in_sandbox: The path to the workspace inside the sandbox.
24
+ workspace_base: The base path of the workspace on the host filesystem.
25
+
26
+ Returns:
27
+ The resolved path on the host filesystem.
28
+ """
14
29
path_in_sandbox = Path (file_path )
15
30
16
31
# Apply working directory
@@ -22,16 +37,16 @@ def resolve_path(file_path, working_directory):
22
37
abs_path_in_sandbox = path_in_sandbox .resolve ()
23
38
24
39
# If the path is outside the workspace, deny it
25
- if not abs_path_in_sandbox .is_relative_to (config . workspace_mount_path_in_sandbox ):
40
+ if not abs_path_in_sandbox .is_relative_to (workspace_mount_path_in_sandbox ):
26
41
raise PermissionError (f'File access not permitted: { file_path } ' )
27
42
28
43
# Get path relative to the root of the workspace inside the sandbox
29
44
path_in_workspace = abs_path_in_sandbox .relative_to (
30
- Path (config . workspace_mount_path_in_sandbox )
45
+ Path (workspace_mount_path_in_sandbox )
31
46
)
32
47
33
48
# Get path relative to host
34
- path_in_host_workspace = Path (config . workspace_base ) / path_in_workspace
49
+ path_in_host_workspace = Path (workspace_base ) / path_in_workspace
35
50
36
51
return path_in_host_workspace
37
52
@@ -53,9 +68,13 @@ def read_lines(all_lines: list[str], start=0, end=-1):
53
68
return all_lines [begin :end ]
54
69
55
70
56
- async def read_file (path , workdir , start = 0 , end = - 1 ) -> Observation :
71
+ async def read_file (
72
+ path , workdir , workspace_base , workspace_mount_path_in_sandbox , start = 0 , end = - 1
73
+ ) -> Observation :
57
74
try :
58
- whole_path = resolve_path (path , workdir )
75
+ whole_path = resolve_path (
76
+ path , workdir , workspace_base , workspace_mount_path_in_sandbox
77
+ )
59
78
except PermissionError :
60
79
return ErrorObservation (
61
80
f"You're not allowed to access this path: { path } . You can only access paths inside the workspace."
@@ -84,11 +103,21 @@ def insert_lines(
84
103
return new_lines
85
104
86
105
87
- async def write_file (path , workdir , content , start = 0 , end = - 1 ) -> Observation :
106
+ async def write_file (
107
+ path ,
108
+ workdir ,
109
+ workspace_base ,
110
+ workspace_mount_path_in_sandbox ,
111
+ content ,
112
+ start = 0 ,
113
+ end = - 1 ,
114
+ ) -> Observation :
88
115
insert = content .split ('\n ' )
89
116
90
117
try :
91
- whole_path = resolve_path (path , workdir )
118
+ whole_path = resolve_path (
119
+ path , workdir , workspace_base , workspace_mount_path_in_sandbox
120
+ )
92
121
if not os .path .exists (os .path .dirname (whole_path )):
93
122
os .makedirs (os .path .dirname (whole_path ))
94
123
mode = 'w' if not os .path .exists (whole_path ) else 'r+'
0 commit comments