Skip to content

Commit dada004

Browse files
authored
Remove config from files (#3039)
1 parent 9cf2b5b commit dada004

File tree

3 files changed

+54
-12
lines changed

3 files changed

+54
-12
lines changed

opendevin/controller/agent_controller.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ async def start_delegate(self, action: AgentDelegateAction):
274274
)
275275
await self.delegate.set_agent_state_to(AgentState.RUNNING)
276276

277-
async def _step(self):
277+
async def _step(self) -> None:
278278
if self.get_agent_state() != AgentState.RUNNING:
279279
await asyncio.sleep(1)
280280
return

opendevin/runtime/server/files.py

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import os
22
from pathlib import Path
33

4-
from opendevin.core.config import config
54
from opendevin.events.observation import (
65
ErrorObservation,
76
FileReadObservation,
@@ -10,7 +9,23 @@
109
)
1110

1211

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+
"""
1429
path_in_sandbox = Path(file_path)
1530

1631
# Apply working directory
@@ -22,16 +37,16 @@ def resolve_path(file_path, working_directory):
2237
abs_path_in_sandbox = path_in_sandbox.resolve()
2338

2439
# 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):
2641
raise PermissionError(f'File access not permitted: {file_path}')
2742

2843
# Get path relative to the root of the workspace inside the sandbox
2944
path_in_workspace = abs_path_in_sandbox.relative_to(
30-
Path(config.workspace_mount_path_in_sandbox)
45+
Path(workspace_mount_path_in_sandbox)
3146
)
3247

3348
# 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
3550

3651
return path_in_host_workspace
3752

@@ -53,9 +68,13 @@ def read_lines(all_lines: list[str], start=0, end=-1):
5368
return all_lines[begin:end]
5469

5570

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:
5774
try:
58-
whole_path = resolve_path(path, workdir)
75+
whole_path = resolve_path(
76+
path, workdir, workspace_base, workspace_mount_path_in_sandbox
77+
)
5978
except PermissionError:
6079
return ErrorObservation(
6180
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(
84103
return new_lines
85104

86105

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:
88115
insert = content.split('\n')
89116

90117
try:
91-
whole_path = resolve_path(path, workdir)
118+
whole_path = resolve_path(
119+
path, workdir, workspace_base, workspace_mount_path_in_sandbox
120+
)
92121
if not os.path.exists(os.path.dirname(whole_path)):
93122
os.makedirs(os.path.dirname(whole_path))
94123
mode = 'w' if not os.path.exists(whole_path) else 'r+'

opendevin/runtime/server/runtime.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,13 +169,26 @@ async def run_ipython(self, action: IPythonRunCellAction) -> Observation:
169169
async def read(self, action: FileReadAction) -> Observation:
170170
# TODO: use self.file_store
171171
working_dir = self.sandbox.get_working_directory()
172-
return await read_file(action.path, working_dir, action.start, action.end)
172+
return await read_file(
173+
action.path,
174+
working_dir,
175+
config.workspace_base,
176+
config.workspace_mount_path_in_sandbox,
177+
action.start,
178+
action.end,
179+
)
173180

174181
async def write(self, action: FileWriteAction) -> Observation:
175182
# TODO: use self.file_store
176183
working_dir = self.sandbox.get_working_directory()
177184
return await write_file(
178-
action.path, working_dir, action.content, action.start, action.end
185+
action.path,
186+
working_dir,
187+
config.workspace_base,
188+
config.workspace_mount_path_in_sandbox,
189+
action.content,
190+
action.start,
191+
action.end,
179192
)
180193

181194
async def browse(self, action: BrowseURLAction) -> Observation:

0 commit comments

Comments
 (0)