Skip to content

Commit 16ce391

Browse files
committed
Merge branch 'main' of github.com:emagedoc/CodeClash
2 parents 55f1330 + 53feda4 commit 16ce391

3 files changed

Lines changed: 57 additions & 9 deletions

File tree

codeclash/agents/minisweagent.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
from codeclash.agents.abstract import Player
1717
from codeclash.agents.utils import resolve_api_key
18+
from codeclash.constants import DIR_LOGS
1819

1920

2021
class ClashAgent(DefaultAgent):
@@ -88,9 +89,11 @@ def run(self):
8889
result = exc_message
8990
print(exc_message)
9091
finally:
92+
player_id = self.name.split("_")[-1]
9193
save_traj(
9294
self.agent, # type: ignore
93-
Path(f"{self.name}_r{self.format_vars['round']}.traj.json"),
95+
DIR_LOGS
96+
/ f"{self.format_vars['game_id']}/{player_id}_r{self.format_vars['round']}.traj.json",
9497
exit_status=exit_status,
9598
result=result,
9699
)

codeclash/games/abstract.py

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
import subprocess
3+
import traceback
34
from abc import ABC, abstractmethod
45
from collections import Counter
56
from pathlib import Path
@@ -10,7 +11,7 @@
1011

1112
from codeclash.agents.abstract import Player
1213
from codeclash.constants import DIR_LOGS, DIR_WORK, GH_ORG
13-
from codeclash.games.utils import copy_between_containers
14+
from codeclash.games.utils import copy_between_containers, copy_file_from_container
1415
from codeclash.utils.environment import assert_zero_exit_code
1516

1617

@@ -119,13 +120,34 @@ def execute_round(self, agents: list[Player]):
119120

120121
def _post_round_setup(self, agents: list[Player]):
121122
for agent in agents:
122-
copy_between_containers(
123-
self.environment,
124-
agent.environment,
125-
self.round_log_path,
126-
f"{agent.environment.config.cwd}/logs/round_{self.round}.log",
127-
)
128-
print(f"Copied round log to {agent.name}'s container.")
123+
try:
124+
copy_between_containers(
125+
self.environment,
126+
agent.environment,
127+
self.round_log_path,
128+
f"{agent.environment.config.cwd}/logs/round_{self.round}.log",
129+
)
130+
except Exception:
131+
print(
132+
f"Error copying round log to {agent.name}'s container: {traceback.format_exc()}"
133+
)
134+
else:
135+
print(f"Copied round log to {agent.name}'s container.")
136+
137+
try:
138+
copy_file_from_container(
139+
self.environment,
140+
self.round_log_path,
141+
DIR_LOGS / f"{self.game_id}/round_{self.round}.log",
142+
)
143+
except Exception:
144+
print(
145+
f"Error copying round log to {agent.name}'s container: {traceback.format_exc()}"
146+
)
147+
else:
148+
print(
149+
f"Copied round log from {agent.name}'s container to local log dir."
150+
)
129151
print(f"Round {self.round} completed.")
130152

131153
def run_round(self, agents: list[Player]):

codeclash/games/utils.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,26 @@ def copy_file_to_container(
7676
raise RuntimeError(
7777
f"Failed to copy {src_path} to {container.container_id}:{dest_path}: {result.stdout}{result.stderr}"
7878
)
79+
80+
81+
def copy_file_from_container(
82+
container: DockerEnvironment,
83+
src_path: str | Path,
84+
dest_path: str | Path,
85+
):
86+
"""
87+
Copy a file from a Docker container to the local filesystem.
88+
"""
89+
cmd = [
90+
"docker",
91+
"cp",
92+
f"{container.container_id}:{src_path}",
93+
str(dest_path),
94+
]
95+
Path(dest_path).parent.mkdir(parents=True, exist_ok=True)
96+
result = subprocess.run(cmd, check=False, capture_output=True, text=True)
97+
if result.returncode != 0:
98+
raise RuntimeError(
99+
f"Failed to copy {container.container_id}:{src_path} to {dest_path}: {result.stdout}{result.stderr}"
100+
)
101+
return result

0 commit comments

Comments
 (0)