Skip to content

Commit 1c23c1b

Browse files
docker
1 parent 49c9333 commit 1c23c1b

File tree

3 files changed

+67
-234
lines changed

3 files changed

+67
-234
lines changed

commit0/configs/user.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
defaults:
22
- base
33
- _self_
4+
5+
backend: local

commit0/harness/execution_context.py

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ def read_stream(stream: modal.io_streams.StreamReader) -> str:
3232
strings.append(line)
3333
return "\n".join(strings)
3434

35+
3536
class ExecutionContext(ABC):
3637
def __init__(
3738
self,
@@ -46,7 +47,11 @@ def __init__(
4647
The execution context will persist for the lifetime of this object.
4748
The execution context can be a Docker container or Modal sandbox.
4849
"""
49-
raise NotImplementedError
50+
self.spec = spec
51+
self.logger = logger
52+
self.eval_file = eval_file
53+
self.timeout = timeout
54+
self.log_dir = log_dir
5055

5156
def copy_ssh_pubkey_from_remote(self) -> None:
5257
raise NotImplementedError
@@ -60,14 +65,35 @@ def exec_run_with_timeout(self, command: str, timeout: int) -> None:
6065
def exec_run(self, command: str) -> None:
6166
raise NotImplementedError
6267

63-
def copy_from_remote(self, remote_path, local_path) -> None:
68+
def copy_from_remote(self, remote_path: Path, local_path: Path) -> None:
6469
raise NotImplementedError
6570

66-
def delete_file_from_remote(self, remote_path) -> None:
71+
def delete_file_from_remote(self, remote_path: Path) -> None:
6772
raise NotImplementedError
6873

74+
def write_test_output(self, test_output, timed_out):
75+
test_output_path = self.log_dir / "test_output.txt"
76+
with open(test_output_path, "w") as f:
77+
f.write(test_output)
78+
if timed_out:
79+
f.write(f"\n\nTimeout error: {timeout} seconds exceeded.")
80+
raise EvaluationError(
81+
self.spec.repo,
82+
f"Test timed out after {timeout} seconds.",
83+
self.logger,
84+
)
85+
86+
# copy back report.json if there is any
87+
report_file = Path(self.spec.repo_directory) / "report.json"
88+
# Run the test command inside the container to check if the file exists
89+
exit_code, output = self.exec_run(f"test -e {report_file}")
90+
# Check the exit code of the command
91+
if exit_code == 0:
92+
self.copy_from_remote(report_file, self.log_dir / "report.json")
93+
self.delete_file_from_remote(str(report_file))
94+
6995
def __enter__(self):
70-
raise NotImplementedError
96+
return self
7197

7298
def __exit__(self, exc_type, exc_value, exc_traceback):
7399
raise NotImplementedError
@@ -82,8 +108,9 @@ def __init__(
82108
timeout: int,
83109
log_dir: Path,
84110
):
111+
super().__init__(spec, logger, eval_file, timeout, log_dir)
112+
85113
self.client = docker.from_env()
86-
self.logger = logger
87114
self.container = create_container(
88115
client=self.client,
89116
image_name=spec.repo_image_key,
@@ -92,6 +119,8 @@ def __init__(
92119
)
93120
self.container.start()
94121
self.copy_ssh_pubkey_from_remote()
122+
copy_to_container(self.container, eval_file, Path("/eval.sh"))
123+
95124

96125
def copy_ssh_pubkey_from_remote(self) -> None:
97126
copy_ssh_pubkey_from_container(self.container)
@@ -111,9 +140,6 @@ def copy_from_remote(self, remote_path: Path, local_path: Path) -> None:
111140
def delete_file_from_remote(self, remote_path: Path) -> None:
112141
delete_file_from_container(self.container, str(remote_path))
113142

114-
def __enter__(self):
115-
return self
116-
117143
def __exit__(self, exc_type, exc_value, exc_traceback):
118144
cleanup_container(self.client, self.container, self.logger)
119145
close_logger(self.logger)
@@ -128,22 +154,22 @@ def __init__(
128154
timeout: int,
129155
log_dir: Path,
130156
):
131-
self.logger = logger
157+
super().__init_(spec, logger, eval_file, timeout, log_dir)
158+
132159
# the image must exist on dockerhub
133160
reponame = spec.repo.split("/")[-1]
134161
image_name = f"wentingzhao/{reponame}"
135-
image = modal.Image.from_registry(image_name)
162+
image = (
163+
modal.Image.from_registry(image_name)
164+
.copy_local_file(eval_file, "/eval.sh")
165+
)
136166

137-
self.nfs = modal.NetworkFileSystem.ephemeral().__enter__()
138167
self.sandbox = modal.Sandbox.create(
139168
"sleep",
140169
"infinity",
141170
image=image,
142-
network_file_systems={
143-
"/vol": self.nfs,
144-
},
145-
cpu=8.0,
146-
timeout=30,
171+
cpu=4.0,
172+
timeout=300,
147173
)
148174

149175
self.copy_ssh_pubkey_from_remote()
@@ -206,9 +232,6 @@ def copy_from_remote(self, remote_path: Path, local_path: Path) -> None:
206232
def delete_file_from_remote(src, remote_path):
207233
self.sandbox.exec("bash", "-c", f"rm {str(remote_path)}")
208234

209-
def __enter__(self):
210-
return self
211-
212235
def __exit__(self, exc_type, exc_value, exc_traceback):
213-
# self.nfs.__exit__()
214-
pass
236+
self.sandbox.terminate()
237+
close_logger(self.logger)

0 commit comments

Comments
 (0)