Skip to content

Commit 444198b

Browse files
committed
Enh: Merge all metadata in tournament and write out
1 parent d79f925 commit 444198b

4 files changed

Lines changed: 30 additions & 12 deletions

File tree

codeclash/games/abstract.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ def get_metadata(self) -> dict:
9292
return self._metadata
9393

9494
def end(self, cleanup: bool = False):
95-
(self.log_local / "metadata.json").write_text(json.dumps(self.get_metadata()))
9695
if cleanup:
9796
for artifact in self.artifacts:
9897
if artifact.exists():

codeclash/tournaments/abstract.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@
33
import traceback
44
from pathlib import Path
55

6-
from codeclash.agents import get_agent
7-
from codeclash.agents.abstract import Player
8-
from codeclash.agents.utils import GameContext
9-
from codeclash.constants import DIR_LOGS, DIR_WORK
6+
from codeclash.constants import DIR_LOGS
107
from codeclash.utils.environment import create_file_on_container
118
from codeclash.utils.log import get_logger
129

codeclash/tournaments/pvp_training.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
PvP training mode where multiple agents compete against each other.
33
"""
44

5+
import json
6+
57
from codeclash.agents import get_agent
68
from codeclash.agents.abstract import Player
79
from codeclash.agents.utils import GameContext
@@ -34,6 +36,15 @@ def __init__(
3436
def rounds(self) -> int:
3537
return self.config["tournament"]["rounds"]
3638

39+
def get_metadata(self) -> dict:
40+
# will be saved in end()
41+
return {
42+
**super().get_metadata(),
43+
"scoreboard": self.scoreboard,
44+
"game": self.game.get_metadata(),
45+
"agents": [agent.get_metadata() for agent in self.agents],
46+
}
47+
3748
def get_agent(self, agent_config: dict, prompts: dict) -> Player:
3849
"""Create an agent with environment and game context."""
3950
environment = self.game.get_environment(
@@ -60,7 +71,7 @@ def run(self) -> None:
6071
for round_num in range(1, self.rounds + 1):
6172
self.run_training_round(round_num)
6273
finally:
63-
self.cleanup()
74+
self.end()
6475

6576
def run_training_round(self, round_num: int) -> None:
6677
"""Execute a single training round."""
@@ -93,8 +104,11 @@ def run_agent(self, agent: Player, round_num: int) -> None:
93104
agent.run()
94105
agent.post_run_hook(round=round_num)
95106

96-
def cleanup(self) -> None:
97-
"""Clean up game resources and push agents if requested."""
107+
def end(self) -> None:
108+
"""Save output files, clean up game resources and push agents if requested."""
109+
(self.local_output_dir / "metadata.json").write_text(
110+
json.dumps(self.game.get_metadata())
111+
)
98112
self.game.end(self.cleanup_on_end)
99113
if self.push_agent:
100114
for agent in self.agents:

codeclash/tournaments/single_player_training.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ def __init__(self, config: dict, cleanup: bool = False):
3434
def rounds(self) -> int:
3535
return self.config["tournament"]["rounds"]
3636

37+
def get_metadata(self) -> dict:
38+
return {
39+
**super().get_metadata(),
40+
"scoreboard": self.scoreboard,
41+
"game": self.game.get_metadata(),
42+
"agents": [self.agent.get_metadata(), self.mirror_agent.get_metadata()],
43+
}
44+
3745
def get_game_context(self, agent_config: dict, *, round: int) -> GameContext:
3846
"""Create a game context for an agent."""
3947
return GameContext(
@@ -72,7 +80,7 @@ def run(self):
7280
if self.config["tournament"]["evaluate_matrix"]:
7381
self.evaluate()
7482
finally:
75-
self.cleanup()
83+
self.end()
7684

7785
def run_training_round(self, round_num: int) -> None:
7886
"""Execute a single training round, i.e., run the game, then run the agent."""
@@ -110,11 +118,11 @@ def set_mirror_state_to_round(self, round_num: int):
110118
full_diff = filter_git_diff(full_diff)
111119
self.mirror_agent.reset_and_apply_patch(full_diff)
112120

113-
def cleanup(self):
121+
def end(self):
114122
"""Clean up game resources."""
115123
self.game.end(self.cleanup_on_end)
116124

117-
def evaluate(self, n_repetitions: int = 3):
125+
def evaluate(self, n_repetitions: int = 3) -> None:
118126
"""Evaluate the agent's performance by
119127
calculating the matrix of every round against each other.
120128
"""
@@ -150,4 +158,4 @@ def evaluate(self, n_repetitions: int = 3):
150158
)
151159
matrix[p1_round][p2_round].append(winner)
152160
self.logger.info(f"Evaluation matrix: {matrix}")
153-
return matrix
161+
self._metadata.setdefault("evaluation", {})["matrix"] = matrix

0 commit comments

Comments
 (0)