Skip to content

Commit 3875d3b

Browse files
committed
Crucial fix: Ensure agent code is copied over properly
1 parent dd90fbc commit 3875d3b

2 files changed

Lines changed: 19 additions & 0 deletions

File tree

codeclash/games/game.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,14 @@ def get_metadata(self) -> dict:
200200
def _pre_round_setup(self, agents: list[Player]):
201201
"""Copy agent codebases into game's container"""
202202
for agent in agents:
203+
# First make sure that the folder in the game container is removed
204+
# This is important for 2 reasons:
205+
# 1. Subtle differences in docker cp behavior regarding to trailing slashes
206+
# depending on whether the destination exists
207+
# 2. If files are removed from the agent container, they should also be removed
208+
# from the game container
203209
self.logger.debug(f"Copying {agent.name}'s codebase")
210+
assert_zero_exit_code(self.environment.execute(f"rm -rf /{agent.name}"), logger=self.logger)
204211
copy_between_containers(
205212
src_container=agent.environment,
206213
dest_container=self.environment,

codeclash/utils/environment.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ def copy_between_containers(
2323
):
2424
"""
2525
Copy files from one Docker container to another via a temporary local directory.
26+
27+
Be extremely careful with trailing slashes in src_path and dest_path, the behavior
28+
of docker cp is also different depending on whether the destination exists.
2629
"""
30+
print(f"Copy between containers: {src_path} -> {dest_path}")
2731
with tempfile.TemporaryDirectory() as temp_dir:
2832
temp_path = Path(temp_dir) / Path(src_path).name
2933

@@ -66,6 +70,9 @@ def copy_to_container(
6670
Copy a file or directory from the local filesystem to a Docker container.
6771
6872
The copy operation is recursive for directories.
73+
74+
Be extremely careful with trailing slashes in src_path and dest_path, the behavior
75+
of docker cp is also different depending on whether the destination exists.
6976
"""
7077
if not str(dest_path).startswith("/"):
7178
# If not an absolute path, assume relative to container's cwd
@@ -76,6 +83,7 @@ def copy_to_container(
7683
str(src_path),
7784
f"{container.container_id}:{dest_path}",
7885
]
86+
print(f"Copy to container: cmd={cmd}")
7987
# Ensure destination folder exists
8088
assert_zero_exit_code(container.execute(f"mkdir -p {Path(dest_path).parent}"))
8189
result = subprocess.run(cmd, check=False, capture_output=True, text=True)
@@ -95,13 +103,17 @@ def copy_from_container(
95103
Copy a file or directory from a Docker container to the local filesystem.
96104
97105
The copy operation is recursive for directories.
106+
107+
Be extremely careful with trailing slashes in src_path and dest_path, the behavior
108+
of docker cp is also different depending on whether the destination exists.
98109
"""
99110
cmd = [
100111
"docker",
101112
"cp",
102113
f"{container.container_id}:{src_path}",
103114
str(dest_path),
104115
]
116+
print(f"Copy from container: cmd={cmd}")
105117
Path(dest_path).parent.mkdir(parents=True, exist_ok=True)
106118
result = subprocess.run(cmd, check=False, capture_output=True, text=True)
107119
if result.returncode != 0:

0 commit comments

Comments
 (0)