Skip to content

Commit 9119057

Browse files
committed
Add log_local, log_env vars
1 parent a660496 commit 9119057

5 files changed

Lines changed: 21 additions & 11 deletions

File tree

codeclash/agents/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ def get_agent(config: dict, prompts: dict, game: CodeGame) -> Player:
2121
environment,
2222
GameContext(
2323
id=game.game_id,
24+
log_env=game.log_env,
25+
log_local=game.log_local,
2426
name=game.name,
2527
player_id=config["name"],
2628
prompts=prompts,

codeclash/agents/abstract.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from minisweagent import Environment
66

77
from codeclash.agents.utils import GameContext
8-
from codeclash.constants import DIR_LOGS, GH_ORG
8+
from codeclash.constants import GH_ORG
99
from codeclash.utils.environment import assert_zero_exit_code
1010
from codeclash.utils.log import get_logger
1111

@@ -26,7 +26,7 @@ def __init__(
2626
self.game_context.render_and_set_prompts()
2727
self.logger = get_logger(
2828
self.name,
29-
log_path=DIR_LOGS / game_context.id / f"{self.name}.log",
29+
log_path=self.game_context.log_local / f"{self.name}.log",
3030
emoji="👤",
3131
)
3232

codeclash/agents/minisweagent.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
from codeclash.agents.abstract import Player
1818
from codeclash.agents.utils import GameContext, resolve_api_key
19-
from codeclash.constants import DIR_LOGS
2019
from codeclash.utils.environment import copy_file_to_container
2120

2221

@@ -97,8 +96,7 @@ def run(self):
9796
print(exc_message)
9897
finally:
9998
traj_path = (
100-
DIR_LOGS
101-
/ self.game_context.id
99+
self.game_context.log_local
102100
/ f"{self.name}_r{self.game_context.round}.traj.json"
103101
)
104102
save_traj(
@@ -107,5 +105,9 @@ def run(self):
107105
exit_status=exit_status,
108106
result=result,
109107
)
110-
copy_file_to_container(self.environment, traj_path, traj_path)
108+
copy_file_to_container(
109+
self.environment,
110+
traj_path,
111+
self.game_context.log_env / traj_path.name,
112+
)
111113
self.commit()

codeclash/agents/utils.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
from dataclasses import asdict, dataclass
3+
from pathlib import Path
34

45
from dotenv import load_dotenv
56
from jinja2 import Template
@@ -26,6 +27,8 @@ class GameContext:
2627
"""
2728

2829
id: str
30+
log_env: Path
31+
log_local: Path
2932
name: str
3033
player_id: str
3134
prompts: dict

codeclash/games/abstract.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import getpass
12
import os
23
import subprocess
34
import time
@@ -30,9 +31,10 @@ def __init__(self, config: dict):
3031
self.rounds: int = self.config.get("rounds", 1)
3132
self.round: int = 0
3233
self.game_id: str = f"{self.name}{time.strftime('%y%m%d%H%M%S')}"
33-
self.log_path: Path = (DIR_WORK / DIR_LOGS / self.game_id).resolve()
34+
self.log_env: Path = (DIR_WORK / DIR_LOGS / self.game_id).resolve()
35+
self.log_local: Path = (DIR_LOGS / getpass.getuser() / self.game_id).resolve()
3436
self.logger = get_logger(
35-
self.name, log_path=DIR_LOGS / self.game_id / "game.log", emoji="🏓"
37+
self.name, log_path=self.log_local / "game.log", emoji="🏓"
3638
)
3739
self.environment: DockerEnvironment = self.get_environment()
3840
assert len(config["players"]) >= 2, "At least two players are required"
@@ -120,7 +122,8 @@ def _pre_round_setup(self, agents: list[Player]):
120122

121123
# Ensure the log path + file exists
122124
assert_zero_exit_code(
123-
self.environment.execute(f"mkdir -p {self.log_path}"), logger=self.logger
125+
self.environment.execute(f"mkdir -p {self.log_env}"),
126+
logger=self.logger,
124127
)
125128
assert_zero_exit_code(
126129
self.environment.execute(f"touch {self.round_log_path}"), logger=self.logger
@@ -156,7 +159,7 @@ def _post_round_setup(self, agents: list[Player]):
156159
copy_file_from_container(
157160
self.environment,
158161
self.round_log_path,
159-
DIR_LOGS / f"{self.game_id}/round_{self.round}.log",
162+
self.log_local / self.round_log_path.name,
160163
)
161164
except Exception:
162165
self.logger.error(
@@ -186,4 +189,4 @@ def round_log_path(self) -> Path:
186189
"""
187190
Get the path to the current round's log file.
188191
"""
189-
return self.log_path / f"round_{self.round}.log"
192+
return self.log_env / f"round_{self.round}.log"

0 commit comments

Comments
 (0)