Skip to content

Commit 53feda4

Browse files
committed
Copy logs to local folder
1 parent 97fcd0e commit 53feda4

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

@@ -121,13 +122,34 @@ def execute_round(self, agents: list[Player]):
121122

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

133155
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)